0% found this document useful (0 votes)
63 views22 pages

CS 304: Design and Analysis of Algorithm: Strongly Connected Component

1) The algorithm computes strongly connected components (SCCs) in two passes of depth-first search (DFS): first on the original graph G to find finishing times, then on the transpose graph GT considering vertices in decreasing finishing time order. 2) Any SCCs connected by a directed edge in G must have the later finishing SCC appear before the earlier in the DFS tree of GT. 3) The SCCs form the nodes of a directed acyclic component graph, with edges between SCCs corresponding to edges between their vertices in G.

Uploaded by

Monjurur shipu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views22 pages

CS 304: Design and Analysis of Algorithm: Strongly Connected Component

1) The algorithm computes strongly connected components (SCCs) in two passes of depth-first search (DFS): first on the original graph G to find finishing times, then on the transpose graph GT considering vertices in decreasing finishing time order. 2) Any SCCs connected by a directed edge in G must have the later finishing SCC appear before the earlier in the DFS tree of GT. 3) The SCCs form the nodes of a directed acyclic component graph, with edges between SCCs corresponding to edges between their vertices in G.

Uploaded by

Monjurur shipu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

CS 304: Design and Analysis of

Algorithm

Strongly Connected Component

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

The transpose MT of an NxN matrix M is the matrix obtained


when the rows become columns and the column become rows:
a d a d GT
G

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

Time: (V + E).

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):

■ VSCC = {v1, v2, …, vk}, where vi corresponds to each


strongly connected component Ci
■ There is an edge (vi, vj)  ESCC if G contains a directed
edge (x, y) for some x  Ci and y  Cj
● The component graph is a DAG
Lemma 1

Let C and C’ be distinct SCCs in G


Let u, v  C, and u’, v’  C ’
Suppose there is a path u  u’ in G
Then there cannot also be a path v’  v in G.
Proof C C’
• Suppose there is a path v’  v
u
• There exists u  u’  v’ u’
• There exists v’  v  u v
• u and v’ are reachable from each v’
other, so they are not in separate
SCC’s: contradiction!
Notations
● Extend notation for d (starting time) and f (finishing
time) to sets of vertices U  V:
■ d(U) = minuU { d[u] } (earliest discovery time)
■ f(U) = maxuU { f[u] } (latest finishing time)
C1 C2
a b c d
d(C1) =11 d(C2) =1
13/14 11/ 16 1/ 10 8/ 9
f(C1) =16 f(C2) =10

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

● Let C and C’ be distinct SCCs in a directed graph G =


(V, E). If there is an edge (u, v)  E, where u  C
and v  C’ then f(C) > f(C’).
● Consider C1 and C2, connected by edge (b, c)
C1 C2
a b c d
d(C1) =11 d(C2) =1
13/14 11/ 16 1/ 10 8/ 9
f(C1) =16 f(C2) =10

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

● Let C and C’ be distinct SCCs in a directed graph G =


(V, E). If there is an edge (u, v)  ET, where u  C
and v  C’ then f(C) < f(C’).
● Consider C2 and C1, connected by edge (c, b)
C1 = C’ C2 = C • Since (c, b)  ET 
a b c d (b, c)  E
• From previous
lemma:
f(C1) > f(C2)
e f g h
f(C’) > f(C)
C3 C4
f(C) < f(C’)
Corollary

● Each edge in GT that goes between different


components goes from a component with an earlier
finish time (in the DFS) to one with a later finish time

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

 The only tree edges will be to vertices in C2


● Each time we choose a new root it can reach only:
■ vertices in its own component
■ vertices in components 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

You might also like