-
Notifications
You must be signed in to change notification settings - Fork 37
/
bridgesInGraph.cpp
83 lines (65 loc) · 1.64 KB
/
bridgesInGraph.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
// CPP Program to find Bridges in a Graph
#include <bits/stdc++.h>
using namespace std;
vector<int> adj[100001];
int vis[100001];
int in[100001];
int low[100001];
int timer = 0;
vector<vector<int>> ans;
void dfs(int node, int par)
{
vis[node] = 1;
in[node] = low[node] = timer;
timer++;
for (int child : adj[node])
{
if (child == par)
continue;
if (vis[child] == 1)
{
// back edge
low[node] = min(in[child], low[node]);
}
else
{
dfs(child, node);
if (low[child] > in[node])
{
vector<int> temp;
temp.push_back(node);
temp.push_back(child);
ans.push_back(temp);
}
// forward edge
low[node] = min(low[child], low[node]);
}
}
}
vector<vector<int>> bridges(int n, vector<vector<int>> &conn)
{
if (n == 2)
{
vector<int> temp = {conn[0][0], conn[0][1]};
ans.push_back(temp);
return ans;
}
for (int i = 0; i < conn.size(); i++)
{
adj[conn[i][0]].push_back(conn[i][1]);
adj[conn[i][1]].push_back(conn[i][0]);
}
dfs(0, -1);
return ans;
}
int main()
{
int nodes = 4;
vector<vector<int>> connections{{0,1}, {1,2}, {2,0}, {1,3}};
vector<vector<int>> result = bridges(nodes, connections);
cout << "Bridges Connections in the Graph are: ";
for(int i=0;i<result.size();i++)
{
cout << result[i][0] << " " << result[i][1] << endl;
}
}