-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCut-Bridge_or_Critical-Point_using_Tarjans_Algo.cpp
63 lines (53 loc) · 1.41 KB
/
Cut-Bridge_or_Critical-Point_using_Tarjans_Algo.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
#include <bits/stdc++.h>
using namespace std;
class Graph
{
int V;
vector<int> *graph;
public:
Graph(int V)
{
this->V = V;
graph = new vector<int>[V];
}
void addEdge(int u, int v)
{
graph[u].push_back(v);
}
void Cut_Bridge_Algorithm(int source, vector<int> &discovery, vector<int> &low, vector<int> &parent)
{
static int time = 0;
discovery[source] = low[source] = ++time;
for (auto v : graph[source])
{
if (discovery[v] == -1)
{
parent[v] = source;
Cut_Bridge_Algorithm(v, discovery, low, parent);
low[source] = min(low[source], low[v]);
}
// checking the CRITICAL POINT during backtracking.
if (low[v] > discovery[source])
cout << "The Critical Section or Cut Bridge is : " << source << " to " << v << endl;
else if (v != parent[source])
low[source] = min(low[source], discovery[v]);
}
}
};
int main()
{
int V = 5;
Graph g(V);
g.addEdge(1, 0);
g.addEdge(0, 2);
g.addEdge(2, 1);
g.addEdge(0, 3);
g.addEdge(3, 4);
vector<int> discovery(V, -1);
vector<int> low(V, -1);
vector<int> parent(V, -1);
for (int i = 0; i < V; i++)
if (discovery[i] == -1)
g.Cut_Bridge_Algorithm(i, discovery, low, parent);
return 0;
}