0% found this document useful (0 votes)
11 views

L32 Connected

This document discusses strongly connected components (SCCs) in directed graphs and algorithms to find them. It begins by defining what an SCC is - a maximal set of vertices where every pair is reachable from each other. SCCs can be used to group software modules or social network friends. The algorithm works by first running DFS to find finishing times, then taking the transpose graph and running DFS again in decreasing finishing time order. This outputs the vertices of each SCC as the vertices in each tree of the second DFS.

Uploaded by

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

L32 Connected

This document discusses strongly connected components (SCCs) in directed graphs and algorithms to find them. It begins by defining what an SCC is - a maximal set of vertices where every pair is reachable from each other. SCCs can be used to group software modules or social network friends. The algorithm works by first running DFS to find finishing times, then taking the transpose graph and running DFS again in decreasing finishing time order. This outputs the vertices of each SCC as the vertices in each tree of the second DFS.

Uploaded by

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

DFS Applications

Strongly connected components

Instructor: Ashok Singh Sairam


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.

• Aim: Find SCC of a DAG


MA512: Data Structures and Algorithms
2
SCC: Applications
• Packaging software modules
 Construct directed graph of which modules call which
other modules
• Social networking sites
 Find people who are friends of one another and have
common interest

MA512: Data Structures and Algorithms


3
Transpose of a Directed Graph
• GT = transpose of a
G
b c d
directed G.
T T
 G = (V, E ),

 ET = {(u, v):(v, u)  E }.
T e f g h
 G is G with all edges

reversed. a GT b c d

e f g h
MA512: Data Structures and Algorithms
4
Transpose of a Directed Graph
• Transpose: Adjacency list representation
 Adj’[1..|V|]: new adjacency list representation
 For each vertex 𝑢 ∈ 𝐺

• For each vertex 𝑣 ∈ 𝑎𝑑𝑗 𝑢


• Insert (Adj’[v], u)
• 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.)

MA512: Data Structures and Algorithms


5
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.

• We will show that the component graph is a DAG

MA512: Data Structures and Algorithms


6
Component Graph: example

• GSCC for the example considered:

MA512: Data Structures and Algorithms


7
SCCs is a DAG: example
• If there is a path from u to u’, there cannot be a path
from v’ to v

u u’

v v’

C C’

MA512: Data Structures and Algorithms


8
GSCC are a DAG
Lemma 22.13
Let C and C be distinct SCC’s in G, let u, v  C, u, v  C, and
suppose there is a path u u in G. Then there cannot also be a path
v v in G.
Proof:
• Suppose there is a path v v in G.
• Then there are paths u u v and v v u in G.
• Therefore, u and v are reachable from each other, so
they are not in separate SCC’s

MA512: Data Structures and Algorithms


9
SCC: Algorithm
SCC(G)
1. call DFS(G) to compute finishing times f [u] for all u
2. compute GT
3. call DFS(GT), but in the main loop, consider vertices in order of
decreasing f [u] (as computed in first DFS)
4. output the vertices in each tree of the depth-first forest formed
in second DFS as a separate SCC

MA512: Data Structures and Algorithms


10
SCC: Example
• Compute finishing time of all vertices
G a b c d
13/14 11/16 1/10 8/9

12/15 3/4 2/7 5/6


e f g h

MA512: Data Structures and Algorithms


11
a b c d

13/14 11/16 1/10 8/9

12/15 3/4 2/7 5/6

• Compute GT e f g h

• Call DFS, consider vertices in decreasing order f(u)


a b c d

e f g h

MA512: Data Structures and Algorithms


12
a b c d a b c d
13/14 11/16 1/10 8/9

12/15 3/4 2/7 5/6


e f g h
e f g h

• Output vertices in each tree in second DFS …

cd
abe

fg h

MA512: Data Structures and Algorithms


13
Exercise

MA512: Data Structures and Algorithms


14
How does it work?
• Idea:
 Considering vertices in 2nd DFS in decreasing order f(u) 
visiting vertices of component graph in topological order.
 Because we are running DFS on GT, we will not be visiting

any v from a u, where v and u are in different components.


• Notation:
 d[u] and f [u] always refer to first DFS.

 Extend notation for d and f to sets of vertices U  V:

 d(U) = minuU{d[u]} (earliest discovery time)


 f (U) = maxuU{ f [u]} (latest finishing time)

MA512: Data Structures and Algorithms


15
Lemma 22.14
Let C and C be distinct SCC’s in G = (V, E). Suppose there is an
edge (u, v)  E such that u  C and v C. Then f (C) > f (C).
Proof: Case 1: d(C) < d(C)
 Let x be the first vertex discovered in C.
 At time d[x], all vertices in C and C are white. Thus, there
exist paths of white vertices from x to all vertices in C and C.
 By the white-path theorem, all vertices in C and C are
descendants of x in depth-first tree.
 By the parenthesis theorem, f [x] = f (C) > f(C).
C
C

u v

x
MA512: Data Structures and Algorithms
16
Proof:Case 2: d(C) > d(C)
 Let y be the first vertex discovered in C.
 At time d[y], all vertices in C are white and there is a white
path from y to each vertex in C  all vertices in C become
descendants of y. Again, f [y] = f (C).
 At time d[y], all vertices in C are also white.

 By earlier lemma, since there is an edge (u, v), we cannot


have a path from C to C.
 So no vertex in C is reachable from y.
 Therefore, at time f [y], all vertices in C are still white.
 Therefore, for all w  C, f [w] > f [y], which implies that f (C)
> f (C). C C

u v

x y

MA512: Data Structures and Algorithms


17
Corollary 22.15
Let C and C be distinct SCC’s in G = (V, E). Suppose there is an
edge (u, v)  ET, where u  C and v  C. Then f(C) < f(C).
Proof:
• (u, v)  ET  (v, u)  E.
• Since SCC’s of G and GT are the same, f(C) > f (C),
by Lemma 22.14.

MA512: Data Structures and Algorithms


18
Correctness of SCC
• When we do the second DFS, on GT, start with SCC
C such that f(C) is maximum.
 The second DFS starts from some x  C, and it visits all
vertices in C.
 Corollary 22.15 says that since f(C) > f (C) for all C  C,
there are no edges from C to C in GT.
 Therefore, DFS will visit only vertices in C.
 Which means that the depth-first tree rooted at x
contains exactly the vertices of C.

MA512: Data Structures and Algorithms


19
• The next root chosen in the second DFS is in SCC
C such that f (C) is maximum over all SCC’s other
than C.
 DFS visits all vertices in C, but the only edges out of C
go to C, which we’ve already visited.
 Therefore, the only tree edges will be to vertices in C.
• We can continue the process.
• Each time we choose a root for the second DFS, it
can reach only
 vertices in its SCC—get tree edges to these,
 vertices in SCC’s already visited in second DFS—get no
tree edges to these.

MA512: Data Structures and Algorithms


20
Acknowledgement
David A. Plaisted
Department of Computer Science
University of North Carolina at Chapel Hill

Analysis of Algorithms
21

You might also like