Repository navigation
Expand file tree
/
Copy pathstrongly_connected_component.cpp
More file actions
99 lines (86 loc) · 1.81 KB
/
Copy pathstrongly_connected_component.cpp
File metadata and controls
99 lines (86 loc) · 1.81 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
#include <iostream>
#include <vector>
#include <stack>
#include <functional>
using namespace std;
class Graph {
public:
vector<vector<int>> adj;
bool isDirected;
int V;
Graph(int v, bool type = true): V(v), isDirected(type) {
adj.resize(v);
}
void addEdge(int u, int v) {
adj[u].push_back(v);
if (!isDirected) {
adj[v].push_back(u);
}
}
Graph transpose() const {
Graph other(this->V, this->isDirected);
for (int u = 0; u < this->V; u++) {
for (int v: this->adj[u]) {
other.adj[v].push_back(u);
}
}
return other;
}
};
vector<vector<int>> findSsc(const Graph&);
void dfs(const Graph &g,
vector<bool> &vis,
int,
function<void(int)> postConsumer = [](int){});
int main () {
Graph g(5);
g.addEdge(1, 0);
g.addEdge(0, 2);
g.addEdge(2, 1);
g.addEdge(0, 3);
g.addEdge(3, 4);
vector<vector<int>> res = findSsc(g);
for (vector<int> &comp: res) {
for (int v : comp) {
cout << v << " ";
}
cout << endl;
}
return 0;
}
vector<vector<int>> findSsc(const Graph &g) {
int V = g.V;
vector<bool> vis(V, false);
stack<int> stk;
for (int i = 0; i < V; i++) {
if (!vis[i]) {
dfs(g, vis, i, [&](int v){
stk.push(v); // this consumer function will add each processed node to stack
});
}
}
fill(vis.begin(), vis.end(), false); // mark vis to all false
Graph transposedGraph = g.transpose();
vector<vector<int>> res;
while (!stk.empty()) {
int v = stk.top();
stk.pop();
vector<int> component;
if (!vis[v]) {
dfs(transposedGraph, vis, v, [&](int v){
component.push_back(v);
});
}
res.push_back(component);
}
return res;
}
void dfs(const Graph &g, vector<bool> &vis, int u, function<void(int)> consumer) {
vis[u] = true;
for (int v : g.adj[u]) {
if (!vis[v]) {
dfs(g, vis, v, consumer);
}
}
consumer(u);
}