Graph Traversal DFS
Graph Traversal DFS
x y z
s t
w v u
Depth-first search: Example
x y z
1
s t
w v u
Depth-first search: Example
x y z
1
s t
w v u
Depth-first search: Example
x y z
1
s t
w v u
Depth-first search: Example
x y z
1
s t
w v u
Depth-first search: Example
x y z
1
s t
3 4
w v u
Depth-first search: Example
x y z
1
s t
3 4 5
w v u
Depth-first search: Example
x y z
1
s t
3 4 5 6
w v u
Depth-first search: Example
x y z
1
s t
2 7
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8
s t
2 7
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8
s t
2 7
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8
s t
2 7 9
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8
s t
2 7 9
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 8 11
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 12 8 11
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6 14
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6 14
w v u
Depth-first search: Example
x y z
1 12 8 11 13
s t
2 7 9 10
3 4 5 6 14 15
w v u
Depth-first search: Example
x y z
1 12 8 11 13 16
s t
2 7 9 10
3 4 5 6 14 15
w v u
Depth-first search: DFT and DFF
By keeping track of parents, we want to construct a forest resulting
from the DFS traversal.
x y z
1 12 8 11 13 16
s t
2 7 9 10
3 4 5 6 14 15
w v u y
z
1 12 8 11 13 16
x t
s
2 7 9 10
3 4 5 6 14 15
w v u
Theorem: When DFS(s) is called on a node s, no recursive
calls will be made on nodes not reachable from s.
Proof: By contradiction; assume a recursive call is made on at
least one node not reachable from s.
There must be a first node v visited this way; v can't be s, since s
is trivially reachable from itself.
Thus DFS(v) must have been recursively invoked by DFS(u) for
some node u ≠ v, which in turn called DFS(v).
This means edge (u, v) must exist. Now, we consider two cases:
– Case 1: u is reachable from s. But then v is reachable from s,
because we can take the path from s to u and follow edge (u,
v).
– Case 2: u is not reachable from s. But then v was not the first
node not reachable from s to have DFS called on it.
In either case, we reach a contradiction, so our assumption was
wrong. Thus DFS(s) never makes recursive calls on nodes not
reachable from s.
Parenthesis Theorem
In DFS of graph, for all u, v, exactly one of the following holds:
1. Interval (d[u], f [u]) or ( d[v],f [v]) are disjoint and neither u nor v is a descendant of
the other.
2. Interval (d[u], f [u]) is contained within interval ( d[v],f [v]) and u is a descendant of
v.
3. Interval ( d[v],f [v]) is contained within interval (d[u], f [u] and v is a descendant of u.
Proof: Consider d[u] < d[v]. Two subcases - d[v] < f [ u] or not.
The first subcase d[v] < f [u]: so v was discovered while u was still gray,
implies v is a descendant of u. Also v was discovered more recently than u, as
all of its outgoing
edges are explored and is finished, before the search returns to and finishes
u. Hence, interval ( d[v],f [v] is entirely contained within the interval (d[u], f
[u]).
Other subcase, f[u] < d[v], and d[u] < f [u].
So, d[u] < f [u] < d[v] < f [v]; interval (d[u], f [u],) or ( d[v],f [v]) are disjoint.
Because the intervals are disjoint, neither vertex was discovered while the
other
was gray, and so neither vertex is a descendant of the other.
The case in which : d[v] < d[u] is similar.
Example (Parenthesis Theorem)
y z s t
3/6 2/9 1/10 11/16
B F C B
(s (z (y (x x) y) (w w) z) s) (t (v v) (u u) t)
Classification of Edges
• Consider edge (u,v) in directed graph G = (V,E) w.r.t.
DFS forest
d c f
tree back
4/5 3/6 10/11
35
Theorem: A graph G (directed or not) contains a cycle
if and only if a DFS of G yields a back edge.
• Assume G contains a cycle. Let v be the first vertex
reached on the cycle by a DFS of G. All the vertices
reachable from v will be explored from v, including the
vertex u that is just “before” v in the cycle. Since v is
an ancestor of u, the edge (u,v) will be a back edge.
• Say the DFS results in a back edge from u to v.
Clearly, u→v ( “there is a path from u to v”, or “v is
reachable from u”). And since v is an ancestor of u (by
def of back edge), v→u . So v and u must be part of a
cycle.
Back Edge Detection
370
• Example: 142 341
Valid order
C
A
A B E C D F
Topological sort : Algorithm 1
• Step 1: Identify vertices that have no incoming
edges
– The “In-degree” of these vertices is zero.
B
C
A
D E
Topological sort : Algorithm 1
• Step 1: Identify vertices that have no incoming
edges
– If no such edges, graph has cycles (cyclic graph).
B
C
A
Example of a cyclic
graph: No vertex of
In-degree 0
D
Topological sort : Algorithm 1
• Step 2: Delete this vertex of in-degree 0 and all
its outgoing edges from the graph. Place it in
the output.
B
C
A
D E
Topological sort : Algorithm 1
• Repeat Step1 and Step 2 until graph is empty.
C
A
D E
Topological sort : Algorithm 1
• Repeat Step1 and Step 2 until graph is empty.
C
A B
D E
Topological sort : Algorithm 1
• Repeat Step1 and Step 2 until graph is empty.
C
A B F
D E
Topological sort : Algorithm 1
• Repeat Step1 and Step 2 until graph is empty.
A B F C D E
Summary of Topological Sort
Algorithm B
• Problem:
– Need a faster way to find vertices with in-degree 0
instead of searching through entire in-degree array
Topological sort: Improvement
• Key Idea: Initialize and maintain a queue (or
stack) of vertices with In-degree 0
Queue A F
C
A
F
In-Degree
D E array
Topological sort: Improvement
After each vertex is output, when updating In-degree
array, enqueue any vertex whose In-degree has become
zero. F B
Queue
Dequeue Enqueue
Output A
B
C
A
F
In-Degree
D E array
Topological Sort Algorithm 1:
Analysis
• For input graph G(V,E), Run time =??
• Break down into total time required to:
– Initialize In-degree array: O(|E|)
– Initialize Queue with In-degree 0 vertices: O(|V|)
– Dequeue and output vertex:
• |V| vertices, each takes only O(1) to dequeue and output.
• So total time = O(|V|)
– Reduce In-degree of all vertices adjacent to a vertex
and Enqueue any In-Degree 0 vertices: O(|E|)
– Total time =O(|V| + |E|) Linear running time!
Topological sort: Algorithm 2
• The algorithm
– Run DFS(G)
– When a vertex is finished, output it
– Vertices are output in the reverse topologically
sorted order
• Runs in O(V+E) time – a linear time
algorithm