0% found this document useful (0 votes)
65 views60 pages

Graph Traversal: Depth-First Search

The document discusses depth-first search (DFS) for graph traversal. It begins by framing the problem of exploring a graph and representing it as nodes and edges. It then presents the solution approach of DFS, which involves starting at a source node, following connections until a dead end is reached, backtracking to unexplored neighbors, and repeating until the target is found or the entire graph is explored. Key aspects of DFS include avoiding repeated nodes and classifying edges as tree, forward, back, or cross edges. The document proves that a graph contains a cycle if and only if DFS encounters a back edge during traversal. It provides examples and analyzes the time complexity of DFS as O(V+E) where V is
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views60 pages

Graph Traversal: Depth-First Search

The document discusses depth-first search (DFS) for graph traversal. It begins by framing the problem of exploring a graph and representing it as nodes and edges. It then presents the solution approach of DFS, which involves starting at a source node, following connections until a dead end is reached, backtracking to unexplored neighbors, and repeating until the target is found or the entire graph is explored. Key aspects of DFS include avoiding repeated nodes and classifying edges as tree, forward, back, or cross edges. The document proves that a graph contains a cycle if and only if DFS encounters a back edge during traversal. It provides examples and analyzes the time complexity of DFS as O(V+E) where V is
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

Graph Traversal

Depth-First Search

Md. Bakhtiar Hasan

Lecturer
Department of Computer Science and Engineering
Islamic University of Technology
Problem At Hand

“Exploring” a graph

MBH (CSE, IUT) Graph Traversal 2 / 13


Problem At Hand

“Exploring” a graph

s t

MBH (CSE, IUT) Graph Traversal 2 / 13


Problem At Hand

“Exploring” a graph

s t

MBH (CSE, IUT) Graph Traversal 2 / 13


Problem At Hand

“Exploring” a graph

s t

Possible Problems
Finding a way out
Exploring all possible paths
MBH (CSE, IUT) Graph Traversal 2 / 13
Solution Setup

Convert each junction to a node

c d k t

b e i m

a g h l

s f j n

MBH (CSE, IUT) Graph Traversal 3 / 13


Solution Setup

Convert each junction to a node


Create edge for connected junctions

c d k t

b e i m

a g h l

s f j n

MBH (CSE, IUT) Graph Traversal 3 / 13


Solution Idea

Start from source/start point

MBH (CSE, IUT) Graph Traversal 4 / 13


Solution Idea

Start from source/start point


Follow junctions, leaving breadcrumbs, till we reach dead-end

MBH (CSE, IUT) Graph Traversal 4 / 13


Solution Idea

Start from source/start point


Follow junctions, leaving breadcrumbs, till we reach dead-end
Backtrack along breadcrumbs until we reach unexplored neighbor

MBH (CSE, IUT) Graph Traversal 4 / 13


Solution Idea

Start from source/start point


Follow junctions, leaving breadcrumbs, till we reach dead-end
Backtrack along breadcrumbs until we reach unexplored neighbor
Explore those neighbors

MBH (CSE, IUT) Graph Traversal 4 / 13


Solution Idea

Start from source/start point


Follow junctions, leaving breadcrumbs, till we reach dead-end
Backtrack along breadcrumbs until we reach unexplored neighbor
Explore those neighbors
Avoid repeating junction

MBH (CSE, IUT) Graph Traversal 4 / 13


Solution Idea

Start from source/start point


Follow junctions, leaving breadcrumbs, till we reach dead-end
Backtrack along breadcrumbs until we reach unexplored neighbor
Explore those neighbors
Avoid repeating junction
Until we find the target/finish point

MBH (CSE, IUT) Graph Traversal 4 / 13


Solution Idea

Start from source/start point


Follow junctions, leaving breadcrumbs, till we reach dead-end
Backtrack along breadcrumbs until we reach unexplored neighbor
Explore those neighbors
Avoid repeating junction
Until we find the target/finish point

Depth-First Search

MBH (CSE, IUT) Graph Traversal 4 / 13


Solution Idea

Start from source/start point


Follow junctions, leaving breadcrumbs, till we reach dead-end
Backtrack along breadcrumbs until we reach unexplored neighbor
Explore those neighbors
Avoid repeating junction
Until we find the target/finish point
(Optional) Check and explore if any other junction is not explored

