Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1
Last Classs 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 A B
In an undirected graph G(V, E) is
called connected, if G contains a path C
between every pair of vertices
Otherwise, they are called disconnected. D E
A directed graph is called connected if
every pair of distinct vertices in the F
graph is connected.
Connected Components Connected ?
A connected component is a maximal No
Yes
connected subgraph of G. Each vertex
belongs to exactly one connected
component, as does each edge.
Connectivity (cont.)
C D E C D E
Weakly Connected ? F F
YES
Connectivity (cont.)
C D E C D E
Strongly Connected ?
No F F
Connected Components
7
DFS - Strongly Connected
Components
A C E G
B D F H
8
DFS - Strongly Connected
Components
A C E G
B D F H
9
Component Graph
10
Strongly Connected Components
b c Edges b c
have reverse
direction!
a b c d M MT
a b c d
a 1 a
b 1 b 1
c c 1 1
d 1 1 d 1
11
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 SCCs. (u and v are
reachable from each other in G if and only if
reachable from each other in GT.)
12
Algorithm to determine SCCs
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
Time: (V + E).
13
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
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 SCCs other than C1
DFS visits all vertices in C2
the only edges out of C2 go to C1, which weve 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