Breadth-First Search Outline and Reading Breadth

Breadth-First Search
9/14/2004 2:42 PM
Outline and Reading
Breadth-first search (§6.3.3)
Breadth-First Search
L0
L1
A
B
C
D
DFS vs. BFS (§6.3.3)
L2
E
F
9/14/2004 2:42 PM
Algorithm
Example
Properties
Analysis
Applications
Breadth-First Search
1
Comparison of applications
Comparison of edge labels
9/14/2004 2:42 PM
Breadth-First Search
2
9/14/2004 2:42 PM
Breadth-First Search
4
Breadth-First Search
Breadth-first search
(BFS) is a general
technique for traversing
a graph
A BFS traversal of a
graph G
BFS on a graph with n
vertices and m edges
takes O(n + m ) time
BFS can be further
extended to solve other
graph problems
Visits all the vertices and
edges of G
Determines whether G is
connected
Computes the connected
components of G
Computes a spanning
forest of G
9/14/2004 2:42 PM
Find and report a path
with the minimum
number of edges
between two given
vertices
Find a simple cycle, if
there is one
Breadth-First Search
3
Breadth-First Search
5
6
With a Queue – visit only
4
1
(in this example the graph is directed)
2
6
4
1
2
Visited: {1}
T=φ
(1,2) - Is 2 visited?
Visited: {1,2}
T = {(1,2)}
(1,6) - Is 6 visited?
Visited: {1,2,6}
T = {(1,2), (1,6)}
9/14/2004 2:42 PM
3
5
3
to visit: {(1,2), (1,6)}
to visit: {(1,6), (2,4), (2,5)}
(2,4) - Is 4 visited?
Visited: {1,2,6,4}
T = {(1,2), (1,6), (2,4)}
to visit: {(2,5), (6,5), (4,5), (4,3)}
(2,5) - Is 5 visited ?
Visited: {1,2,6,4,5}
T = {(1,2), (1,6), (2,4), (2,5)}
to visit: {(6,5), (4,5), (4,3)}
(6,5) - 5? already visited!
(4,5) - 5? already visited!
to visit: {(2,4), (2,5), (6,5)}
Breadth-First Search
(4,3) - 3?
5
9/14/2004 2:42 PM
Breadth-First Search
6
1
Breadth-First Search
9/14/2004 2:42 PM
Complexity
If non directed
5
6
4
1
2
Number of enqueue:
3
∑ d (v ) = 2 m
v∈V
Number of dequeue:
T = {(1,2), (1,6), (2,4), (2,5)}
∑ d (v ) = 2 m
v∈V
O(m)
9/14/2004 2:42 PM
Breadth-First Search
7
9/14/2004 2:42 PM
Breadth-First Search
8
Algorithm BFS(s): Input: A vertex s in a graph
Output: A labeling of the edges as “discovery” edges and “cross
edges”
Breadth-First Search
with Labeling
initialize L0 to contain vertex s
i←0
while Li is not empty do
create Li+1 to initially be empty
for each vertex v in Li do
if edge e incident on v do
let w be the other endpoint of e
if vertex w is unexplored then
label e as a discovery edge
insert w into Li+1
else label e as a cross edge
i←i+1
9/14/2004 2:42 PM
Breadth-First Search
9
9/14/2004 2:42 PM
Breadth-First Search
10
BFS Algorithm again –
Example
more details
The algorithm uses a
mechanism for setting and
getting “labels” of vertices
and edges
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)
9/14/2004 2:42 PM
Algorithm BFS(G, s)
L0 ← new empty sequence
L0.insertLast(s)
setLabel(s, VISITED)
i←0
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.insertLast(w)
else
setLabel(e, CROSS)
i ← i +1
Breadth-First Search
11
L0
unexplored vertex
visited vertex
unexplored edge
discovery edge
cross edge
A
A
L0
L1
L1
L0
C
E
9/14/2004 2:42 PM
B
D
C
E
A
B
A
L1
F
F
A
B
C
E
Breadth-First Search
D
D
F
12
2
Breadth-First Search
9/14/2004 2:42 PM
Example (cont.)
L0
L1
A
B
C
E
L0
L1
Example (cont.)
D
B
B
C
L2
L2
F
E
L0
C
E
L1
D
F
B
C
D
E
F
Breadth-First Search
13
Property 1
B
BFS(G, s) visits all the vertices and
edges of Gs
C
D
E
L0
L1
The path of Ts from s to v has i
edges
Every path from s to v in Gs has at
least i edges
Breadth-First Search
A
B
L2
C
E
D
F
15
E
F
A
C
D
E
F
Breadth-First Search
14
once as UNEXPLORED
once as VISITED
once as UNEXPLORED
once as DISCOVERY or CROSS
Recall that
Σv deg(v) = 2m
9/14/2004 2:42 PM
Breadth-First Search
16
DFS vs. BFS
Compute the connected components of G
Compute a spanning forest of G
Find a simple cycle in G, or report that G is a
forest
Given two vertices of G, find a path in G between
them with the minimum number of edges, or
report that no such path exists
Applications
Breadth-First Search
17
DFS
BFS
√
√
Spanning forest, connected
components, paths, cycles
√
Shortest paths
√
Biconnected components
L0
A
B
C
E
D
L1
F
9/14/2004 2:42 PM
A
B
L2
DFS
9/14/2004 2:42 PM
D
Each vertex is inserted once into a sequence Li
Method incidentEdges is called once for each vertex
BFS runs in O(n + m) time provided the graph is
represented by the adjacency list structure
Using the template method pattern, we can
specialize the BFS traversal of a graph G to
solve the following problems in O(n + m) time
C
Each edge is labeled twice
F
Applications
B
L2
F
9/14/2004 2:42 PM
The discovery edges labeled by
BFS(G, s) form a spanning tree Ts
of Gs
9/14/2004 2:42 PM
E
B
Property 2
D
A
Setting/getting a vertex/edge label takes O(1) time
Each vertex is labeled twice
A
Gs: connected component of s
L1
Analysis
Notation
For each vertex v in Li
L1
L2
Properties
Property 3
C
L0
A
L2
F
9/14/2004 2:42 PM
B
D
L0
A
A
L1
L1
A
L2
L0
L0
C
E
D
F
BFS
Breadth-First Search
18
3
Breadth-First Search
9/14/2004 2:42 PM
DFS vs. BFS (cont.)
Back edge (v,w)
Cross edge (v,w)
w is an ancestor of v in
the tree of discovery
edges
L0
A
B
C
E
D
L1
F
A
B
L2
DFS
9/14/2004 2:42 PM
w is in the same level as
v or in the next level in
the tree of discovery
edges
C
E
D
F
BFS
Breadth-First Search
19
4