-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph_Representation1.cpp
115 lines (99 loc) · 2.5 KB
/
Graph_Representation1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/********************************************
* GRAPH REPRESENTATIONS using SET
* @author Amirul Islam (shiningflash)
*
* Undirected Graph__
* This approach cannot be used for graphs containing parallel edges.
* Since sets are internally implemented as binary search trees,
* an edge between two vertices can be searched in O(logV) time
*******************************************/
#include <cstdio>
#include <iomanip>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <sstream>
#include <stack>
#include <list>
#include <iostream>
#include <assert.h>
#define mem(x,y) memset(x,y,sizeof(x))
#define CLEAR(x) memset(x,0,sizeof(x))
#define pb push_back
#define Sort(v) sort(v.begin(),v.end())
#define _sort(s, n) sort(s, s+n)
#define sqr(x) ((x)*(x))
#define le 50001
#define ERR 1e-9
#define pi (2*acos(0))
#define PI 3.141592653589793
#define MX 1e9
#define scanint(a) scanf("%d", &a)
#define scanLLD(a) scanf("%lld", &a)
typedef long long ll;
using namespace std;
/* -------------------------- */
void printGraph(int nVertex, set <int, greater<int> > *adj) {
for (int i = 0; i < nVertex; i++) {
set <int, greater<int> > lst = adj[i];
printf("%d => ", i);
for (auto it = lst.begin(); it != lst.end(); it++)
cout << *it << " ";
printf("\n");
}
}
void searchGraph(int nVertex, set <int, greater<int> > *adj, int src, int dest) {
auto it = adj[src].find(dest);
if (it == adj[src].end()) // not found
printf("%d to %d: No, Edge not found!\n", src, dest);
else
printf("%d to %d: Yes, Edge Found!\n", src, dest);
}
int main() {
// build graph
int nVertex, nEdge, u, v;
scanf("%d %d", &nVertex, &nEdge);
set <int, greater<int> > *adj = new set <int, greater<int> >[nVertex];
// for undirected graph
for (int i = 0; i < nEdge; i++) {
scanf("%d %d", &u, &v);
adj[u].insert(v);
adj[v].insert(u);
}
// print graph
printGraph(nVertex, adj);
printf("\n");
// search graph
// @complexity O(logV)
searchGraph(nVertex, adj, 2, 1); // src - 2, dest - 1
searchGraph(nVertex, adj, 0, 3); // src - 0, dest - 3
}
/******************
INPUT
---
- here, 5 nVertex, 7 nEdge
5 7
0 1
0 4
1 2
1 3
1 4
2 3
3 4
OUTPUT:
-----
0 => 4 1
1 => 4 3 2 0
2 => 3 1
3 => 4 2 1
4 => 3 1 0
2 to 1: Yes, Edge Found!
0 to 3: No, Edge not found!
*****************/