DFS, DAG, and Strongly Connected Components: Shang-Hua Teng
DFS, DAG, and Strongly Connected Components: Shang-Hua Teng
Topological Sort
Topological sort of a DAG:
Linear ordering of all vertices in graph G such that
vertex u comes before vertex v if edge (u, v) G
Time: O(V+E)
Correctness: need to prove that
(u,v) G f[u]>f[v]
c
d
f
e
b
Strongly-Connected
Graph G is strongly connected if, for every u
and v in V, there is some path from u to v and
some path from v to u.
Strongly
Connected
Not Strongly
Connected
Strongly-Connected Components
A strongly connected component of a graph is a
maximal subset of nodes (along with their
associated edges) that is strongly connected.
Nodes share a strongly connected component
if they are inter-reachable.
{a,c,g}
{f,d,e,b}
b
c
d
f
{a,c,g}
{f,d,e,b}
e
b
Finding Strongly-Connected
Components
Input: A directed graph G = (V,E)
Output: a partition of V into disjoint sets so
that each set defines a strongly connected
component of G
How should we compute the partition?
d (U ) min uU d [u ]
f (U ) max uU f [u ]
If we output U in VSCC in the decreasing order of
f[U], then we topologically sort GSCC
Lemma: Let U and U be distinct strongly connected
component, suppose there is an edge (u,v) in E
where u in U and v in U. Then f[U] > f[U]
x u v U'
Transpose of a Digraph
Transpose of G = (V,E):
GT=(V, ET), where ET={(u, v): (v, u) E}
If G is a DAG then GT is also a DAG
If we print the topological order of G in the
reverse order, then it is a topological order
of GT
Strongly-Connected Components
Strongly-Connected-Components(G)
1. call DFS(G) to compute finishing times f[u] for each
vertex u.
2. compute GT
3. call DFS(GT), but in the main loop of DFS, consider
the vertices in order of decreasing f[u]
4. output the vertices of each tree in the depth-first forest
of step 3 as a separate strongly connected component.
The graph GT is the transpose of G, which is visualized
by reversing the arrows on the digraph.
a4
d1
b3
c2
Graph Gr
a4
c2
b3
a4
after step 1
d1
b3
c2
d1
Runtime
Lines 1 and 3 are (E+V) due to DFS
Line 2 involves creating an adjacency list or
matrix, and it is also O(E+V)
Line 4 is constant time
So, SCC(G) is (E+V)
Strongly-Connected Components
DFS on G, starting at c.
node a
d=13
f=14
node b
d=11
f=16
node c
d=1
f=10
node d
d=8
f=9
node e
d=12
f=15
node f
d=3
f=4
node g
d=2
f=7
node h
d=5
f=6
DFS on G
node a
d=13
f=14
node b
d=11
f=16
node c
d=1
f=10
node d
d=8
f=9
node e
d=12
f=15
node f
d=3
f=4
node g
d=2
f=7
node h
d=5
f=6
node a
d=2
f=5
=b
node b
d=1
f=6
=NIL
node c
d=7
f=10
=NIL
node d
d=8
f=9
=c
node e
d=3
f=4
=a
node f
d=12
f=13
=g
node g
d=11
f=14
=NIL
node h
d=15
f=16
=NIL
DFS on G
node b
d=1
f=6
=NIL
node c
d=7
f=10
=NIL
node d
d=8
f=9
=c
node e
d=3
f=4
=a
node f
d=12
f=13
=g
node g
d=11
f=14
=NIL
node h
d=15
f=16
=NIL
Strongly-Connected Components
These are the 4 trees that result, yielding the strongly connected
components.
Finally, merge the nodes of any given tree into a super-node, and draw
links between them, showing the resultant acyclic component graph.
b
a
c
d
abe
cd
fg
Component Graph