-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph_Bridge_Edge.cpp
111 lines (102 loc) · 2.9 KB
/
Graph_Bridge_Edge.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
#include <bits/stdc++.h>
using namespace std;
#define V 13
vector<vector<int> > adjacent_list(V);
vector<bool> notVisited(V, true);
vector<pair<int, int> > edge;
void BFS(int n, int x, int y)
{
queue<int> q;
q.push(n);
while (q.size())
{
int u = q.front();
q.pop();
for (auto v : adjacent_list[u])
{
if ((u == x && v == y) || (u == y && v == x))
continue;
if (notVisited[v])
{
notVisited[v] = false;
q.push(v);
}
}
}
}
int main()
{
int graph[V][V] = {{0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0}};
int countEdge{0}, stp{0}, countStp{0};
pair<int,int> eind;
for(int i=0; i<V; i++)
{
for(int j=0; j<V; j++)
{
if(graph[i][j])
{
if(i < j)
{
countEdge++;
eind.first = i;
eind.second = j;
edge.push_back(eind);
}
adjacent_list[i].push_back(j);
}
}
}
sort(edge.begin(), edge.end());
// for(int i = 0; i < adjacent_list.size(); i++)
// {
// for(int j = 0; j < adjacent_list[i].size(); j++)
// {
// cout << adjacent_list[i][j] << " ";
// }
// cout << endl;
// }
// for(int i=0; i < edge.size(); i++)
// {
// cout << edge[i].first << " " << edge[i].second << endl;
// }
for(int i=0; i < V; i++)
{
if(notVisited[i])
{
stp++;
notVisited[i] = false;
BFS(i, -1, -1);
}
}
for(int i = 0; i < edge.size(); i++)
{
notVisited.clear();
notVisited.resize(V+1, true);
countStp = 0;
for(int j = 0; j < V; j++)
{
if(notVisited[j])
{
countStp++;
notVisited[j] = false;
BFS(j, edge[i].first, edge[i].second);
}
}
if( countStp > stp)
{
cout << edge[i].first << " " << edge[i].second << endl;
}
}
}