Download presentation
Presentation is loading. Please wait.
Published byCori Morgan Modified over 8 years ago
1
Backtracking Algorithm Depth-First Search Text Read Weiss, § 9.6 Depth-First Search and § 10.5 Backtracking Algorithms
2
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
3
A H B F E D C G Walk-Through Visited Array A B C D E F G H Task: Conduct a depth-first search of the graph starting with node D
4
A H B F E D C G Walk-Through Visited Array A B C D √ E F G H Visit D D The order nodes are visited: D
5
A H B F E D C G Walk-Through Visited Array A B C D √ E F G H Consider nodes adjacent to D, decide to visit C first (Rule: visit adjacent nodes in alphabetical order) D The order nodes are visited: D
6
A H B F E D C G Walk-Through Visited Array A B C √ D √ E F G H Visit C CDCD The order nodes are visited: D, C
7
A H B F E D C G Walk-Through Visited Array A B C √ D √ E F G H No nodes adjacent to C; cannot continue backtrack, i.e., pop stack and restore previous state CDCD The order nodes are visited: D, C
8
A H B F E D C G Walk-Through Visited Array A B C √ D √ E F G H Back to D – C has been visited, decide to visit E next D The order nodes are visited: D, C
9
A H B F E D C G Walk-Through Visited Array A B C √ D √ E √ F G H Back to D – C has been visited, decide to visit E next EDED The order nodes are visited: D, C, E
10
A H B F E D C G Walk-Through Visited Array A B C √ D √ E √ F G H Only G is adjacent to E EDED The order nodes are visited: D, C, E
11
A H B F E D C G Walk-Through Visited Array A B C √ D √ E √ F G √ H Visit G GEDGED The order nodes are visited: D, C, E, G
12
A H B F E D C G Walk-Through Visited Array A B C √ D √ E √ F G √ H Nodes D and H are adjacent to G. D has already been visited. Decide to visit H. GEDGED The order nodes are visited: D, C, E, G
13
A H B F E D C G Walk-Through Visited Array A B C √ D √ E √ F G √ H √ Visit H HGEDHGED The order nodes are visited: D, C, E, G, H
14
A H B F E D C G Walk-Through Visited Array A B C √ D √ E √ F G √ H √ Nodes A and B are adjacent to F. Decide to visit A next. HGEDHGED The order nodes are visited: D, C, E, G, H
15
A H B F E D C G Walk-Through Visited Array A √ B C √ D √ E √ F G √ H √ Visit A AHGEDAHGED The order nodes are visited: D, C, E, G, H, A
16
A H B F E D C G Walk-Through Visited Array A √ B C √ D √ E √ F G √ H √ Only Node B is adjacent to A. Decide to visit B next. AHGEDAHGED The order nodes are visited: D, C, E, G, H, A
17
A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F G √ H √ Visit B BAHGEDBAHGED The order nodes are visited: D, C, E, G, H, A, B
18
A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F G √ H √ No unvisited nodes adjacent to B. Backtrack (pop the stack). AHGEDAHGED The order nodes are visited: D, C, E, G, H, A, B
19
A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F G √ H √ No unvisited nodes adjacent to A. Backtrack (pop the stack). HGEDHGED The order nodes are visited: D, C, E, G, H, A, B
20
A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F G √ H √ No unvisited nodes adjacent to H. Backtrack (pop the stack). GEDGED The order nodes are visited: D, C, E, G, H, A, B
21
A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F G √ H √ No unvisited nodes adjacent to G. Backtrack (pop the stack). EDED The order nodes are visited: D, C, E, G, H, A, B
22
A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F G √ H √ No unvisited nodes adjacent to E. Backtrack (pop the stack). D The order nodes are visited: D, C, E, G, H, A, B
23
A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F G √ H √ F is unvisited and is adjacent to D. Decide to visit F next. D The order nodes are visited: D, C, E, G, H, A, B
24
A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F √ G √ H √ Visit F FDFD The order nodes are visited: D, C, E, G, H, A, B, F
25
A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F √ G √ H √ No unvisited nodes adjacent to F. Backtrack. D The order nodes are visited: D, C, E, G, H, A, B, F
26
A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F √ G √ H √ No unvisited nodes adjacent to D. Backtrack. The order nodes are visited: D, C, E, G, H, A, B, F
27
A H B F E D C G Walk-Through Visited Array A √ B √ C √ D √ E √ F √ G √ H √ Stack is empty. Depth-first traversal is done. The order nodes are visited: D, C, E, G, H, A, B, F
28
Consider Trees 1.What depth-first traversals do you know? 2.How do the traversals differ? 3.In the walk-through, we visited a node just as we pushed the node onto the stack. Is there another time at which you can visit the node? 4.Conduct a depth-first search of the same graph using the strategy you came up with in #3.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.