-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnected Components.cpp
More file actions
211 lines (187 loc) · 2.96 KB
/
Connected Components.cpp
File metadata and controls
211 lines (187 loc) · 2.96 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
/*
Connected Components
{Kosaraju's Two Pass Algorithm} (for SCCs)
Time Complexity: O(N+M)
*/
#include <bits/stdc++.h>
using namespace std;
class Graph
{
private:
int V;
int E;
char type;
vector <int> *adj_list;
vector <int> *rev_adj_list;
bool *visited_BFS;
queue <int>Q;
int WCC;
bool *visited_DFS;
stack <int>S;
int *rev_sink_order;
int order;
int SCC;
public:
Graph(int V, char type)
{
this->V = V;
this->E = 0;
this->type = type;
this->adj_list = new vector<int>[V];
this->rev_adj_list = new vector<int>[V];
this->visited_BFS = new bool[V];
for(int v = 0; v<V; v++)
{
visited_BFS[v] = false;
}
this->WCC = 0;
this->visited_DFS = new bool[V];
for(int v = 0; v<V; v++)
{
visited_DFS[v] = false;
}
this->rev_sink_order = new int[V];
this->order = 0;
this->SCC = 0;
}
void add_edge(int u, int v)
{
adj_list[u].push_back(v);
rev_adj_list[v].push_back(u);
E++;
}
void end_graph()
{
if(type == 'u')
{
for(int i = 0; i<V; i++)
{
for(int j = 0; j<adj_list[i].size(); j++)
{
this->add_edge(adj_list[i][j], i);
}
}
}
if(type == 'd')
{
}
}
void reset_BFS()
{
for(int v = 0; v<V; v++)
{
visited_BFS[v] = false;
}
this->WCC = 0;
}
void BFS(int s)
{
Q.push(s);
while(!Q.empty())
{
int temp = Q.front();
Q.pop();
if(visited_BFS[temp] == false)
{
visited_BFS[temp] = true;
for(int n = 0; n<adj_list[temp].size(); n++)
{
Q.push(adj_list[temp][n]);
}
}
}
}
int calculate_WCC()
{
reset_BFS();
for(int i = 0; i<V; i++)
{
if(visited_BFS[i] == false)
{
BFS(i);
WCC++;
}
}
return WCC;
}
void reset_DFS()
{
for(int v = 0; v<V; v++)
{
visited_DFS[v] = false;
}
}
void DFS(int s, bool rev = false)
{
S.push(s);
while(!S.empty())
{
int temp = S.top();
S.pop();
if(visited_DFS[temp] == false)
{
visited_DFS[temp] = true;
if(rev)
{
for(int n = 0; n<rev_adj_list[temp].size(); n++)
{
DFS(rev_adj_list[temp][n], true);
}
rev_sink_order[order] = s;
order++;
}
else
{
for(int n = 0; n<adj_list[temp].size(); n++)
{
DFS(adj_list[temp][n], false);
}
}
}
}
}
int calculate_SCC()
{
if(this->type == 'u')
{
return -1;
}
else
{
reset_DFS();
for(int i = 0; i<V; i++)
{
if(visited_DFS[i] == false)
{
DFS(i, true);
}
}
reset_DFS();
for(int i = V-1; i>=0; i--)
{
if(visited_DFS[rev_sink_order[i]] == false)
{
DFS(rev_sink_order[i], false);
SCC++;
}
}
return SCC;
}
}
};
int main()
{
int V;
char type;
cin>>V>>type;
Graph g(V, type);
int u,v;
while(cin>>u>>v)
{
g.add_edge(u,v);
}
g.end_graph();
cout<<g.calculate_WCC()<<endl;
cout<<g.calculate_SCC()<<endl;
return 0;
}