-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph.cpp
467 lines (440 loc) · 12.6 KB
/
Graph.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const int MAXN = 1e5, MOD = 1e9 + 7;
// Edges starts from 0 to N-1
class Graph
{
int numOfVertex;
vector<int> adjlist[MAXN];
// for BFS
int distance[MAXN]; // distance from the source
int visited[MAXN]; // 0=>never visited 1=>visiting 2=>done visited
int previos[MAXN]; // Stores the immediate parent/previous y
// for dfs
int discoverytime[MAXN];
int finishtime[MAXN];
// for topological sort
int indeg[MAXN];
ll time;
ll maxD, maxNode;
public:
Graph(int n_vertex)
{
memset(indeg, 0x00, MAXN);
this->numOfVertex = n_vertex;
}
void add_new_edge(int sourcenode, int destinationnode)
{
adjlist[sourcenode].push_back(destinationnode);
indeg[destinationnode]++;
// adjlist[destinationnode].push_back(sourcenode); // comment it out if it is a directed graph
// indeg[sourcenode]++; //toplogical sort
}
bool detect_cycle_one_vertex(int currentnode, bool visited[], int parentnode)
{
visited[currentnode] = true;
vector<int>::iterator itr;
for (itr = adjlist[currentnode].begin(); itr != adjlist[currentnode].end(); itr++)
{
if (!visited[*itr])
{
if (detect_cycle_one_vertex(*itr, visited, currentnode))
return true;
}
else if (*itr != parentnode)
return true;
}
return false;
}
bool detect_cycle_in_full_graph()
{
bool *visited = new bool[numOfVertex];
for (int i = 0; i < numOfVertex; i++)
{
visited[i] = false;
}
for (int currentnode = 0; currentnode < numOfVertex; currentnode++)
if (!visited[currentnode] && detect_cycle_one_vertex(currentnode, visited, -1))
return true;
return false;
}
bool isCycle()
{
vector<int> in_degree(numOfVertex, 0);
for (int u = 0; u < numOfVertex; u++)
{
for (auto v : adjlist[u])
in_degree[v]++;
}
queue<int> container_queue;
for (int i = 0; i < numOfVertex; i++)
if (in_degree[i] == 0)
container_queue.push(i);
int cnt = 1;
vector<int> top_order;
while (!container_queue.empty())
{
int u = container_queue.front();
container_queue.pop();
top_order.push_back(u);
vector<int>::iterator itr;
for (itr = adjlist[u].begin(); itr != adjlist[u].end(); itr++)
if (--in_degree[*itr] == 0)
{
container_queue.push(*itr);
cnt++;
}
}
if (cnt != numOfVertex)
return true;
else
return false;
}
void dfs()
{
for (int i = 0; i < numOfVertex; i++)
{
visited[i] = 0;
previos[i] = -1;
finishtime[i] = INT_MIN;
discoverytime[i] = INT_MIN;
}
time = 0;
int numberofconnectedcomponent = 0;
for (int i = 1; i <= numOfVertex; ++i)
{
if (visited[i] == 0)
{
numberofconnectedcomponent++;
cout << i << endl;
dfs_visit(i);
}
}
cout << numberofconnectedcomponent << endl;
}
void dfs_visit(int i)
{
time = time + 1;
discoverytime[i] = time;
visited[i] = 1;
vector<int>::iterator it;
for (it = adjlist[i].begin(); it != adjlist[i].end(); it++)
{
if (visited[*it] == 0)
{
previos[*it] = i;
dfs_visit(*it);
}
visited[i] = 2;
time = time + 1;
finishtime[i] = time;
}
}
void bfs(int x)
{ // Here x is the root vertex
for (int i = 0; i < MAXN; i++)
{
visited[i] = 0; // 2 means black//1 means grey //0 means white
previos[i] = INT_MIN;
distance[i] = INT_MIN;
}
queue<int> container_queue;
container_queue.push(x);
visited[x] = 1;
previos[x] = -1;
distance[x] = 0;
while (!container_queue.empty())
{
int y = container_queue.front();
container_queue.pop();
// cout << y << endl;
vector<int>::iterator it;
for (it = adjlist[y].begin(); it != adjlist[y].end(); it++)
{
if (!visited[*it])
{
visited[*it] = 1;
previos[*it] = y;
distance[*it] = distance[y] + 1;
container_queue.push(*it);
}
visited[y] = 2;
}
}
}
void printadjlist()
{
for (int i = 1; i < numOfVertex + 1; i++)
{
cout << i << " -> ";
for (int x : adjlist[i])
{
cout << x << " ";
}
cout << endl;
}
}
void printAllPaths(int s, int d)
{
// Create an array to store paths
int *path = new int[numOfVertex];
int path_index = 0; // Initialize path[] as empty
// Initialize all vertices as not visited
for (int i = 0; i < numOfVertex; i++)
visited[i] = 0;
// Call the recursive helper function to print all paths
printAllPathsUtil(s, d, path, path_index);
}
void printAllPathsUtil(int u, int d, int path[], int &path_index)
{
// Mark the current y and store it in path[]
visited[u] = 1;
path[path_index] = u;
path_index++;
// If current vertex is same as destination, then print
// current path[]
if (u == d)
{
for (int i = 0; i < path_index; i++)
cout << path[i] << " ";
cout << endl;
}
else // If current vertex is not destination
{
// Recur for all the vertices adjacent to current vertex
vector<int>::iterator i;
for (i = adjlist[u].begin(); i != adjlist[u].end(); ++i)
if (!visited[*i])
printAllPathsUtil(*i, d, path, path_index);
}
// Remove current vertex from path[] and mark it as unvisited
path_index--;
visited[u] = false;
}
void isBt(int x)
{
visited[x] = 1;
distance[x] = 0;
queue<int> container_queue;
container_queue.push(x);
while (!container_queue.empty())
{
int y = container_queue.front();
container_queue.pop();
vector<int>::iterator it;
for (it = adjlist[y].begin(); it != adjlist[y].end(); it++)
{
if (visited[*it] == -1)
{
visited[*it] = 1;
distance[*it] = !distance[y];
container_queue.push(*it);
}
}
}
}
bool isBipartite()
{
for (int i = 0; i < numOfVertex; ++i)
{
visited[i] = -1;
distance[i] = -1;
}
for (int i = 0; i < numOfVertex; i++)
if (visited[i] == -1)
isBt(i);
for (int i = 0; i < numOfVertex; i++)
{
for (auto it : adjlist[i])
{
if (distance[i] == distance[it])
return false;
}
}
return true;
}
void PrintShortestPathBFS(int source, int destination)
{
bfs(source);
if (distance[destination] == INT_MIN)
cout << "No Path Exists" << endl;
else
{
stack<int> ans;
int iterator = destination;
while (previos[iterator] != -1)
{
ans.push(iterator);
iterator = previos[iterator];
}
ans.push(iterator);
while (!ans.empty())
{
cout << ans.top() << " ";
ans.pop();
}
cout << endl;
}
}
void modifiedbfs(int x)
{ // Here x is the root vertex
for (int i = 0; i < MAXN; i++)
{
visited[i] = 0; // 2 means black//1 means grey //0 means white
previos[i] = INT_MIN;
distance[i] = INT_MIN;
}
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> container_queue;
container_queue.push({0, x});
visited[x] = 1;
previos[x] = -1;
distance[x] = 0;
while (!container_queue.empty())
{
int node = container_queue.top().second;
int dis = container_queue.top().first;
container_queue.pop();
for (auto it = adjlist[node].begin(); it != adjlist[node].end(); it++)
{
if (!visited[*it])
{
visited[*it] = 1;
previos[*it] = node;
distance[*it] = distance[node] + 1;
container_queue.push({dis + 1, *it});
}
visited[node] = 2;
}
}
}
void printlexicographicallyshortestpath(int source, int destination)
{
modifiedbfs(source);
if (distance[destination] != INT_MIN)
{
stack<int> s1;
int v = destination;
s1.push(v);
while (previos[v] != -1)
{
s1.push(previos[v]);
v = previos[v];
}
while (!s1.empty())
{
cout << s1.top();
s1.pop();
if (!s1.empty())
cout << " ";
}
cout << endl;
}
else
{
cout << "Not Reachable" << endl;
}
}
void topologicalsort()
{
queue<int> q;
int tempindeg[MAXN];
for (int i = 0; i < numOfVertex; i++)
{
if (indeg[i] == 0)
q.push(i);
}
for (int i = 0; i < numOfVertex; i++)
{
tempindeg[i] = indeg[i];
}
while (!q.empty())
{
int temp = q.front();
q.pop();
cout << temp << " ";
for (auto it : adjlist[temp])
{
tempindeg[it]--;
if (tempindeg[it] == 0)
q.push(it);
}
}
cout << endl;
}
vector<vector<int>> toadjacencylist()
{
// Initialize a matrix
vector<vector<int>> matrix(numOfVertex, vector<int>(numOfVertex, 0));
for (int i = 0; i < numOfVertex; i++)
{
for (auto j : adjlist[i])
matrix[i][j] = 1;
}
return matrix;
}
void printadjmat()
{
vector<vector<int>> adjmat = toadjacencylist();
for (int i = 0; i < adjmat.size(); i++)
{
for (int j = 0; j < adjmat.size(); j++)
{
cout << adjmat[i][j] << " ";
}
cout << endl;
}
}
int countTriangle(vector<vector<int>> graph, bool isDirected)
{
int count_Triangle = 0;
// Consider every possible
// triplet of edges in graph
for (int i = 0; i < numOfVertex; i++)
{
for (int j = 0; j < numOfVertex; j++)
{
for (int k = 0; k < numOfVertex; k++)
{
// Check the triplet if
// it satisfies the condition
if (graph[i][j] && graph[j][k] && graph[k][i])
count_Triangle++;
}
}
}
// If graph is directed ,
// division is done by 3,
// else division by 6 is done
isDirected? count_Triangle /= 3 :
count_Triangle /= 6;
return count_Triangle;
}
void lengthcalc(int node, int distance)
{
visited[node]= 1;
if(distance>maxD) {
maxD =distance, maxNode=node;
cout<<maxD<<" "<<maxNode<<endl;
}
for (auto child:adjlist[node])
{
if(visited[child]==0) lengthcalc(child,distance+1);
}
}
int maximumpathlength(){
for(int i=0; i<numOfVertex;i++){
visited[i]=0;
}
maxD=INT_MIN;
for(int i=0; i<numOfVertex; i++){
lengthcalc(i,0);
lengthcalc(maxNode,0);
}
return maxD;
}
};
int main()
{
return 0;
}