-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathStrongly_Connected_Components.cpp
More file actions
265 lines (234 loc) · 6.17 KB
/
Copy pathStrongly_Connected_Components.cpp
File metadata and controls
265 lines (234 loc) · 6.17 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
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
/* Objective of the program:
A directed graph is strongly connected if there is a path between all pairs of vertices. A
strongly connected component (SCC) of a directed graph is a maximal strongly connected
subgraph.Given a directed graph, write a program that prints the components and their associated vertices.
Also, write a program that prints the components of the component graph in a topologically sorted order.
*/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct info
{
int vertex;
int time;
};
void DFS_visit(vector<int> v[], int n, int start, int visited[], int entry[], int exit[], int p[])
{
static int time = 0;
time++;
entry[start] = time;
for(int i=0; i<v[start].size(); i++)
{
int temp = v[start][i];
if(visited[temp] == 0)
{
visited[temp] = 1;
p[temp] = start;
DFS_visit(v, n, temp, visited, entry, exit, p);
}
}
time++;
exit[start] = time;
}
bool compare(struct info a, struct info b)
{
if(a.time > b.time)
return true;
return false;
}
void insert(vector<int> adj[], int a, int b)
{
// Since the graph given is directed and unweighted
// The edges are uni-directional in nature and therefore
// in the adjacency list, the vertex are being added to parent set
adj[a].push_back(b);
}
void pgraph(vector<int> v[], int n)
{
// This is a check on how the graph looks like via adjacency list representation
for(int i=0; i<n; i++)
{
cout << i + 1 << " ->";
for(int j=0; j<v[i].size(); j++)
{
cout << v[i][j] + 1 << " ->";
}
cout << "NULL" << endl;
}
}
void final_DFS_visit(vector<int> v[], int n, int start, int visited[], int mark[], int count)
{
for(int i=0; i<v[start].size(); i++)
{
int temp = v[start][i];
if(visited[temp] == 0)
{
visited[temp] = 1;
mark[temp] = count;
cout << temp + 1 << " ";
final_DFS_visit(v, n, temp, visited, mark, count);
}
}
}
void ultimate_DFS(vector<int> v[], int n, int mark[], int original)
{
// Topological Sort on Final Graph
int parent[n];
int visited[n] = {0};
int entry[n] = {-1};
int exit[n] = {-1};
for(int i=0; i<n; i++)
{
if(visited[i] == 0)
{
parent[i] = -1;
visited[i] = 1;
DFS_visit(v, n, i, visited, entry, exit, parent);
}
}
// Exit array has the exit times
struct info a[n];
for(int i=0; i<n; i++)
{
a[i].vertex = i;
a[i].time = exit[i];
}
sort(a, a+n, compare);
cout << endl << "Topological Sort: " << endl;
cout << "(";
for(int i=0; i<n; i++)
{
int node_mark = a[i].vertex;
cout << "( ";
for(int j=0; j<original; j++)
{
if(mark[j] == node_mark + 1)
{
cout << j + 1 << " ";
}
}
if(i != n-1)
cout << "), " ;
else
cout << ")" ;
}
cout << ")" << endl;
}
void final_DFS(vector<int> v[], struct info d[], int n, vector<int> real[])
{
int mark[n];
int count = 1;
int visited[n] = {0};
for(int i=0; i<n; i++)
{
int start = d[i].vertex;
if(visited[start] == 0)
{
cout << "Component " << count << ": " << start + 1 << " ";
visited[start] = 1;
mark[start] = count;
final_DFS_visit(v, n, start, visited, mark, count);
count++;
cout << endl;
}
}
int total = count - 1;
// We have to do topological sorting on SCC's
int x[total][total] = {0};
for(int i=0; i<n; i++)
{
for(int j=0; j<real[i].size(); j++)
{
int s = i;
int d = real[i][j];
if(mark[s] == mark[d])
continue;
else
{
int t1 = mark[s] - 1;
int t2 = mark[d] - 1;
x[t1][t2] = 1;
}
}
}
vector<int> final_graph[total];
// Print the final graph
for(int i=0; i<total; i++)
for(int j=0; j<total; j++)
if(x[i][j] == 1)
final_graph[i].push_back(j);
ultimate_DFS(final_graph, total, mark, n);
}
void final(vector<int> v[], struct info d[], int n, vector<int> real[])
{
final_DFS(v, d, n, real);
}
void DFS(vector<int> v[], int n)
{
int parent[n];
int visited[n] = {0};
int entry[n] = {-1};
int exit[n] = {-1};
for(int i=0; i<n; i++)
{
if(visited[i] == 0)
{
parent[i] = -1;
visited[i] = 1;
DFS_visit(v, n, i, visited, entry, exit, parent);
}
}
// Exit array has the exit times
struct info d[n];
for(int i=0; i<n; i++)
{
d[i].vertex = i;
d[i].time = exit[i];
}
sort(d, d+n, compare);
// Let us do transpose of the graph
vector<int> v_transpose[n];
for(int i=0; i<n; i++)
{
for(int j=0; j<v[i].size(); j++)
{
int source = i;
int destination = v[i][j];
v_transpose[destination].push_back(source);
}
}
// Run DFS on transposed graph in the order of topological sorting
final(v_transpose, d, n, v);
}
int main()
{
// Instructions: -
/* a) Vertices are 1 - based. That is if n = 5, then allowed vertices are v[] = {1, 2, 3, 4, 5}
b) You can add infinitely many edges in the form 0 1 <enter> 1 2 <enter>
c) End of edges input will be considered only when -1 <enter> is inputted in terminal by user.
d) The following program finds Strongly Connected Components and also does the Topological Sorting of Graph.
*/
int n;
cout << "Enter the number of vertices: ";
cin >> n;
vector<int> v[n];
// adj is the adjacency list
int a, b, count = 0;
cout << "Enter the Directed Edges: " << endl;
while(1)
{
cin >> a;
if(a == -1)
break;
else
{
cin >> b;
a--; b--;
insert(v, a, b);
count++;
}
}
cout << endl;
DFS(v, n);
}