i was recently tasked with finding a way to calculate SCC's in a directed graph by a teacher expecting me to reach Tarjan's or Kosaraju's Algorithm, but i reached a completely different solution. its arguably a bit worse in both simplicity and complexity, but i decided to write a blog about it anyways since i think i used some cool ideas, and who knows maybe somebody else can utilize this either to find a useful variant or in some completely unrelated problem. my teacher named it KS-SCC hinting at the fact that its dumb/useless(in farsi) lmao.
Conventions and facts used in the proofs
A vertex is active if mark = 1. The active vertices are exactly the path from the DFS root to the current vertex, so every active vertex is an ancestor of the current vertex, or the current vertex itself.
A component is a DSU set at the current moment. The top of a component is its shallowest vertex.
Unable to parse markup [type=CF_MATHJAX]
$ denotes the DFS subtree of
Unable to parse markup [type=CF_MATHJAX]
$.
Unable to parse markup [type=CF_MATHJAX]
$ means that there is a directed path from
Unable to parse markup [type=CF_MATHJAX]
$ to
Unable to parse markup [type=CF_MATHJAX]
$ (possibly empty).
- An edge
Unable to parse markup [type=CF_MATHJAX]
$, processed while
Unable to parse markup [type=CF_MATHJAX]
$ is the current vertex, is recorded if it is not a tree edge and
Unable to parse markup [type=CF_MATHJAX]
$ = up[find(z)] is active at that moment. For a recorded edge the algorithm does cnt[s]++ and cnt[t]--. Since
Unable to parse markup [type=CF_MATHJAX]
$ is active, it is an ancestor of
Unable to parse markup [type=CF_MATHJAX]
$, or
Unable to parse markup [type=CF_MATHJAX]
$ itself.claim 2: two nodes u & v are in the same SCC if and only if there exists a directed path from v to u and a directed path from u to v. [proof]
now to find the scc's we do the following recursive depth-first traversal: mark[u] = 0: undiscovered | mark[u] = 1: discovered & unfinished | mark[u] = 2: finished cnt[u] represents the number cycle closing edges in u's subtree such that the cycle includes atleast one vertex that is an ancestor of u up[u] represents the node with the lowest distance to root of dfs(i.e height) in u's SCC
SCC_DFS(u): 1. set mark[u] = 1. 2. go through all nodes v, connected by an edge to u:
if mark[v] == 0 (tree edge) call SCC_DFS(v) if cnt[v] > 0 merge the components of u & v add cnt[v] to cnt[u]
else if mark[v] == 1 (back edge) increment cnt[u] decrement cnt[v]
else if mark[v] == 2 (cross edge) if (mark[up[v]] == 1) cnt[u]++, cnt[up[v]]-- 3. set mark[u] = 2
explanation / Informal proof of the procedure: when looking at the list of edges adjacent to u during dfs there are three types you encounter: if e is an tree edge: that means that v is an undiscovered vertex so we still don't know anything about v's relation to u, so first call its SCC_DFS. claim 3: if node u is part of closed walk W with one of its ancestors a, there exists a closed walk W' where par[u], u ∈ W. so by claim 1 & 3, if cnt[v] > 0 then we can merge u & v component then we update cnt[u] based on cnt[v]
if e is an back edge: it closes a cycle: C = [P = {path from v to u using tree edges}] + e so we update the difference array
if e is an cross edge: by claim 2 we should merge u and v if there exists a path from v to u; since v's subtree is finished (i.e mark[v] = 2) there needs to be a w such that v has a directed path to w and mark[w] == 1.
claim 4: such w exists if and only if the lowest height node in v's SCC, fits w's criteria start of proof: suppose there exists a w that v has a directed path to, and mark[w] is 1, its easy to see that w is an ancestor of v, meaning there exists a directed path from w to v. by claim 2 w and v must be in the same SCC.
by claim 4 we can check if mark[up[v]] == 1 then we can treat this cross edge like an back edge from u to up[v] so we update the difference array accordingly
and we update up[v] in dsu: when merging the two components with roots u & v to a merged component with root u, we do: if h[up[v]] < h[up[u]] up[u] = up[v]. note we update up only for component's root so when looking for up[u] we should do up[component_root(u)] instead
then the SCC for each node u is: component_root(u)



