-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDiGraph.cpp
341 lines (284 loc) · 6.63 KB
/
DiGraph.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
int V, E;
int input[1000][2];
/*BEGIN Graph class */
class DGraph {
int V;
int E;
public:
vector<int> adj[1000];
DGraph(int Vx);
void addEdge(int v, int w);
void printDG();
DGraph reverse();
bool isCycle();
int getV(){
return V;
}
};
DGraph DGraph::reverse() {
DGraph G(V);
for (int i=0; i < V; i++) {
for (vector<int>::iterator it= adj[i].begin(); it != adj[i].end(); it++) {
G.addEdge(*it,i);
}
}
return G;
}
bool DGraph::isCycle(){
// Create a vector to store indegrees of all
// vertices. Initialize all indegrees as 0.
vector<int> in_degree(V, 0);
vector<int>::iterator it;
// Traverse adjacency lists to fill indegrees of
// vertices. This step takes O(V+E) time
for (int u = 0; u < V; u++) {
for (it = adj[u].begin(); it!=adj[u].end(); it++)
in_degree[*it]++;
}
// Create an queue and enqueue all vertices with
// indegree 0
queue<int> q;
for (int i = 0; i < V; i++)
if (in_degree[i] == 0)
q.push(i);
// Initialize count of visited vertices
int cnt = 0;
// Create a vector to store result (A topological
// ordering of the vertices)
vector<int> top_order;
// One by one dequeue vertices from queue and enqueue
// adjacents if indegree of adjacent becomes 0
while (!q.empty()) {
// Extract front of queue (or perform dequeue)
// and add it to topological order
int u = q.front();
q.pop();
top_order.push_back(u);
// Iterate through all its neighbouring nodes
// of dequeued node u and decrease their in-degree
// by 1
vector<int>::iterator itr;
for (itr = adj[u].begin(); itr != adj[u].end(); itr++)
// If in-degree becomes zero, add it to queue
if (--in_degree[*itr] == 0)
q.push(*itr);
cnt++;
}
// Check if there was a cycle
if (cnt != V)
return true;
else
return false;
}
DGraph::DGraph(int Vx) {
V = Vx;
E=0;
for (int i=0; i<V; i++) {
vector<int> tmp;
adj[i] = tmp;
}
}
void DGraph::addEdge(int v, int w) {
E++;
adj[v].push_back(w);
}
void DGraph::printDG() {
for (int i=0; i< V; i++) {
for (vector<int>::iterator it = adj[i].begin(); it != adj[i].end(); it++) {
cout << i << " " << *it << endl;
}
}
}
/*END Graph class */
/*BEGIN DFS */
class DFSPaths {
int s;
bool marked[1000];
int edgeTo[1000];
void dfs(DGraph G, int v);
public:
DFSPaths(DGraph G, int v);
bool hasPathTo(int v);
vector<int> pathTo(int v);
};
bool DFSPaths::hasPathTo(int v) {
return marked[v];
}
vector<int> DFSPaths::pathTo(int v) {
vector<int> path;
if (!hasPathTo(v)) return path;
for (int x = v; x!=s; x = edgeTo[x]) {
path.push_back(x);
}
path.push_back(s);
reverse(path.begin(), path.end());
return path;
}
DFSPaths::DFSPaths(DGraph G, int v) {
s = v;
memset(marked, false, sizeof(marked));
memset(edgeTo, -1, sizeof(edgeTo));
dfs(G, s);
}
void DFSPaths::dfs(DGraph G, int v) {
marked[v] = true;
for (vector<int>::iterator it=G.adj[v].begin(); it != G.adj[v].end(); it++) {
if (!marked[*it]) {
dfs(G, *it);
edgeTo[*it] = v;
}
}
}
/*END DFS*/
/*BEGIN BFS*/
class BFSPaths{
int s;
bool marked[1000];
int edgeTo[1000];
void bfs(DGraph G, int v);
public:
BFSPaths(DGraph G, int v);
bool hasPathTo(int v);
vector<int> pathTo(int v);
};
BFSPaths::BFSPaths(DGraph G, int v) {
s = v;
memset(marked, false, sizeof(marked));
memset(edgeTo, -1, sizeof(edgeTo));
bfs(G, s);
}
void BFSPaths::bfs(DGraph G, int v) {
queue<int> bfsq;
marked[v] = true;
bfsq.push(v);
while (!bfsq.empty()) {
int tmp = bfsq.front();
bfsq.pop();
for (vector<int>::iterator it=G.adj[tmp].begin(); it!=G.adj[tmp].end(); it++) {
if(!marked[*it]) {
bfsq.push(*it);
edgeTo[*it] = tmp;
marked[*it] = true;
}
}
}
}
bool BFSPaths::hasPathTo(int v) {
return marked[v];
}
vector<int> BFSPaths::pathTo(int v) {
vector<int> path;
if (!hasPathTo(v)) return path;
for (int i= v; i != s; i = edgeTo[i]) {
path.push_back(i);
}
path.push_back(s);
reverse(path.begin(), path.end());
return path;
}
/*END BFS */
/*BEGIN Topo sort*/
class DFOrder {
bool marked[1000];
public:
stack<int> reversePost;
DFOrder(DGraph G) {
while(!reversePost.empty()) {
reversePost.pop();
}
memset(marked, false, sizeof(marked));
for (int v = 0; v< G.getV(); v++)
if (!marked[v])
dfs(G,v);
};
void dfs(DGraph G, int v) {
marked[v] = true;
for (vector<int>::iterator it=G.adj[v].begin(); it != G.adj[v].end(); it++)
if (!marked[*it])
dfs(G, *it);
reversePost.push(v);
}
void printTO() {
while (!reversePost.empty()) {
int tmp = reversePost.top();
reversePost.pop();
cout << tmp << " ";
}
cout << endl;
}
};
/* END topo sort*/
/* BEGIN Connected component */
class CC {
bool marked[1000];
int id[1000];
int count;
public:
CC(DGraph G) {
memset(marked, false, sizeof(marked));
memset(id, 0, sizeof(id));
count =0;
DFOrder DFO(G.reverse());
while (!DFO.reversePost.empty()) {
int tmp = DFO.reversePost.top();
DFO.reversePost.pop();
if (!marked[tmp]) {
dfs(G,tmp);
count++;
}
}
};
void dfs(DGraph G, int v) {
marked[v] = true;
id[v]= count;
for (vector<int>::iterator it = G.adj[v].begin(); it != G.adj[v].end(); it++) {
if (!marked[*it]) {
dfs(G, *it);
}
}
};
bool isConnected(int v, int w) {
return id[v]==id[w];
};
};
/* END Connected component*/
int main() {
scanf_s("%d %d", &V, &E);
for (int i=0; i<E; i++) {
scanf_s("%d %d", &input[i][0], &input[i][1]);
}
DGraph G(V);
for (int i=0; i<E; i++) {
G.addEdge(input[i][0], input[i][1]);
}
G.printDG();
DFSPaths DFS(G, 0);
BFSPaths BFS(G,0);
vector<int> tmp = DFS.pathTo(2);
vector<int> tmp1 = BFS.pathTo(2);
cout << "DFS:" ;
for (vector<int>::iterator it = tmp.begin(); it!=tmp.end(); it++) {
cout << *it << " ";
}
cout << endl;
cout << "BFS:" ;
for (vector<int>::iterator it = tmp1.begin(); it!=tmp1.end(); it++) {
cout << *it << " ";
}
cout << endl;
cout << G.isCycle();
cout << endl;
//DFOrder DFO(G);
//DFO.printTO();
CC c(G);
cout << c.isConnected(0,1);
cout << endl;
//G.printUG();
return 0;
}