Repository navigation
Expand file tree
/
Copy pathstronglyConnectedComponents.cpp
More file actions
58 lines (56 loc) · 1.66 KB
/
Copy pathstronglyConnectedComponents.cpp
File metadata and controls
58 lines (56 loc) · 1.66 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
// Kosaraju's Algorithm
// do dfs and store nodes in a stack after returning from the call
// reverse adjacency list
// again do a dfs by using top of stack
// https://www.geeksforgeeks.org/problems/strongly-connected-components-kosarajus-algo/1
class Solution
{
private:
void dfs(int node, vector<vector<int>> adj, vector<int>& visited, stack<int>& st) {
visited[node] = 1;
for (auto it : adj[node]) {
if (visited[it] == 0) {
dfs(it, adj, visited, st);
}
}
st.push(node);
}
void depthFirstSearch(int node, vector<vector<int>>& adj, vector<int>& visited) {
visited[node] = 1;
for (auto it : adj[node]) {
if (visited[it] == 0) {
depthFirstSearch(it, adj, visited);
}
}
}
public:
//Function to find number of strongly connected components in the graph.
int kosaraju(int V, vector<vector<int>>& adj) {
stack<int> st;
vector<int> visited(V, 0);
for (int i = 0; i < V; i++) {
if (visited[i] == 0) {
dfs(i, adj, visited, st);
}
}
vector<vector<int>> adjR(V);
for (int i = 0; i < V; i++) {
visited[i] = 0;
for (auto &it : adj[i]) {
// i -> it
// it -> i
adjR[it].push_back(i);
}
}
int scc = 0;
while (!st.empty()) {
int node = st.top();
st.pop();
if (visited[node] == 0) {
scc++;
depthFirstSearch(node, adjR, visited);
}
}
return scc;
}
};