Graph Traversal: Depth-First Search
Graph Traversal: Depth-First Search
Depth-First Search
Lecturer
Department of Computer Science and Engineering
Islamic University of Technology
Problem At Hand
“Exploring” a graph
“Exploring” a graph
s t
“Exploring” a graph
s t
“Exploring” a graph
s t
Possible Problems
Finding a way out
Exploring all possible paths
MBH (CSE, IUT) Graph Traversal 2 / 13
Solution Setup
c d k t
b e i m
a g h l
s f j n
c d k t
b e i m
a g h l
s f j n
Depth-First Search
Depth-First Search
s a b
c d e
parent
s a b c d e
stack
s a b
c d e
s a b
c d e
s a b
c d e
s a b
c d e
s a b
c d e
s a b
c d e
Claim
A graph G has a cycle ⇐⇒ DFS has a back edge
Claim
A graph G has a cycle ⇐⇒ DFS has a back edge
Proof:
Claim
A graph G has a cycle ⇐⇒ DFS has a back edge
Proof:
Consider that, our graph has a back-edge (ca).
a b c
Claim
A graph G has a cycle ⇐⇒ DFS has a back edge
Proof:
Consider that, our graph has a back-edge (ca).
a b c
=⇒ c must be descendant of a
Claim
A graph G has a cycle ⇐⇒ DFS has a back edge
Proof:
Consider that, our graph has a back-edge (ca).
a b c
=⇒ c must be descendant of a
=⇒ There’s a path consisting of tree-edge(s) from a to c
Claim
A graph G has a cycle ⇐⇒ DFS has a back edge
Proof:
Consider that, our graph has a back-edge (ca).
a b c
=⇒ c must be descendant of a
=⇒ There’s a path consisting of tree-edge(s) from a to c
=⇒ Cycle
Claim
A graph G has a cycle ⇐⇒ DFS has a back edge
Proof:
Claim
A graph G has a cycle ⇐⇒ DFS has a back edge
Proof:
Consider that, our graph has a cycle (< v0 , v1 , . . . , vn , v0 >).
v2 v3
v1 v4
v0 vn
Claim
A graph G has a cycle ⇐⇒ DFS has a back edge
Proof:
Consider that, our graph has a cycle (< v0 , v1 , . . . , vn , v0 >).
v2 v3
v1 v4
v0 vn
Claim
A graph G has a cycle ⇐⇒ DFS has a back edge
Proof:
Consider that, our graph has a cycle (< v0 , v1 , . . . , vn , v0 >).
v2 v3
v1 v4
v0 vn
Claim
A graph G has a cycle ⇐⇒ DFS has a back edge
Proof:
Consider that, our graph has a cycle (< v0 , v1 , . . . , vn , v0 >).
v2 v3
v1 v4
v0 vn
Claim
A graph G has a cycle ⇐⇒ DFS has a back edge
Proof:
Consider that, our graph has a cycle (< v0 , v1 , . . . , vn , v0 >).
v2 v3
v1 v4
v0 vn
Claim
A graph G has a cycle ⇐⇒ DFS has a back edge
Proof:
Consider that, our graph has a cycle (< v0 , v1 , . . . , vn , v0 >).
v2 v3
v1 v4
v0 vn
Claim
A graph G has a cycle ⇐⇒ DFS has a back edge
Proof:
Consider that, our graph has a cycle (< v0 , v1 , . . . , vn , v0 >).
v2 v3
v1 v4
v0 vn
Algo
DS
SA DM
P&S
Topological Sort
Claim
For any edge, e = (u, v ), v finishes before u in DFS.
u v
Claim
For any edge, e = (u, v ), v finishes before u in DFS.
u v
Proof:
Claim
For any edge, e = (u, v ), v finishes before u in DFS.
u v
Proof:
Case 1: u starts before v
Claim
For any edge, e = (u, v ), v finishes before u in DFS.
u v
Proof:
Case 1: u starts before v
• DFS will visit v before finishing u
Claim
For any edge, e = (u, v ), v finishes before u in DFS.
u v
Proof:
Case 1: u starts before v
• DFS will visit v before finishing u
• v finishes before u
Claim
For any edge, e = (u, v ), v finishes before u in DFS.
u v
Proof:
Case 1: u starts before v
• DFS will visit v before finishing u
• v finishes before u
Case 2: v starts before u
Claim
For any edge, e = (u, v ), v finishes before u in DFS.
u v
Proof:
Case 1: u starts before v
• DFS will visit v before finishing u
• v finishes before u
Case 2: v starts before u
• Problem if we visit u before finishing v
Claim
For any edge, e = (u, v ), v finishes before u in DFS.
u v
Proof:
Case 1: u starts before v
• DFS will visit v before finishing u
• v finishes before u
Case 2: v starts before u
• Problem if we visit u before finishing v
• Can only happen when (v , u) exists
Claim
For any edge, e = (u, v ), v finishes before u in DFS.
u v
Proof:
Case 1: u starts before v
• DFS will visit v before finishing u
• v finishes before u
Case 2: v starts before u
• Problem if we visit u before finishing v
• Can only happen when (v , u) exists
• Will create a back-edge → can’t exists in DAG
Claim
For any edge, e = (u, v ), v finishes before u in DFS.
u v
Proof:
Case 1: u starts before v
• DFS will visit v before finishing u
• v finishes before u
Case 2: v starts before u
• Problem if we visit u before finishing v
• Can only happen when (v , u) exists
• Will create a back-edge → can’t exists in DAG
• v finishes before u