Depth-First Search

MBH (CSE, IUT) Graph Traversal 4 / 13


Example

Assume, adjL is sorted in alphabetical order

s a b

c d e

parent
s a b c d e

stack

MBH (CSE, IUT) Graph Traversal 5 / 13


Complexity

Traverse every vertex once → V

MBH (CSE, IUT) Graph Traversal 6 / 13


Complexity

Traverse every vertex once → V


From each vertex v , traverse adjL[v]
From the Handshaking
 Lemma,
P E (Directed Graph)
adjL[v ] =
v ∈V 2E (Undirected Graph)

MBH (CSE, IUT) Graph Traversal 6 / 13


Complexity

Traverse every vertex once → V


From each vertex v , traverse adjL[v]
From the Handshaking
 Lemma,
P E (Directed Graph)
adjL[v ] =
v ∈V 2E (Undirected Graph)
Time complexity: O(V + E )

MBH (CSE, IUT) Graph Traversal 6 / 13


Edge Classification

Tree Edge: Created by traversing child-parent. Used to visit new


vertex

s a b

c d e

MBH (CSE, IUT) Graph Traversal 7 / 13


Edge Classification

Tree Edge: Created by traversing child-parent. Used to visit new


vertex
Non-Tree Edge:

s a b

c d e

MBH (CSE, IUT) Graph Traversal 7 / 13


Edge Classification

Tree Edge: Created by traversing child-parent. Used to visit new


vertex
Non-Tree Edge:
• Forward Edge: Ancestor → Descendant

s a b

c d e

MBH (CSE, IUT) Graph Traversal 7 / 13


Edge Classification

Tree Edge: Created by traversing child-parent. Used to visit new


vertex
Non-Tree Edge:
• Forward Edge: Ancestor → Descendant
• Back Edge: Descendant → Ancestor

s a b

c d e

MBH (CSE, IUT) Graph Traversal 7 / 13


Edge Classification

Tree Edge: Created by traversing child-parent. Used to visit new


vertex
Non-Tree Edge:
• Forward Edge: Ancestor → Descendant
• Back Edge: Descendant → Ancestor
• Cross Edge: Between two non-ancestor related subtree

s a b

c d e

MBH (CSE, IUT) Graph Traversal 7 / 13


Edge Classification

Tree Edge: Created by traversing child-parent. Used to visit new


vertex
Non-Tree Edge:
• Forward Edge: Ancestor → Descendant
• Back Edge: Descendant → Ancestor
• Cross Edge: Between two non-ancestor related subtree
Undirected graph → Tree Edge + Back Edge

s a b

c d e

MBH (CSE, IUT) Graph Traversal 7 / 13


Cycle Detection

Claim
A graph G has a cycle ⇐⇒ DFS has a back edge

MBH (CSE, IUT) Graph Traversal 8 / 13


Cycle Detection

Claim
A graph G has a cycle ⇐⇒ DFS has a back edge

Proof:

MBH (CSE, IUT) Graph Traversal 8 / 13


Cycle Detection

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

MBH (CSE, IUT) Graph Traversal 8 / 13


Cycle Detection

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

MBH (CSE, IUT) Graph Traversal 8 / 13


Cycle Detection

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

MBH (CSE, IUT) Graph Traversal 8 / 13


Cycle Detection

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

MBH (CSE, IUT) Graph Traversal 8 / 13


Cycle Detection

Claim
A graph G has a cycle ⇐⇒ DFS has a back edge

Proof:

MBH (CSE, IUT) Graph Traversal 9 / 13


Cycle Detection

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

MBH (CSE, IUT) Graph Traversal 9 / 13


Cycle Detection

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

Let, v0 be the first vertex visited in the cycle

MBH (CSE, IUT) Graph Traversal 9 / 13


Cycle Detection

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

Let, v0 be the first vertex visited in the cycle


v1 will finish before finishing v0

MBH (CSE, IUT) Graph Traversal 9 / 13


Cycle Detection

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

Let, v0 be the first vertex visited in the cycle


v1 will finish before finishing v0
v2 will finish before finishing v1 and v0

MBH (CSE, IUT) Graph Traversal 9 / 13


Cycle Detection

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

