Professional Documents
Culture Documents
Breadth-First Search: M) Time
Breadth-First Search: M) Time
Breadth-first search
(BFS) is a general
technique for traversing
a graph
A BFS traversal of a
graph G
Breadth-First Search
L0
L1
L2
Breadth-First Search
BFS Algorithm
Algorithm BFS(G)
Input graph G
Output labeling of the edges
and partition of the
vertices of G
for all u G.vertices()
setLabel(u, UNEXPLORED)
for all e G.edges()
setLabel(e, UNEXPLORED)
for all v G.vertices()
if getLabel(v) = UNEXPLORED
BFS(G, v)
2010 Goodrich, Tamassia
Breadth-First Search
Example
Algorithm BFS(G, s)
L0 new empty sequence
L0.addLast(s)
setLabel(s, VISITED)
i0
while Li.isEmpty()
Li +1 new empty sequence
for all v Li.elements()
for all e G.incidentEdges(v)
if getLabel(e) = UNEXPLORED
w opposite(v,e)
if getLabel(w) = UNEXPLORED
setLabel(e, DISCOVERY)
setLabel(w, VISITED)
Li +1.addLast(w)
else
setLabel(e, CROSS)
i i +1
Breadth-First Search
L0
unexplored vertex
visited vertex
unexplored edge
discovery edge
cross edge
A
A
L0
L1
L1
L0
C
E
2010 Goodrich, Tamassia
C
E
L1
C
E
Breadth-First Search
D
F
4
Example (cont.)
L0
L1
L0
C
E
L0
L1
Example (cont.)
L1
L2
L1
L0
L2
C
E
D
F
A
C
E
D
F
Breadth-First Search
Property 2
L0
L1
Breadth-First Search
L2
C
E
D
F
once as UNEXPLORED
once as DISCOVERY or CROSS
once as UNEXPLORED
once as VISITED
L1
Property 1
Analysis
L2
Breadth-First Search
Notation
L1
L0
L2
Properties
Property 3
L1
L2
L0
L2
L0
Recall that
Sv deg(v) = 2m
Breadth-First Search
Applications
w is an ancestor of v in
the tree of discovery
edges
L0
A
B
C
E
L1
DFS
2010 Goodrich, Tamassia
L2
A
C
E
D
F
BFS
Breadth-First Search
11
BFS
Biconnected components
L0
A
B
C
E
L1
L2
DFS
Breadth-First Search
DFS
C
E
D
F
BFS
Breadth-First Search
10