Read Weiss, 9.6 Depth-First Search and 10.5 Backtracking Algorithms

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

Backtracking Algorithm

Depth-First Search
Text
• Read Weiss, § 9.6 Depth-First Search and § 10.5
Backtracking Algorithms
Requirements
• Also called Depth-First Search
• Can be used to attempt to visit all nodes of a
graph in a systematic manner
• Works with directed and undirected graphs
• Works with weighted and unweighted graphs
Walk-Through
Visited Array
F C A
A B
B C
D
H D
E
G E F
G
H

Task: Conduct a depth-first search of the


graph starting with node D
Walk-Through
Visited Array
F C A
A B
B C
D
H D √
E
G E F
G
H D
The order nodes are visited:
Visit D
D
Walk-Through
Visited Array
F C A
A B
B C
D
H D √
E
G E F
G
H D
The order nodes are visited:
Consider nodes adjacent to D,
D decide to visit C first (Rule:
visit adjacent nodes in
alphabetical order)
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E
G E F
G C
H D
The order nodes are visited:
Visit C
D, C
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E
G E F
G C
H D
The order nodes are visited:
No nodes adjacent to C; cannot
D, C continue  backtrack, i.e.,
pop stack and restore
previous state
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E
G E F
G
H D
The order nodes are visited:
Back to D – C has been visited,
D, C decide to visit E next
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E √
G E F
G E
H D
The order nodes are visited:
Back to D – C has been visited,
D, C, E decide to visit E next
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E √
G E F
G E
H D
The order nodes are visited:
Only G is adjacent to E
D, C, E
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E √
G E G
F
G √ E
H D
The order nodes are visited:
Visit G
D, C, E, G
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E √
G E G
F
G √ E
H D
The order nodes are visited:
Nodes D and H are adjacent to
D, C, E, G G. D has already been
visited. Decide to visit H.
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
Visit H
D, C, E, G, H
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
Nodes A and B are adjacent to F.
D, C, E, G, H Decide to visit A next.
Walk-Through
Visited Array
F C A √
A B
B C √ A
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
Visit A
D, C, E, G, H, A
Walk-Through
Visited Array
F C A √
A B
B C √ A
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
Only Node B is adjacent to A.
D, C, E, G, H, A Decide to visit B next.
Walk-Through
Visited Array
F C A √
A B √ B
B C √ A
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
Visit B
D, C, E, G, H, A, B
Walk-Through
Visited Array
F C A √
A B √
B C √ A
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B B. Backtrack (pop the stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
H
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B A. Backtrack (pop the stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E G
F
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B H. Backtrack (pop the
stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F
G √ E
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B G. Backtrack (pop the
stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F
G √
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B E. Backtrack (pop the stack).
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F
G √
H √ D
The order nodes are visited:
F is unvisited and is adjacent to
D, C, E, G, H, A, B D. Decide to visit F next.
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F √
G √ F
H √ D
The order nodes are visited:
Visit F
D, C, E, G, H, A, B, F
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F √
G √
H √ D
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B, F F. Backtrack.
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F √
G √
H √
The order nodes are visited:
No unvisited nodes adjacent to
D, C, E, G, H, A, B, F D. Backtrack.
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F √
G √
H √
The order nodes are visited:
Stack is empty. Depth-first
D, C, E, G, H, A, B, F traversal is done.

You might also like