ALGORITHMS AND DATA STRUCTURES
Prof. Massimo Poncino
Lez 19 - Application of Depth-First-Search
Algorithms Applications of DFS
Outline
Î Temporal labels
Temporal labels
Î Application of DFS
Æ Topological sort
Æ Strongly connected
components
DFS: temporal labels DFS Visit with labels
DFS marks every vertex with
“temporal” information DFS_recursive(
DFS_recursive (G)
DFS_recursive(G)
d[v]: time in which v is for each vertex u ∈ V do
first visited (discovered color[u] = white
color[u
color[u]
and made grey) pred[
pred[u] = NULL
pred[u]
f[v]: time in which v is
processed (processed for each vertex u ∈ V do
and made black) u] = white
if color[u
color[
color[u]
Useful in many applications (G,u)
then DFS_visit(
DFS_visit G,u)
DFS_visit(G,u)
of DFS
Copyright © Università Telematica Internazionale UNINETTUNO
1
ALGORITHMS AND DATA STRUCTURES
Prof. Massimo Poncino
Lez 19 - Application of Depth-First-Search
DFS Visit with labels DFS Visit with labels
DFS_recursive(
DFS_recursive (G)
DFS_recursive(G) DFS_visit(
DFS_visit (G,u)
G,u)
DFS_visit(G,u)
for each vertex u ∈ V do color [[u]
u] ← grey
color[u
color[ u] = white
color[u]
while ∃ v adj to u do
pred[
pred[u] = NULL
pred[u]
if color [[v]
v] = white
time = 0 then P[vv] ← u
P[
P[v]
for each vertex u ∈ V do DFS_visit(
DFS_visit (G,v)
G,v)
DFS_visit(G,v)
if color[u
color[u] = white
color[u] color[u] ← black
color[
color[u]
then DFS_visit(
DFS_visit (G,u)
G,u)
DFS_visit(G,u)
DFS Visit with labels DFS: temporal labels
Notation
DFS_visit(
DFS_visit (G,u)
G,u)
DFS_visit(G,u)
color [[u]
u] ← grey Each vertex is labeled
u] = time = time + 1
d[u
d[
d[u] with a pair d[v]/f[v]
while ∃ v adj to u do
if color [[v]
v] = white
then P[v
P[ v] ← u
P[v]
d[v]/f[v] v
DFS_visit(
DFS_visit (G,v)
G,v)
DFS_visit(G,v)
color[u] ← black
color[
color[u]
f[u] = time = time + 1
f[u
f[u]
DFS with labels: example DFS with labels: example
B H 1/- A B H 1/- A
A 2/- B A 2/- B
C D G C D G
3/- C 3/- C
H
G 4/- D G 4/- D
F K F F K F
5/- F 5/- F
D D
C 6/- G C 6/- G
B B
A s 7/- H A s 7/8 H
Copyright © Università Telematica Internazionale UNINETTUNO
2
ALGORITHMS AND DATA STRUCTURES
Prof. Massimo Poncino
Lez 19 - Application of Depth-First-Search
DFS with labels: example DFS with labels: example
B H 1/- A B H 1/- A
A 2/- B A 2/- B
C D G C D G
3/- C 3/- C
K 4/- D 4/- D
F K F F K F
5/- F 5/- F
D D
10/ 10/11
C 6/9 G K C 6/9 G K
B B
A s 7/8 H A s 7/8 H
DFS with labels: example DFS with labels: example
B H 1/- A B H 1/- A
A 2/- B A 2/- B
C D G C D G
3/- C 3/- C
4/- D 4/13 D
K F K F
5/12 F 5/12 F
D
10/11 10/11
C 6/9 G K C 6/9 G K
B B
A s 7/8 H A s 7/8 H
DFS with labels: example DFS with labels: example
B H 1/- A B H 1/- A
A 2/- B A 2/15 B
C D G C D G
3/14 C 3/14 C
4/13 D 4/13 D
K F K F
5/12 F 5/12 F
10/11 10/11
6/9 G K 6/9 G K
B
A s 7/8 H A s 7/8 H
Copyright © Università Telematica Internazionale UNINETTUNO
3
ALGORITHMS AND DATA STRUCTURES
Prof. Massimo Poncino
Lez 19 - Application of Depth-First-Search
DFS with labels: example DFS: temporal labels
B H 1/16 A
A 2/15 B Any vertex v is
C D G
3/14 C White for times < d[v]
4/13 D Grey for times between
K F d[v] and f[v]
5/12 F
10/11 Black for times > f[v]
6/9 G K
s 7/8 H
DFS: classification of edges DFS: classification of edges
Four types
(DFS) Tree edges (Tree) Temporal labels can be
used to systematically
Edge connecting a vertex to classify edges of DFS
a descendant in the DFS visits
forest (Forward)
Edge connecting a vertex to Based on the color of a
an ancestor in the DFS vertex v reached while
forest (Backward) exploring an edge
All other edges (Cross)
DFS: classification of edges Classification of edges:
example
White
1/
(u,v) is a tree edge A
Grey T
(u,v) is a back edge 2/5
6/ B
BB CC
Black: (u,v) is C
A forward edge if d[u]< d[v]
A cross edge if d[v] < d[u] 3/4 D E
Copyright © Università Telematica Internazionale UNINETTUNO
4
ALGORITHMS AND DATA STRUCTURES
Prof. Massimo Poncino
Lez 19 - Application of Depth-First-Search
Classification of edges: Classification of edges:
example example
1/ 1/10
A A
T T
2/5 2/5
6/9 B 6/9 B
B CC B C
C C
F
3/4 D E 7/8 3/4 D E 7/8
Application of DFS
Topological sort of
vertices in a DAG
Application of DFS
Calculation of the
(strongly) connected
components (SCC) of an
(un)directed graph
Topological sort Topological sort:
A topological sort of a DAG G=(V,E) applications
is a linear sorting of its vertices Scheduling a sequence of
such that if there exists an edge
(u,v) ∈ E then u precedes v activities
v Vertices = activities
w x u w y v x Edges = temporal dependencies
u
u w y v x (u,v) → activity u must
y be completed before v
Copyright © Università Telematica Internazionale UNINETTUNO
5
ALGORITHMS AND DATA STRUCTURES
Prof. Massimo Poncino
Lez 19 - Application of Depth-First-Search
Topological sort: Topological sort:
applications applications
Example: getting dressed Example: getting dressed
pants
pants shirt
socks
socks shoes
belt tie
belt
shoes shirt
tie
jacket
jacket
Topological sort Topological sort: algorithm
A topological sort is Based on DFS
not unique in general
TopSort(G)
TopSort(
TopSort(G)
u w y v x 1 Use D FS(G) to compute f[v]
FS(
DFS(G) f[v]
v u w y v x 2 Each time a vertex is finished
add it as head of a list
w x
u u w v x y Return the list of vertices
u w v x y
1 = Θ(V + E)
y Θ(V + E)
u v w y x 2 = Θ(1)
u v w y x
Topological sort: example Connected components
v v 6/9
A connected component is a
w x
w x maximal connected subgraph
u u
2/5
of an undirected graph G
7/8
1/10
y Connected component are
y 3/4
the equivalence classes of
the connectivity relation
u v x w y (“is reachable from”)
output: u v x w y
Copyright © Università Telematica Internazionale UNINETTUNO
6
ALGORITHMS AND DATA STRUCTURES
Prof. Massimo Poncino
Lez 19 - Application of Depth-First-Search
Detecting connected Detecting connected
components components
Given an undirected graph Given an undirected graph
G(V,E) with n=|V| G(V,E) with n=|V|
Run DFS from a source node Otherwise, repeat DFS from
unvisited nodes
If the DFS tree contains n
vertices ¨ all the graph is
a single CC The DFS forest contains as
many trees as there are CCs
Detecting connected Strongly connected
components components
Algorithm Referred to directed graphs
Modify DFS so that each
vertex is properly labeled Strongly connected components
are the equivalence classes
Cost Ο(V+E) of the strong connectivity
relation (“there exists a
path between”)
Detecting strongly Detecting strongly
connected components connected components
Algorithm based on (G)
SCC(
SCC
SCC(G)
transpose graph DFS(G)
DFS(G)
Given a directed graph
G(V,E), the transpose graph Build tranpose graph GT
GT(V,ET) is obtained by DFS(GT) processing vertices
DFS(G
reversing the direction of in decreasing order of f[v]
f[v]
the edges
Return vertices of each DFS
ETT = {(u, v) : (v, u) ∈ E} tree as a distinct SCC
Copyright © Università Telematica Internazionale UNINETTUNO
7
ALGORITHMS AND DATA STRUCTURES
Prof. Massimo Poncino
Lez 19 - Application of Depth-First-Search
Detecting strongly connected Detecting strongly connected
components: example components: example
Step 1: DFS(G) Step 2: Build transpose graph
0/15 1/14 2/11 9/10
0 1 2 3 0 1 2 3
G G
4 5 6 7 4 5 6 7
12/13 6/7 3/8 4/5
Detecting strongly connected Detecting strongly connected
components: example components: example
Step 3: DFS(GT), processing
Step 2: Build transpose graph vertices in DFS(G) order
0 1 2 3
GT
0 1 2 3
4 5 6 7
GT
4 5 6 7
v 0 1 2 3 4 5 6 7
f[v] 15 14 11 10 13 7 8 5
Detecting strongly connected Detecting strongly connected
components: example components: example
Step 3/4 Step 3/4
0 1 2 3 0 1 2 3
GT GT
4 5 6 7 4 5 6 7
v 0 1 2 3 4 5 6 7 v 0 1 2 3 4 5 6 7
f[v] 15 14 11 10 13 7 8 5 f[v] 15 14 11 10 13 7 8 5
Copyright © Università Telematica Internazionale UNINETTUNO
8
ALGORITHMS AND DATA STRUCTURES
Prof. Massimo Poncino
Lez 19 - Application of Depth-First-Search
Detecting strongly connected Detecting strongly connected
components: example components: example
Step 3/4 Step 3/4
0 1 2 3 0 1 2 3
GT GT
4 5 6 7 4 5 6 7
v 0 1 2 3 4 5 6 7 v 0 1 2 3 4 5 6 7
f[v] 15 14 11 10 13 7 8 5 f[v] 15 14 11 10 13 7 8 5
Detecting strongly connected Detecting strongly connected
components: example components: example
Step 3/4 Step 3/4
0 1 2 3 0 1 2 3
GT GT
4 5 6 7 4 5 6 7
v 0 1 2 3 4 5 6 7 v 0 1 2 3 4 5 6 7
f[v] 15 14 11 10 13 7 8 5 f[v] 15 14 11 10 13 7 8 5
Detecting strongly connected Detecting strongly connected
components: example components: example
Step 3/4 Step 3/4
0 1 2 3 0 1 2 3
GT GT
4 5 6 7 4 5 6 7
v 0 1 2 3 4 5 6 7 v 0 1 2 3 4 5 6 7
f[v] 15 14 11 10 13 7 8 5 f[v] 15 14 11 10 13 7 8 5
Copyright © Università Telematica Internazionale UNINETTUNO
9
ALGORITHMS AND DATA STRUCTURES
Prof. Massimo Poncino
Lez 19 - Application of Depth-First-Search
Detecting strongly Detecting strongly
connected components connected components
Complexity Transpose graph in a
Two DFS visits Θ(V+E) adjacency list representation
Construction of Transpose(G)
transpose graph ? for u = 1 to |V|
foreach v ∈ Adj[u]
Θ(E)
Depends on Insert u in
implementation front of Adj[v]
Detecting strongly
connected components
Complexity
Two DFS visits Θ (V+E)
Θ(V+E)
Construction of
transpose graph Θ (E)
Θ(E) Algorithms
Overall: Θ (V+E)
Θ(V+E)
Copyright © Università Telematica Internazionale UNINETTUNO
10