- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathgraphseries.cpp
591 lines (524 loc) · 11 KB
/
graphseries.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#include<bits/stdc++.h>
#definelllonglongint
usingnamespacestd;
boolcycleBFS(int node, vector<int> &vis, vector<int> arr[])
{
queue<pair<int, int>> q;
q.push({node, -1});
vis[node] = 1;
while (!q.empty())
{
int n = q.front().first;
int p = q.front().second;
q.pop();
for (auto i : arr[n])
{
if (vis[i] == 0)
{
vis[i] = 1;
q.push({i, n});
}
else
{
if (i != p)
returntrue;
}
}
}
returnfalse;
}
boolcycleDFS(int node, int parent, vector<int> &vis, vector<int> arr[])
{
vis[node] = 1;
for (auto i : arr[node])
{
if (!vis[i])
{
if (cycleDFS(i, node, vis, arr))
returntrue;
}
else
{
if (i != parent)
returntrue;
}
}
returnfalse;
}
voidbfs(int node, vector<int> &vis, vector<int> arr[], vector<int> &ans)
{
queue<int> q;
q.push(node);
vis[node] = 1;
while (!q.empty())
{
int f = q.front();
q.pop();
ans.push_back(f);
for (auto i : arr[f])
{
if (!vis[i])
{
vis[i] = 1;
q.push(i);
}
}
}
}
voiddfs(int node, vector<int> &vis, vector<int> arr[], vector<int> &ans)
{
vis[node] = 1;
ans.push_back(node);
for (auto i : arr[node])
{
if (!vis[i])
{
vis[i] = 1;
dfs(i, vis, arr, ans);
}
}
}
boolbipertiteBFS(int n, vector<int> &color, vector<int> arr[])
{
queue<int> q;
q.push(n);
color[n] = 1;
while (!q.empty())
{
int front = q.front();
q.pop();
for (auto i : arr[n])
{
if (color[i] == -1)
{
color[i] = 1 - color[n];
q.push(i);
}
else
{
if (color[i] == color[n])
returnfalse;
}
}
}
returntrue;
}
boolbipertiteDFS(int n, vector<int> &color, vector<int> arr[])
{
if (color[n] == -1)
color[n] = 1;
for (auto i : arr[n])
{
if (color[i] == -1)
{
color[i] = 1 - color[n];
if (!bipertiteDFS(i, color, arr))
returnfalse;
}
else
{
if (color[i] == color[n])
returnfalse;
}
}
returntrue;
}
boolcycleDirectedDFS(int n, vector<int> &vis, vector<int> &dfsvis, vector<int> arr[])
{
vis[n] = 1;
dfsvis[n] = 1;
for (auto i : arr[n])
{
if (vis[i] == 0)
{
if (cycleDirectedDFS(i, vis, dfsvis, arr))
returntrue;
}
else
{
if (dfsvis[i])
returntrue;
}
}
dfsvis[n] = 0;
returnfalse;
}
voidfindTopoSortDFS(int node, vector<int> arr[], stack<int> &s, vector<int> &vis)
{
vis[node] = 1;
for (auto i : arr[node])
{
if (!vis[i])
{
findTopoSortDFS(i, arr, s, vis);
}
}
s.push(node);
}
vector<int> topoSortDFS(vector<int> arr[], int n)
{
stack<int> s;
vector<int> vis(n, 0);
for (int i = 0; i < n; i++)
{
if (!vis[i])
{
findTopoSortDFS(i, arr, s, vis);
}
}
vector<int> ans;
while (!s.empty())
{
ans.push_back(s.top());
s.pop();
}
return ans;
}
// BFS (Kahn's Algorithm)
vector<int> topoSortBFS(vector<int> arr[], int n)
{
queue<int> q;
vector<int> inorder(n, 0);
for (int i = 0; i < n; i++)
{
for (auto it : arr[i])
{
inorder[it]++;
}
}
for (int i = 0; i < n; i++)
{
if (inorder[i] == 0)
q.push(i);
}
int count = 0; // this is used to find cycle in graph
vector<int> ans;
while (!q.empty())
{
int front = q.front();
q.pop();
count++;
ans.push_back(front);
for (auto i : arr[front])
{
inorder[i]--;
if (inorder[i] == 0)
q.push(i);
}
}
if (count == n)
{
// this means that our graph does not contain a cycle
cout << "Graph does not contain a cycle";
}
else
{
// this means that our graph contains a cycle
cout << "Graph contains a cycle";
}
return ans;
}
intshortestPathUndirectedBFS(vector<int> arr[], int n, int source, int destination)
{
queue<int> q;
vector<int> distance(n, INT_MAX);
distance[source] = 0;
q.push(source);
while (!q.empty())
{
int front = q.front();
q.pop();
for (auto i : arr[front])
{
distance[i] = min(distance[front] + 1, distance[i]);
q.push(i);
}
}
// now my distance array has shortest distance from source to every node.
return distance[destination];
}
// This function is customized for the "shortestPathDAG" fuction
voidfindTopoSortDFS2(int node, vector<pair<int, int>> arr[], stack<int> &s, vector<int> &vis)
{
vis[node] = 1;
for (auto i : arr[node])
{
if (!vis[i])
{
findTopoSortDFS2(i.first, arr, s, vis);
}
}
s.push(node);
}
// shortest path in weighted DAG(Directed Acyclic Graph)
voidshortestPathDAG(vector<pair<int, int>> arr[], int n, int source)
{
stack<int> s;
vector<int> vis(n, 0);
for (int i = 0; i < n; i++)
{
if (!vis[i])
{
findTopoSortDFS2(i, arr, s, vis);
}
}
vector<int> distance(n, INT_MAX);
distance[source] = 0;
while (!s.empty())
{
int temp = s.top();
s.pop();
if (distance[temp] != INT_MAX)
{
for (auto i : arr[temp])
{
distance[i.first] = min(distance[i.first], distance[temp] + i.second);
}
}
}
for (auto i : distance)
{
if (i == INT_MAX)
{
cout << "This node is not reachable";
}
else
cout << i << "";
}
}
//shortest path in weighted undirected graph
voiddijkstrasAlgorithm(vector<int,int>arr[],int n,int source){
vector<int>dis(n,INT_MAX);
vector<int>path(n);
//min priority queue
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>q;
dis[source]=0;
q.push({0,source});
while(!q.empty()){
pair<int,int>top=q.top();
int prev=top.second;
int weight=top.first;
q.pop();
for(auto i:arr[prev]){
if(dis[i.first] > dis[prev]+i.second){
dis[i]=dis[prev]+i.second;
path[i.first]=prev;// this line is optional and tells you the shortest path elements
q.push({dis[i.first],i.first});
}
}
}
for(int i:dis)cout<<i<<"";
}
//for minimum spanning tree --> BRUTE FORCE O(n^2);
voidprimsAlogrithm(vector<int,int>arr[],int n){
int key[n],parent[n];
bool mst[n];
for(int i=0;i<n;i++){
key[i]=INT_MAX;
parent[i]=-1;
mst[i]=false;
}
key[0]=0;
parent[0]=-1;
for(int count=0;count<n-1;count++){
int min=INT_MAX,prev;
//this loop finds the minimum value in key array i.e. edge with minimum weight
for(int i=0;i<n;i++){
if(mst[i]==false && key[i]<min){
min=key[i],prev=i;
}
}
//marks the minimum value true in mst array
mst[prev]=true;
//looping in the adjecent nodes of the perv node
for(auto i:arr[prev]){
int node=i.first;
int weight=i.second;
if(mst[node]==false && weight < key[node]){
parent[node]=prev;
key[node]=weight;
}
}
}
for(int i=0;i<n;i++){
cout<<parent[i]<<"--"<<i<<endl;
}
}
//for minimum spanning tree --> Efficent approch O((N+E)logn);
voidprimsAlgo(vector<int,int>arr[],int n){
int key[n],parent[n];
bool mst[n];
for(int i=0;i<n;i++){
key[i]=INT_MAX;
parent[i]=-1;
mst[i]=false;
}
//using min priority queue
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>q;
key[0]=0;
parent[0]=-1;
//in pair first value is weight and second value is node/index.
q.push({0,0});
for(int count=0;count<n-1;count++){
int prev=q.top().second;
q.pop();
//marks the minimum value true in mst array
mst[prev]=true;
//looping in the adjecent nodes of the perv node
for(auto i:arr[prev]){
int node=i.first;
int weight=i.second;
if(mst[node]==false && weight < key[node]){
parent[node]=prev;
key[node]=weight;
q.push({weight,node});
}
}
}
for(int i=0;i<n;i++){
cout<<parent[i]<<"--"<<i<<endl;
}
}
//structure for kruskals algorithm and bellmenford algorithm
structnode{
int u;//first node
int v;// second node
int wt;// weight between the nodes
node(int first,int second,int weight){
u=first;
v=second;
wt=weight;
}
};
//comparator for sorting vector of edges structure
boolcomp(node a,node b){
return a.wt<b.wt;
}
//function for union of two nodes in the set kruskalsalgo
voidunionn(int u,int v,vector<int>&parent,vector<int>&rank){
u=findpair(u,parent);
v=findpair(v,parent);
if(rank[u]<rank[v]){
parent[u]=v;
}elseif(rank[v]<rank[u]){
parent[v]=u;
}else{
parent[v]=u;
rank[u]++;
}
}
//function to find the parent of the node in the set kruskalsalgo
intfindpair(int n,vector<int>&parent){
if(parent[n]==n)return n;
return parent[n]= findpair(parent[n],parent);
}
voidkruskalsAlgo(){
int n,m;
cin>>n>>m;
vector<node>edges;
for(int i=0;i<m;i++){
int u,v,wt;
cin>>u>>v>>wt;
edges.push_back(node(u,v,wt));
}
sort(edges.begin(),edges.end(),comp);
vector<int>parent(n);
vector<int>rank(n,0);
for(int i=0;i<n;i++){
parent[i]=i;
}
int cost=0;
vector<pair<int,int>>mst;//minimum spanning tree
for(auto it : edges){
if(findpair(it.u,parent) !=findpair(it.v,parent)){
cost+=it.wt;
mst.push_back({it.u,it.v});
unionn(it.u,it.v,parent,rank);
}
}
cout<<cost<<endl;
}
voidbridgeInGraph(int node,int parent,int& timer,vector<int>&vis,vector<int>&low,vector<int>&in,vector<int>arr[]){
vis[node]=1;
low[node]=in[node]=timer;
timer++;
for(auto it : arr[node]){
if(it==parent)continue;
if(vis[it]==1){
low[node]=min(low[node],in[it]);
}else{
bridgeInGraph(it,node,timer,vis,low,in,arr);
//after dfs call we backtrack
//if in[node] value is smaller than low[it] then it is an bridge edge.
//how? because if in[node] is smaller than it means that "it" is not
//connect to any ansistor and does not have any other path to reach.
//if if[node] is greater that means "it" is connect to the ansistor so it has more paths to reach it.
if(in[node]<low[it]){
cout<<node<<" - "<<it<<" is a bridge";
}
}
}
}
//shortest path in a graph with -ve weights
voidbellmenFord(int source){
int n,m;
cin>>n>>m;
vector<node>edges;
for(int i=0;i<m;i++){
int x,y,w;
cin>>x>>y>>w;
edges.push_back(node(x,y,w));
}
vector<int>distance(n,INT_MAX);
distance[source]=0;
//relaxing N-1 times every edge
for(int i=0;i<=n-1;i++){
for(auto it:edges){
if(distance[it.v]>distance[it.u]+it.w){
distance[it.v]=distance[it.u]+it.w;
}
}
}
int flag=0;
//deceting negative cycle by relaxing one more time i.e Nth time
for(auto it:edges){
if(distance[it.v]>distance[it.u]+it.w){
cout<<"Negative cycle";
flag=1;
break;
}
}
if(flag==0){
for(int i=0;i<n;i++){
cout<<distance[i]<<"";
}
}
}
intmain()
{
int n, m;
cin >> n >> m;
vector<int> arr[n + 1];
for (int i = 0; i < m; i++)
{
int x, y;
cin >> x >> y;
arr[x].push_back(y);
arr[y].push_back(x);
}
vector<int> ans;
int color[n + 1]; // for bipartite graph
memset(color, -1, sizeof(color)); // for bipartite graph
vector<int> vis(n + 1, 0);
for (int i = 1; i <= n; i++)
{
if (!vis[i])
{
if (cycleDFS(i, -1, vis, arr))
cout << true;
}
}
for (auto i : ans)
cout << i << "";
return0;
}