-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKosarajuWithoutClasses.cpp
201 lines (169 loc) · 4.41 KB
/
KosarajuWithoutClasses.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
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
// Kosaraju's Algo
#include <iostream>
#include <deque>
#include <array>
#include <vector>
using namespace std;
const int MAX_VERTICES = 20;
const bool DEBUG = true;
#define labelize(v) ((char)(v +'a'))
void debug_print(vector <int> &something)
{
if (not DEBUG) return;
for (auto i: something) cout << char(i + 'a')<< ' ';
cout << endl;
}
void debug_print(deque <int> &something, bool with_labels = true)
{
if (not DEBUG) return;
if (with_labels)
for (auto i: something) cout << labelize(i) << ' ';
else
for (auto i: something) cout << i << ' ';
cout << endl;
}
void fill_zeros(int nfverts, bool mat[][MAX_VERTICES])
{
int i, j;
for (i = 0; i < nfverts; ++i)
{
for (j = 0; j < nfverts; ++j)
{
mat[i][j] = 0;
}
}
}
void transpose(int nfverts, bool mat[][MAX_VERTICES])
{
int i, j;
for (i = 0; i < nfverts; ++i)
{
for (j = i + 1; j < nfverts; ++j)
{
swap(mat[i][j], mat[j][i]);
}
}
}
void read_matrix(int &nfverts, bool mat[][MAX_VERTICES])
{
int i, j, nfedges;
char u_label, v_label;
int u, v;
cin >> nfverts;
cin >> nfedges;
fill_zeros(nfverts, mat);
for (i = 0; i < nfedges; ++i)
{
cin >> u_label >> v_label;
u = u_label - 'a';
v = v_label - 'a';
mat[u][v] = 1;
}
}
void print_matrix(int rows, bool mat[][MAX_VERTICES])
{
int i, j;
for (i = 0; i < rows; ++i, cout << '\n')
for (j = 0; j < rows; ++j)
cout << mat[i][j] << ' ';
cout << endl;
}
void recursive_forward_dfs(int nfverts, bool mat[][MAX_VERTICES], int curvx,\
deque <int> &stack_verts, vector <bool> &visited)
{
visited[curvx] = true;
//cout << char(curvx + 'a') << ' ';
for (int v = 0; v < nfverts; ++v)
{
if (mat[curvx][v] and not visited[v])
{
recursive_forward_dfs(nfverts, mat, v, stack_verts, visited);
}
}
stack_verts.push_back(curvx);
//debug_print(stack_verts);
}
void forward_dfs(int nfverts, bool mat[][MAX_VERTICES], \
deque <int> &stack_verts)
{
vector <bool> visited(nfverts);
//cout << "Inside forward_dfs" <<endl;
for (int v = 0; v < nfverts; ++v)
{
if (visited[v]) continue;
else recursive_forward_dfs(nfverts, mat, v, stack_verts, visited);
// cout << endl;
//for (auto i: visited) cout << i; cout << endl;
}
//debug_print(stack_verts);
}
void recursive_reverse_dfs(int nfverts, bool mat[][MAX_VERTICES], int curvx,\
deque <int> &stack_verts, vector <bool> &visited,\
vector <int> &components)
{
visited[curvx] = true;
components.push_back(curvx);
//debug_print(components);
//cout << "Just pushed to components " << char(curvx + 'a') << endl;
for (int v = 0; v < nfverts; ++v)
{
if (mat[curvx][v] and not visited[v])
{
recursive_reverse_dfs(nfverts, mat, v, stack_verts, visited, components);
}
}
}
void reverse_dfs(int nfverts, bool mat[][MAX_VERTICES], \
deque <int> &stack_verts, vector <int> &components, \
deque <int> &tails_for_components)
{
vector <bool> visited(nfverts);
int v;
//cout << "Inside reverse dfs" <<endl;
while (not stack_verts.empty())
{
v = stack_verts.back();
stack_verts.pop_back();
//cout << "Just popped from stack_verts " << labelize(v) << endl;
if (visited[v]) continue;
recursive_reverse_dfs(nfverts, mat, v, stack_verts, visited, components);
tails_for_components.push_back(components.size() - 1);
// cout << endl;
//for (auto i: visited) cout << i; cout << endl;
}
//debug_print(stack_verts);
}
void print_strongly_connected_components\
(vector <int> &components, deque <int> &tails_for_components)
{
int i = 0;
auto terminator = tails_for_components.begin();
for (auto vx: components)
{
cout << labelize(vx) << ' ';
if (i == *terminator)
{
cout << '\n';
++terminator;
}
++i;
}
}
int main()
{
bool mat[MAX_VERTICES][MAX_VERTICES];
int nfverts = MAX_VERTICES;
read_matrix(nfverts, mat);
deque <int> stack_verts;
forward_dfs(nfverts, mat, stack_verts);
transpose(nfverts, mat);
vector <int> components;
components.reserve(nfverts);
// Each ele of this is the index of the last vertex of a component in `components`
deque <int> tails_for_components;
reverse_dfs(nfverts, mat, stack_verts, components, tails_for_components);
print_strongly_connected_components(components, tails_for_components);
/*
debug_print(components);
debug_print(tails_for_components);*/
}