Breadth-First Search - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Breadth-First Search

Description:

Breadth-First Search Text Read Weiss, 9.3 (pp. 299-304) Breadth-First Search Algorithms Requirements Can be used to attempt to visit all nodes of a graph in a ... – PowerPoint PPT presentation

Number of Views:371
Avg rating:3.0/5.0
Slides: 25
Provided by: peopleCsC
Category:

less

Transcript and Presenter's Notes

Title: Breadth-First Search


1
Breadth-First Search
  • Text
  • Read Weiss, 9.3 (pp. 299-304) Breadth-First
    Search Algorithms

2
Requirements
  • 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
Overview
  • 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
4
Overview
  • 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
5
Overview
  • 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
6
Overview
  • 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
7
Overview
  • 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
8
Overview
  • 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
9
Overview
  • 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
10
Overview
  • 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
11
Overview
  • 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
12
Walk-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.
13
Walk-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.
14
Walk-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.
15
Walk-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.
16
Walk-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.
17
Walk-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.
18
Walk-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.
19
Walk-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.
20
Walk-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.
21
Walk-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.
22
Walk-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.
23
Consider Trees
  1. What do we call a breadth-first traversal on
    trees?

24
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com