Title: Breadth-First Search
1Breadth-First Search
- Text
- Read Weiss, 9.3 (pp. 299-304) Breadth-First
Search Algorithms -
2Requirements
- 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
3Overview
- Breadth-first search starts with given node
F
C
A
B
D
H
0
E
G
Task Conduct a breadth-first search of the graph
starting with node D
4Overview
- Breadth-first search starts with given node
- Then visits nodes adjacent in some specified
order (e.g., alphabetical) - Like ripples in a pond
F
C
A
B
D
H
0
E
G
1
Nodes visited D
5Overview
- Breadth-first search starts with given node
- Then visits nodes adjacent in some specified
order (e.g., alphabetical) - Like ripples in a pond
F
C
A
B
D
H
0
E
G
1
Nodes visited D, C
6Overview
- Breadth-first search starts with given node
- Then visits nodes adjacent in some specified
order (e.g., alphabetical) - Like ripples in a pond
F
C
A
B
D
H
0
E
G
1
Nodes visited D, C, E
7Overview
- Breadth-first search starts with given node
- Then visits nodes adjacent in some specified
order (e.g., alphabetical) - Like ripples in a pond
F
C
A
B
D
H
0
E
G
1
Nodes visited D, C, E, F
8Overview
- When all nodes in ripple are visited, visit nodes
in next ripples
F
C
A
B
D
H
0
E
G
1
2
Nodes visited D, C, E, F, G
9Overview
- When all nodes in ripple are visited, visit nodes
in next ripples
F
C
A
B
D
H
0
3
E
G
1
2
Nodes visited D, C, E, F, G, H
10Overview
- When all nodes in ripple are visited, visit nodes
in next ripples
4
F
C
A
B
D
H
0
3
E
G
1
2
Nodes visited D, C, E, F, G, H, A
11Overview
- When all nodes in ripple are visited, visit nodes
in next ripples
4
F
C
A
B
D
H
0
3
E
G
1
2
Nodes visited D, C, E, F, G, H, A, B
12Walk-Through
Enqueued Array
F
C
A
B
C
D
E
F
G
H
A
Q ?
B
D
H
E
G
How is this accomplished? Simply replace the
stack with a queue! Rules (1) Maintain an
enqueued array. (2) Visit node when dequeued.
13Walk-Through
Enqueued Array
F
C
A
B
C
D v
E
F
G
H
A
Q ? D
B
D
H
E
G
Nodes visited
Enqueue D. Notice, D not yet visited.
14Walk-Through
Enqueued Array
F
C
A
B
C v
D v
E v
F v
G
H
A
Q ? C ? E ? F
B
D
H
E
G
Nodes visited D
Dequeue D. Visit D. Enqueue unenqueued nodes
adjacent to D.
15Walk-Through
Enqueued Array
F
C
A
B
C v
D v
E v
F v
G
H
A
Q ? E ? F
B
D
H
E
G
Nodes visited D, C
Dequeue C. Visit C. Enqueue unenqueued nodes
adjacent to C.
16Walk-Through
Enqueued Array
F
C
A
B
C v
D v
E v
F v
G
H
A
Q ? F ? G
B
D
H
E
G
Nodes visited D, C, E
Dequeue E. Visit E. Enqueue unenqueued nodes
adjacent to E.
17Walk-Through
Enqueued Array
F
C
A
B
C v
D v
E v
F v
G v
H
A
Q ? G
B
D
H
E
G
Nodes visited D, C, E, F
Dequeue F. Visit F. Enqueue unenqueued nodes
adjacent to F.
18Walk-Through
Enqueued Array
F
C
A
B
C v
D v
E v
F v
G v
H v
A
Q ? H
B
D
H
E
G
Nodes visited D, C, E, F, G
Dequeue G. Visit G. Enqueue unenqueued nodes
adjacent to G.
19Walk-Through
Enqueued Array
F
C
A v
B v
C v
D v
E v
F v
G v
H v
A
Q ? A ? B
B
D
H
E
G
Nodes visited D, C, E, F, G, H
Dequeue H. Visit H. Enqueue unenqueued nodes
adjacent to H.
20Walk-Through
Enqueued Array
F
C
A v
B v
C v
D v
E v
F v
G v
H v
A
Q ? B
B
D
H
E
G
Nodes visited D, C, E, F, G, H, A
Dequeue A. Visit A. Enqueue unenqueued nodes
adjacent to A.
21Walk-Through
Enqueued Array
F
C
A v
B v
C v
D v
E v
F v
G v
H v
A
Q empty
B
D
H
E
G
Nodes visited D, C, E, F, G, H, A, B
Dequeue B. Visit B. Enqueue unenqueued nodes
adjacent to B.
22Walk-Through
Enqueued Array
F
C
A v
B v
C v
D v
E v
F v
G v
H v
A
Q empty
B
D
H
E
G
Nodes visited D, C, E, F, G, H, A, B
Q empty. Algorithm done.
23Consider Trees
- What do we call a breadth-first traversal on
trees?
24(No Transcript)