CS 304: Design and Analysis of Algorithm: Strongly Connected Component
CS 304: Design and Analysis of Algorithm: Strongly Connected Component
Algorithm
1
Last Class’s Topic
● DFS
● Topological Sort
● Problems:
■ Detect cycle in an undirected graph
■ Detect cycle in a directed graph
■ How many paths are there from “s” to “t” in a
directed acyclic graph?
2
Connectivity
● Connected Graph
■ In an undirected graph G, two vertices u and v are called
connected if G contains a path from u to v. Otherwise, they
are called disconnected.
■ A directed graph is called connected if every pair of
distinct vertices in the graph is connected.
● Connected Components
■ A connected component is a maximal connected subgraph
of G. Each vertex belongs to exactly one connected
component, as does each edge.
Connectivity (cont.)
● Weakly Connected Graph
■ A directed graph is called weakly connected if
replacing all of its directed edges with undirected
edges produces a connected (undirected) graph.
● Strongly Connected Graph
■ It is strongly connected or strong if it contains a
directed path from u to v for every pair of vertices
u, v. The strong components are the maximal
strongly connected subgraphs
Connected Components
● Strongly connected graph
■ A directed graph is called strongly connected if for every
pair of vertices u and v there is a path from u to v and a
path from v to u.
● Strongly Connected Components (SCC)
■ The strongly connected components (SCC) of a directed
graph are its maximal strongly connected subgraphs.
● Here, we work with
■ Directed unweighted graph
Strongly Connected Components
● G is strongly connected if every pair (u, v) of
vertices in G is reachable from one another.
● A strongly connected component (SCC) of G
is a maximal set of vertices C V such that
for all u, v C, both u v and v u exist.
6
DFS - Strongly Connected
Components
A C E G
B D F H
7
DFS - Strongly Connected
Components
A C E G
B D F H
8
Component Graph
● GSCC = (VSCC, ESCC).
● VSCC has one vertex for each SCC in G.
● ESCC has an edge if there’s an edge between the
corresponding SCC’s in G.
● GSCC for the example considered:
9
Strongly Connected Components
b c Edges b c
have reverse
direction!
a b c d M a b c d MT
a 1 a
b 1 b 1
c c 1 1
d 1 1 d 1
10
Transpose of a Directed Graph
● GT = transpose of directed G.
■ GT = (V, ET), ET = {(u, v) : (v, u) E}.
■ GT is G with all edges reversed.
● Can create GT in Θ(V + E) time if using
adjacency lists.
● G and GT have the same SCC’s. (u and v are
reachable from each other in G if and only if
reachable from each other in GT.)
11
Algorithm to determine SCCs
SCC(G)
SCC(G)
1.1. call
callDFS(G)
DFS(G)totocompute
computefinishing
finishingtimes
timesff[u]
[u]for
forall
alluu
2.2. compute
computeGGTT
3.3. call
callDFS(G
DFS(GTT),),but
butininthe
themain
mainloop,
loop,consider
considervertices
verticesininorder
orderof
of
decreasing
decreasingf f[u]
[u](as
(ascomputed
computedininfirst
firstDFS)
DFS)
4.4. output
outputthe
thevertices
verticesinineach
eachtree
treeof
ofthe
thedepth-first
depth-firstforest
forestformed
formedininsecond
second
DFS
DFSasasaaseparate
separateSCCSCC
12
Example
a b c d
DFS on the initial graph G
13/14 11/ 16 1/ 10 8/ 9
b e a c d g h f
16 15 14 10 9 7 6 4
12/15 3/ 4 2/ 7 5/ 6
e f g h
a b c d
DFS on GT:
• start at b: visit a, e
• start at c: visit d
• start at g: visit f
• start at h
e f g h
Strongly connected components: C1 = {a, b, e}, C2 = {c, d}, C3 = {f, g}, C4 = {h}
Component Graph
a b c d
cd
abe
fg h
e f g h
● The component graph GSCC = (VSCC, ESCC):
12/15 3/ 4 2/ 7 5/ 6
e f g h
C3 C4
d(C3) =2 d(C4) =5
f(C3) =7 f(C4) =6
Lemma 2
12/15 3/ 4 2/ 7 5/ 6
e f g h
C3 C4
d(C3) =2 d(C4) =5
f(C3) =7 f(C4) =6
Corollary
C1 = C’ C2 = C
a b c d
e f g h
C3 C4
Why does SCC Work?
● When we do the second DFS, on GT, we start with a component C such
that f(C) is maximum (b, in our case)
● We start from b and visit all vertices in C1
● From corollary: f(C) > f(C’) in G for all C C’ there are no edges from
C to any other SCCs in GT
DFS will visit only vertices in C1
The depth-first tree rooted at b contains exactly the vertices of C1
C1 C2
a b c d
b e a c d g h f
16 15 14 10 9 7 6 4
C3 C4
e f g h
Why does SCC Work? (cont.)
● The next root chosen in the second DFS is in SCC C2 such that f(C) is
maximum over all SCC’s other than C1
● DFS visits all vertices in C2
■ the only edges out of C2 go to C1, which we’ve already visited
C1 C2
a b c d
b e a c d g h f
16 15 14 10 9 7 6 4
C3 C4
e f g h
Reference
● Book: Cormen – Chapter 22 – Section 22.5
● Exercise:
■ 22.5-1: Number of componets change?
■ 22.5-6: Minimize edge list
■ 22.5-7: Semiconnected graph