strongly connected component
TRANSCRIPT
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 1/22
1
CS 304: Design and Analysis of Algorithm
Strongly Connected Component
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 2/22
L ast Class¶s Topic
DF ST opological Sort
P roblems:D etect cycle in an undirected graphD etect cycle in a directed graphHow many paths are there from ³s´ to ³t´ in adirected acyclic graph?
2
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 3/22
Connectivity
Connected GraphIn an undirected graph G, two vertices u and v are calledconnected 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 subgraphof G. Each vertex belongs to exactly one connectedcomponent, as does each edge.
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 4/22
Connectivity (cont.)
W eakly Connected GraphA directed graph is called w eakly connected if replacing all of its directed edges with undirectededges produces a connected (undirected) graph.
Strongly Connected GraphIt is strongly connected or strong if it contains a
directed path from u to v for every pair of verticesu, v. T he strong components are the maximalstrongly connected subgraphs
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 5/22
Connected Components
Strongly connected graphA 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)
T he s trongly connected component s (SCC ) of a directedgraph are its maximal strongly connected subgraphs.
Here, we work withD irected unweighted graph
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 6/22
6
Strongly Connected Components
G is strongly connected if every pair ( u, v) of vertices in G is reachable from one another.
Astrongly 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.
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 7/22
7
DFS - Strongly ConnectedComponents
A
D
EC
FB
G
H
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 8/22
8
DFS - Strongly ConnectedComponents
A
D
EC
FB
G
H
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 9/22
9
Component Graph
GSCC = (V SCC , E SCC ).V SCC has one vertex for each SCC in G.
E SCC
has an edge if there¶s an edge betweenthe corresponding SCC¶s in G.GSCC for the example considered:
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 10/22
10
Strongly Connected Components
T he tran s po s e MT of an NxN matrix M is the matrix obtainedwhen the rows become columns and the column become rows:
a b c da
bc
d
a b c da
bc
d
a
b c
d da
b c
M M T
G TG
Edge s have rever s edirection!
1
1 1
1
1
1
11
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 11/22
11
Transpose of a Directed Graph
GT = tran s po s e of directed G.GT = (V , E T ), E T = { (u, v) : (v, u) E }.GT is G with all edges reversed.
Can create GT in (V + E ) time if usingadjacency lists.G and GT have the same SCC¶s . (u and v arereachable from each other in G if and only if reachable from each other in GT .)
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 12/22
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 13/22
Ex ample
a b c d
e f g h
1/
2/ 3/4 6 5/ 7
8/ 11/
12/
13/ 910 14
15
16
f 4
h6
g 7
d 9
c 10
a14
e15
b16
DFS on t he initi al graph G
DFS on G T:
st ar t a t b: visit a , e
st ar t a t c : visit d st ar t a t g : visit f st ar t a t h
S t r on gly c onn ec t ed c om pon e nts: C 1
= { a , b, e}, C 2
= { c , d}, C 3
= { f , g}, C 4
= { h}
a b c d
e f g h
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 14/22
Component Graph
T he component graph GSCC = (V SCC , ESCC ):
VSCC = {v1, v2, «, v k}, where vi corresponds to each
strongly connected component CiT here is an edge ( vi, v j ) ESCC if G contains a directededge ( x , y) for some x Ci and y C j
T he component graph is a D AG
a b c d
e f g h
a b e
c d
f g h
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 15/22
L emma 1
Let C and C¶ be distinct SCCs in GLet u, v C, and u·, v· C ·Suppose there is a path u @ u· in G
T hen there cannot also be a path v· @ v in G.
u
v u¶
v ¶
Proof
S u pp os e t here is a pa t h v· @ v
T here ex ists u @ u · @ v·
T here ex ists v· @ v @ u
� u a nd v· are reachable fr om each ot her , so t hey are not in s epara t e S CC ¶ s: c ont rad i c tion!
C C ¶
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 16/22
N otations
Extend notation for d (starting time) and f (finishingtime) to sets of vertices U V:
d(U) = min u U { d[ u] } (earliest discovery time)
f(U) = max u U { f[ u] } (latest finishing time)
a b c d
e f g h
1/
2/ 3/4 6 5/ 7
8/ 11/
12/
13/ 910 14
15
16
C 1 C 2
C 3 C 4
d( C 1 )f( C 1 )
d( C 2 )f( C 2 )
d( C 3 )
f( C 3 )
d( C 4 )
f( C 4 )
=11=16
=1=10
=2
=7
=5
=6
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 17/22
L emma 2
Let C and C¶ be distinct SCCs in a directed graph G =(V, E). If there is an edge (u, v) E, where u Cand v C¶ then f(C) > f(C¶).
Consider C 1 and C 2, connected by edge (b , c)a b c d
e f g h
1/
2/ 3/4 6 5/ 7
8/ 11/
12/
13/ 910 14
15
16
C 1 C 2
C 3 C 4
d( C 1 )f( C 1 )
d( C 2 )f( C 2 )
d( C 3 )
f( C 3 )
d( C 4 )
f( C 4 )
=11=16
=1=10
=2
=7
=5
=6
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 18/22
Corollary
Let C and C¶ be distinct SCCs in a directed graph G =(V, E). If there is an edge (u, v) ET , where u Cand v C¶ then f(C) < f(C¶).
Consider C 2 and C 1, connected by edge (c, b)C 1 = C ¶ C 2 = C
C 3 C 4
a b c d
e f g h
S ince (c, b) E T ( b , c) E
Fr om pre viou slemm a :f( C 1 ) > f( C 2 )f( C ¶ ) > f( C )f( C ) < f( C ¶ )
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 19/22
Corollary
Each edge in G T that goes between differentcomponents goes from a component with an earlier finish time (in the DF S) to one with a later finish time
C 1 = C ¶ C 2 = C
C 3 C 4
a b c d
e f g h
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 20/22
W hy does SCC W ork?
W hen we do the second DF S, on G T , we start with a component C such thatf(C) is maximum (b, in our case)W e start from b and visit all vertices in C 1
F rom corollary: f(C) > f(C¶) in G for all C { C¶ there are no edges from
C to any other SCCs in GT
DF S will visit only vertices in C 1
T he depth-first tree rooted at b contains exactly the vertices of C 1
C 1
C 2
C 3 C 4
a b c d
e f g h
f 4
h6
g 7
d 9
c 10
a14
e15
b16
8/6/2019 Strongly Connected Component
http://slidepdf.com/reader/full/strongly-connected-component 21/22
W hy does SCC W ork? (cont.)
T he next root chosen in the second DF S is in SCC C 2 such that f(C) ismaximum over all SCC¶s other than C 1
DF S visits all vertices in C 2
the only edges out of C 2 go to C 1, which we¶ve already visited
T he only tree edges will be to vertices in C 2
Each time we choose a new root it can reach only:vertices in its own componentvertices in components already visited
C 1 C 2
C 3 C 4
a b c d
e f g h
f 4
h6
g 7
d 9
c 10
a14
e15
b16