CSE 304
Design & Analysis of Algorithm
Lecture 3
DFS (Revisited) & Topological Sort
DFS(V, E)
1. for each u V u v w
2. do color[u] ← WHITE
3. prev[u] ← NIL x y z
4. time ← 0
5. for each u V
6. do if color[u] = WHITE
7. then DFS-VISIT(u)
• Every time DFS-VISIT(u) is called, u becomes the root
of a new tree in the depth-first forest
2
DFS-VISIT(u)
1. color[u] ← GRAY u v w
2. time ← time+1
3. d[u] ← time
x y z
4. for each v Adj[u] time = 1
5. do if color[v] = WHITE u v w
1/
6. then prev[v] ← u
7. DFS-VISIT(v) x y z
8. color[u] ← BLACK u v w
1/ 2/
9. time ← time + 1
10.f[u] ← time
x y z
3
Example
u v w u v w u v w
1/ 1/ 2/ 1/ 2/
3/
x y z x y z x y z
u v w u v w u v w
1/ 2/ 1/ 2/ 1/ 2/
B B
4/ 3/ 4/ 3/ 4/5 3/
x y z x y z x y z
u v w u v w u v w
1/ 2/ 1/ 2/7 1/ 2/7
B B F B
4/5 3/6 4/5 3/6 4/5 3/6
x y z x y z x y z
4
Example (cont.)
u v w u v w u v w
1/8 2/7 1/8 2/7 9/ 1/8 2/7 9/
C
F B F B F B
4/5 3/6 4/5 3/6 4/5 3/6
x y z x y z x y z
u v w u v w u v w
1/8 2/7 9/ 1/8 2/7 9/ 1/8 2/7 9/
C C C
F B F B F B
B B
4/5 3/6 10/ 4/5 3/6 10/ 4/5 3/6 10/11
x y z x y z x y z
u v w The results of DFS may depend on:
1/8 2/7 9/12 • The order in which nodes are explored
C
F B in procedure DFS
B
4/5 3/6 10/11 • The order in which the neighbors of a
x y z vertex are visited in DFS-VISIT
5
Edge Classification
• Tree edge (reaches a WHITE u v w
1/
vertex):
– (u, v) is a tree edge if v was first
discovered by exploring edge (u, v) x y z
• Back edge (reaches a GRAY
u v w
vertex): 1/ 2/
– (u, v), connecting a vertex u to an B
ancestor v in a depth first tree 4/ 3/
x y z
– Self loops (in directed graphs) are
also back edges
6
Edge Classification
• Forward edge (reaches a BLACK u v w
1/ 2/7
vertex & d[u] < d[v]): B
F
– Non-tree edges (u, v) that connect a vertex 4/5 3/6
u to a descendant v in a depth first tree x y z
• Cross edge (reaches a BLACK vertex
u v w
& d[u] > d[v]):
1/8 2/7 9/
C
– Can go between vertices in same depth-first F B
tree (as long as there is no ancestor / 4/5 3/6
descendant relation) or between different x y z
depth-first trees
7
Analysis of DFS(V, E)
1. for each u V
2. do color[u] ← WHITE (V)
3. [u] ← NIL
4. time ← 0
5. for each u V (V) – exclusive
6. do if color[u] = WHITE of time for
7. then DFS-VISIT(u) DFS-VISIT
8
Analysis of DFS-VISIT(u)
1. color[u] ← GRAY DFS-VISIT is called exactly
2. time ← time+1 once for each vertex
3. d[u] ← time
4. for each v Adj[u]
5. do if color[v] = WHITE Each loop takes
6. then [v] ← u |Adj[v]|
7. DFS-VISIT(v)
8. color[u] ← BLACK
9. time ← time + 1 Total: ΣvV |Adj[v]| + (V) = (V + E)
10.f[u] ← time (E)
9
Properties of DFS
• u = prev[v] DFS-VISIT(v) was
called during a search of u’s
u v w
adjacency list 1/ 2/
3/
x y z
• Vertex v is a descendant of vertex u
in the depth first forest v is
discovered during the time in which
u is gray 10
Parenthesis Theorem
y z s t
In any DFS of a graph G, for 3/6 2/9 1/10 11/16
all u, v, exactly one of the
4/5 7/8 12/13 14/15
following holds: x w v u
1. [d[u], f[u]] and [d[v], f[v]]
s t
are disjoint, and neither of u
and v is a descendant of the z v u
other y w
2. [d[v], f[v]] is entirely within x
[d[u], f[u]] and v is a
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
descendant of u (s (z (y (x x) y) (w w) z) s) (t (v u) (u u) t)
3. [d[u], f[u]] is entirely within Well-formed expression: parenthesis are
[d[v], f[v]] and u is a properly nested
11
descendant of v
Other Properties of DFS
Corollary
u
Vertex v is a proper descendant of u 1/8 2/7 9/12
C
d[u] < d[v] < f[v] < f[u] F B
B
4/5 3/6 10/11
v
Theorem (White-path Theorem)
In a depth-first forest of a graph G, vertex u
1/ 2/
v is a descendant of u if and only if at
time d[u], there is a path u v consisting
of only white vertices. v
12
Directed Acyclic Graph
• DAG – Directed graph with no cycles.
• Good for modeling processes and structures
that have a partial order:
– a > b and b > c a > c.
– But may have a and b such that neither a > b nor b >
a.
• Can always make a total order (either a > b or
b > a for all a b) from a partial order.
Characterizing a DAG
Lemma
Lemma22.11
22.11
AAdirected
directedgraph
graphGGisisacyclic
acycliciffiffaaDFS
DFSofofGGyields
yieldsno
noback
backedges.
edges.
T T T
v u
B
Topological Sort
Topological sort of a directed acyclic graph G =
(V, E): a linear order of vertices such that if there
exists an edge (u, v), then u appears before v
in the ordering.
• Directed acyclic graphs (DAGs)
– Used to represent precedence of events or processes
that have a partial order
a before b b before c What about
a before c
b before c a before c a and b?
Topological sort helps us establish a total order
15
Topological Sort
Want to “sort” a directed acyclic graph (DAG).
A B D
C E
A B C D E
Think of original DAG as a partial order.
Want a total order that extends this partial order.
Topological Sort - Application
• Application 1
– in scheduling a sequence of jobs.
– The jobs are represented by vertices,
– there is an edge from x to y if job x must be
completed before job y can be done
• (for example, washing machine must finish before we put the
clothes to dry). Then, a topological sort gives an order in
which to perform the jobs
• Application 2
– In open credit system, how to take courses (in order)
such that, pre-requisite of courses will not create any
problem
17
Topological Sort (Fig – Cormen)
undershorts 11/16 17/18 socks TOPOLOGICAL-SORT(V,
12/15
E)
pants shoes 13/14 1.Call DFS(V, E) to compute
shirt 1/8 finishing times f[v] for each
6/7 belt vertex v
watch 9/10
tie 2/ 5 2. When each vertex is finished,
insert it onto the front of a
jacket 3/4 linked list
3. Return the linked list of
vertices
socks undershorts pants shoes watch shirt belt tie jacket
Running time: (V + E)
18
Readings
• Cormen - Chapter 22
• Exercise:
– 22.4-2 : Number of paths (important)
– 22.4-3 : cycle (important and we have already solved
it)
– 22.4-5 : Topological sort using degree
19