TDDC86 – Repetition Material on Depth First Search and SCCs... Christoph Kessler, IDA, Link ¨opings Universitet, 2009. Page 2

advertisement
TDDC86 – Repetition Material on Depth First Search and SCCs in Directed Graphs.
Page 1
Christoph Kessler, IDA, Linköpings Universitet, 2009.
TDDC86 – Repetition Material on Depth First Search and SCCs in Directed Graphs.
Page 2
Christoph Kessler, IDA, Linköpings Universitet, 2009.
DFS and Strongly Connected Components for Directed Graphs
DFS for directed graphs
Consider DFS, recursive formulation, for a directed graph G = (V; E ).
Call DF S (v ) inspects all edges and vertices reachable from v
procedure DepthFirstSearch ( Graph = (
for each 2 do visited[ ] false;
for each 2 do
if not visited[ ] then DFS( );
procedure DFS ( vertex )
G
v
V
v
V
))
We can classify the edges in 4 classes, T , F , B , C :
Tree edges
v
v
visited[v]
previsit(v)
V; E
= edges that DFS follows by its recursive calls
Forward edges
v
f some operation on
)2
for all with (
if not visited[ ] then DFS( )
w
v; w
v
TDDC86 – Repetition Material on Depth First Search and SCCs in Directed Graphs.
v
after visiting the neighbors g
Page 3
Christoph Kessler, IDA, Linköpings Universitet, 2009.
Extend DFS by conceptual computation of T , F , B , C
Add 2 numbers to each vertex v :
dfsnum(v): order of recursive DFS calls (previsit order)
compnum(v): order of completion of DFS calls (postvisit order)
procedure DepthFirstSearch ( Graph = (
G
count1 0
count2 0
f
T
for each 2 do visited[ ] false;
for each 2 do
if not visited[ ] then DFS( );
v
V
v
V
v
v
V; E
))
v
!T
w
(there is a path from v to w consisting only of tree edges)
B
= edges (v; w) where w already visited and w
!T
v
C
= all other edges, i.e., w already visited and neither v
TDDC86 – Repetition Material on Depth First Search and SCCs in Directed Graphs.
Page 4
procedure DFS ( vertex
visited[v] true
count1 count1+1;
for all with ( ) 2
if not visited[ ]
then
w
v; w
v
)
dfsnum[v] count1
E
w
T
T
DFS(w)
f else f
w
[ f(
v; w
)g g
visited earlier g
if !T then
[ f( )g
elseif !T then
[ f( )g
else
[ f( )g g
v
w
w
C
F
C
count2 count2+1;
v
F
B
v; w
v; w
B
v; w
compnum[v] count2
!T
w
nor w
!T
v
Christoph Kessler, IDA, Linköpings Universitet, 2009.
Extend DFS by conceptual computation of T , F , B , C
f
;g
, F , B, C
Backward edges
Cross edges
w
f some other operation on
postvisit(v)
before visiting the neighbors g
E
w
F
= edges (v; w) where w already visited and v
v
true
T
(cont.)
TDDC86 – Repetition Material on Depth First Search and SCCs in Directed Graphs.
Page 5
Christoph Kessler, IDA, Linköpings Universitet, 2009.
TDDC86 – Repetition Material on Depth First Search and SCCs in Directed Graphs.
Page 6
Christoph Kessler, IDA, Linköpings Universitet, 2009.
DFS Theorem
DFS Theorem (cont.)
Lemma 1: DFS needs time O(jV j + jE j).
(g) For v; w; z 2 V with v !T w, (w; z ) 2 E , and not v
(i) dfsnum[z ] < dfsnum[v ]
(ii) (w; z ) 2 B [ C
(iii) compnum[z ] > compnum[v ] , (w; z ) 2 B
(iv) compnum[z ] < compnum[v ] , (w; z ) 2 C
Lemma 2: (Properties of the extended algorithm)
(a) T , F , B , C is a partition of E .
(b) T corresponds to the call tree of DFS.
!T
(c) v
w
, dfsnum[ ] dfsnum[ ] and compnum[ ] compnum[ ]
)2 :
[ , dfsnum[ ] dfsnum[ ]
)2 :
, dfsnum[ ] dfsnum[ ] and compnum[ ] compnum[ ]
)2 :
, dfsnum[ ] dfsnum[ ] and compnum[ ] compnum[ ]
v
(d) For all (v; w
)2
(e) For all (
( )2
(f) For all (
( )2
(
v; w
v; w
T
F
C
v
v
<
w
E
w
v; w
v; w
w
<
v
, holds:
Lemma 2 allows algorithmic classification from dfsnum and compnum.
w
>
For acyclic graphs, B = ;:
for all (v; w) 2 E : compnum[v ]
compnum[w].
Thus, num[v ] = n + 1 compnum[v ] for all v 2 V
v
w
<
v
w
<
>
yields a topological order ! alternative to TOPSORT
E
v
TDDC86 – Repetition Material on Depth First Search and SCCs in Directed Graphs.
TDDC86 – Repetition Material on Depth First Search and SCCs in Directed Graphs.
Page 7
The SCCs can be computed in time (jV j + jE j) by an extension of DFS.
A directed graph G = (V; E ) is strongly connected iff 8v , w
2
V
:v
!
w
The strongly connected components (SCC) of G are
the maximal (wrt. set inclusion) strongly connected subgraphs of G.
d
h
f
C4
g
e
b
C2
a
C1
Christoph Kessler, IDA, Linköpings Universitet, 2009.
Computing the strongly connected components of a directed graph
An application of DFS.
c
Page 8
Christoph Kessler, IDA, Linköpings Universitet, 2009.
Strongly Connected Components of a directed graph
C3
z
E
v; w
B
w
!T
=f g
2 = f
3 = f g
4 = f g
C1
a
C
b; c; e; f; g
C
d
C
h
g
Application of SCCs:
Identifying loops in
low-level code
.
Use the transposed graph GT
G
T
=(
V; E
T
) with
E
T
= f(
v; u
): (
u; v
) 2 g.
E
(adjacency list repres.) can be computed from G in time O(jV j + jE j).
Note: G and GT have the same SCCs.
1. DepthFirstSearch(G) to compute compnum[v ] for each v 2 V
2. compute GT from G
3. DepthFirstSearch(GT ), but in the main loop consider
the vertices v in order of decreasing compnum[v ]
4. output the vertices of each tree in the DFS forest of step 3
as a separate SCC
Proof: see [Cormen/Leiserson/Rivest, chapter 23.5]
Download