Skip to content

Commit 0e22d2b

Browse files
committed
make code more concise (and fix small mistake)
1 parent f4b3019 commit 0e22d2b

File tree

1 file changed

+16
-32
lines changed

1 file changed

+16
-32
lines changed

src/graph/strongly-connected-components.md

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,20 @@ Finally, it is appropriate to mention [topological sort](topological-sort.md) he
7373

7474
## Implementation
7575
```cpp
76-
vector<vector<int>> adj, adj_rev; // Adjacency lists of G and G^T.
77-
vector<bool> visited; // Keeps track of which nodes are already visited.
78-
vector<int> order; // Sorted list of vertices in G by exit time.
79-
vector<int> component; // Stores one strongly connected component at a time.
76+
vector<vector<int>> adj, adj_rev; // adjacency lists of G and G^T
77+
vector<bool> visited; // keeps track of which nodes are already visited
78+
vector<int> order; // sorted list of G's vertices by exit time
79+
vector<int> component; // stores one strongly connected component at a time
8080

81-
// Depth first search on G, used in step 1 of the algorithm.
8281
void dfs1(int v) {
8382
visited[v] = true;
84-
8583
for (auto u : adj[v])
8684
if (!visited[u])
8785
dfs1(u);
8886

8987
order.push_back(v);
9088
}
9189

92-
// Depth first search on G^T, used in step 2 of the algorithm.
9390
void dfs2(int v) {
9491
visited[v] = true;
9592
component.push_back(v);
@@ -100,60 +97,47 @@ void dfs2(int v) {
10097
}
10198

10299
int main() {
103-
int n = ...; // Number of vertices in G.
104-
105-
// Add edges to G.
106-
for (int i=0; i < n; i++) {
100+
int n = ...;
101+
for ( ... ) {
107102
int a = ..., b = ...;
108103
adj[a].push_back(b);
109104
adj_rev[b].push_back(a);
110105
}
111106

112107
visited.assign(n, false);
113108

114-
// Run the first series of depth first searches.
109+
// first series of depth first searches
115110
for (int i = 0; i < n; i++)
116111
if (!visited[i])
117112
dfs1(i);
118113

119114
visited.assign(n, false);
120115
reverse(order.begin(), order.end());
121116

122-
// These vectors are for the condensation graph;
123-
// they are explained in the text below.
124-
vector<int> roots(n, 0);
117+
// condensation graph (explained in text below)
125118
vector<int> root_nodes;
119+
vector<int> roots(n, 0);
126120
vector<vector<int>> adj_scc(n);
127121

128-
// Run the second series of depth first searches,
129-
// and add vertices to condensation graph.
130-
// During this process, we find all strongly connected components.
122+
// second series of depth first searches
131123
for (auto v : order)
132124
if (!visited[v]) {
133125
dfs2(v);
134-
135-
// Found a strongly connected component!
136-
// Add it to the condensation graph...
137126
int root = component.front();
138127
for (auto u : component) roots[u] = root;
139128
root_nodes.push_back(root);
140-
141129
component.clear();
142130
}
143131

144132

145-
// Add edges to condensation graph.
133+
// add edges to condensation graph
146134
for (int v = 0; v < n; v++)
147-
for (auto u : adj[v]) {
148-
int root_v = roots[v],
149-
root_u = roots[u];
150-
151-
if (root_u != root_v)
152-
adj_scc[root_v].push_back(root_u);
153-
}
135+
for (auto u : adj[v])
136+
if (roots[v] != roots[u])
137+
adj_scc[roots[v]].push_back(roots[u]);
154138

155-
// At this point, we found all strongly connected
156-
// components and constructed the condensation graph.
139+
// finished finding SCCs and constructing condensation graph.
140+
.......
157141
}
158142
```
159143

0 commit comments

Comments
 (0)