Let, v0 be the first vertex visited in the cycle


v1 will finish before finishing v0
v2 will finish before finishing v1 and v0
...

MBH (CSE, IUT) Graph Traversal 9 / 13


Cycle Detection

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

Let, v0 be the first vertex visited in the cycle


v1 will finish before finishing v0
v2 will finish before finishing v1 and v0
...
vn will finish before finishing vn−1 , . . . , v0

MBH (CSE, IUT) Graph Traversal 9 / 13


Cycle Detection

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

Let, v0 be the first vertex visited in the cycle


v1 will finish before finishing v0
v2 will finish before finishing v1 and v0
...
vn will finish before finishing vn−1 , . . . , v0
v0 is an ancestor of vn → vn , v0 is a back-edge
MBH (CSE, IUT) Graph Traversal 9 / 13
Job Scheduling

Directed Acyclic Graph (DAG)

MBH (CSE, IUT) Graph Traversal 10 / 13


Job Scheduling

Directed Acyclic Graph (DAG)


Vertices → Task
Edge → Dependency

MBH (CSE, IUT) Graph Traversal 10 / 13


Job Scheduling

Directed Acyclic Graph (DAG)


Vertices → Task
Edge → Dependency
Order task without violating the dependencies

MBH (CSE, IUT) Graph Traversal 10 / 13


Job Scheduling

Directed Acyclic Graph (DAG)


Vertices → Task
Edge → Dependency
Order task without violating the dependencies

SP-I SP-II DBMS

Algo
DS
SA DM

P&S

MBH (CSE, IUT) Graph Traversal 10 / 13


Job Scheduling

Run DFS on any vertex

MBH (CSE, IUT) Graph Traversal 11 / 13


Job Scheduling

Run DFS on any vertex


Push vertex in stack when finished processing

MBH (CSE, IUT) Graph Traversal 11 / 13


Job Scheduling

Run DFS on any vertex


Push vertex in stack when finished processing
Avoid repeating vertices

MBH (CSE, IUT) Graph Traversal 11 / 13


Job Scheduling

Run DFS on any vertex


Push vertex in stack when finished processing
Avoid repeating vertices
Continue running DFS until all vertices are visited

MBH (CSE, IUT) Graph Traversal 11 / 13


Job Scheduling

Run DFS on any vertex


Push vertex in stack when finished processing
Avoid repeating vertices
Continue running DFS until all vertices are visited
Pop elements from the stack and print → Task Ordering

MBH (CSE, IUT) Graph Traversal 11 / 13


Job Scheduling

Run DFS on any vertex


Push vertex in stack when finished processing
Avoid repeating vertices
Continue running DFS until all vertices are visited
Pop elements from the stack and print → Task Ordering

Topological Sort

MBH (CSE, IUT) Graph Traversal 11 / 13


Job Scheduling

Claim
For any edge, e = (u, v ), v finishes before u in DFS.

u v

MBH (CSE, IUT) Graph Traversal 12 / 13


Job Scheduling

Claim
For any edge, e = (u, v ), v finishes before u in DFS.

u v

Proof:

MBH (CSE, IUT) Graph Traversal 12 / 13


Job Scheduling

Claim
For any edge, e = (u, v ), v finishes before u in DFS.

u v

Proof:
Case 1: u starts before v

MBH (CSE, IUT) Graph Traversal 12 / 13


Job Scheduling

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

MBH (CSE, IUT) Graph Traversal 12 / 13


Job Scheduling

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

MBH (CSE, IUT) Graph Traversal 12 / 13


Job Scheduling

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

MBH (CSE, IUT) Graph Traversal 12 / 13


Job Scheduling

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

MBH (CSE, IUT) Graph Traversal 12 / 13


Job Scheduling

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

MBH (CSE, IUT) Graph Traversal 12 / 13


Job Scheduling

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

MBH (CSE, IUT) Graph Traversal 12 / 13


Job Scheduling

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

MBH (CSE, IUT) Graph Traversal 12 / 13


Reading

CLRS 22.3, 22.4


Dasgupta 3.2, 3.3
Jeff Erickson 5.6, 5.7, 6.1-6.3, 6.5, 8.5

MBH (CSE, IUT) Graph Traversal 13 / 13

You might also like