--- tags: Graph Theory title: Strongly Connected Component --- # Kosaraju's Algorithm * Notion: Strongly connected components will also be strongly connected in *transposed graph*. ```cpp= vector<vector<int>> adj, radj; vector<int> timeStamp, scc; vector<bool> vis; void dfs(int u) { vis[u] = true; for (int v : adj[u]) if (!vis[v]) dfs(v); timeStamp.push_back(u); } void dfs2(int u, int rep) { vis[u] = true; scc[u] = rep; for (int v : radj[u]) if (!vis[v]) dfs2(v, rep); } void kosaraju() { int n = adj.size(); fill(vis.begin(), vis.end(), false); for (int i = 0; i < n; ++i) if (!vis[i]) dfs(i); fill(vis.begin(), vis.end(), false); while(!timeStamp.empty()) { int u = timeStamp.back(); timeStamp.pop_back(); if (!vis[u]) dfs2(u, u); } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up