-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCycle_detection_graphs_with_back_edges.cpp
More file actions
77 lines (59 loc) · 1.2 KB
/
Copy pathCycle_detection_graphs_with_back_edges.cpp
File metadata and controls
77 lines (59 loc) · 1.2 KB
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
#include<bits/stdc++.h>
using namespace std;
// Back-Edge : An edge that points from one node to its ancestors
// It is directly related to Cycle (back edge points to already visited node)
// Back-edge means there is a Cyle in graph
// Dfs-tree is just a tree formed after removing back-edges
const int N = 2e5 + 11;
vector<vector<int>> adj(N);
vector<int> vis(N);
vector<int> parent(N);
void dfs(int node, int par)
{
parent[node] = par;
vis[node] = 1;
// it means node is visited and is in call stack
for (auto nbr : adj[node])
{
if (vis[nbr] == 0)
{
dfs(nbr, node);
}
// nbr is not parent and it is in call stack
// means nbr is ancestor so back-edge
else if (nbr != par && vis[nbr] == 1)
{
int ancester = nbr;
cout << "Back_edge : cycle is found" << "\n";
// Priting Cycle
while (node != ancester)
{
cout << node << "-->";
node = parent[node];
}
return;
}
}
vis[node] = 2;
// node is visited and is not in call stack
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 0; i < m; ++i)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; ++i)
{
if (!vis[i])
{
dfs(i, 0);
}
}
return 0;
}