Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
Breadth-First Search 9/14/2004 2:42 PM
1
9/14/2004 2:42 PM Breadth-First Search 1
Breadth-First Search
CB
A
E
D
L0
L1
FL2
9/14/2004 2:42 PM Breadth-First Search 2
Outline and Reading
Breadth-first search (§6.3.3)
� Algorithm
� Example
� Properties
� Analysis
� Applications
DFS vs. BFS (§6.3.3)
� Comparison of applications
� Comparison of edge labels
9/14/2004 2:42 PM Breadth-First Search 3
Breadth-First Search
Breadth-first search (BFS) is a general technique for traversing a graph
A BFS traversal of a graph G � 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
BFS on a graph with nvertices and m edges takes O(n + m ) time
BFS can be further extended to solve other graph problems
� Find and report a path with the minimum number of edges between two given vertices
� Find a simple cycle, if there is one
9/14/2004 2:42 PM Breadth-First Search 4
9/14/2004 2:42 PM Breadth-First Search 5
Breadth-First SearchWith a Queue – visit only(in this example the graph is directed)
1
5
4
2 3
6
Visited: {1} to visit: {(1,2), (1,6)}
T = φ(1,2) - Is 2 visited?
Visited: {1,2} to visit: {(1,6), (2,4), (2,5)}
T = {(1,2)}
(1,6) - Is 6 visited?
Visited: {1,2,6} to visit: {(2,4), (2,5), (6,5)}
T = {(1,2), (1,6)}
9/14/2004 2:42 PM Breadth-First Search 6
(2,4) - Is 4 visited?
Visited: {1,2,6,4} to visit: {(2,5), (6,5), (4,5), (4,3)}
T = {(1,2), (1,6), (2,4)}
(2,5) - Is 5 visited ?
Visited: {1,2,6,4,5} to visit: {(6,5), (4,5), (4,3)}
T = {(1,2), (1,6), (2,4), (2,5)}
(6,5) - 5? already visited!
(4,5) - 5? already visited!
(4,3) - 3?
1
5
4
2 3
6
Breadth-First Search 9/14/2004 2:42 PM
2
9/14/2004 2:42 PM Breadth-First Search 7
1
5
4
2 3
6
T = {(1,2), (1,6), (2,4), (2,5)}
9/14/2004 2:42 PM Breadth-First Search 8
Complexity
Number of enqueue:
Number of dequeue:
O(m)
∑∈
=Vv
mvd 2)(
∑∈
=Vv
mvd 2)(
If non directed
9/14/2004 2:42 PM Breadth-First Search 9
Breadth-First Searchwith Labeling
9/14/2004 2:42 PM Breadth-First Search 10
Algorithm BFS(s): Input: A vertex s in a graph
Output: A labeling of the edges as “discovery” edges and “cross edges”
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+1else label e as a cross edge
i ← i + 1
9/14/2004 2:42 PM Breadth-First Search 11
BFS Algorithm again –more details
The algorithm uses a mechanism for setting and getting “labels” of vertices and edges
Algorithm BFS(G, s)
L0 ← new empty sequenceL0.insertLast(s)
setLabel(s, VISITED)
i ← 0while ¬Li.isEmpty()Li +1 ← new empty sequencefor all v ∈ Li.elements() for all e ∈ G.incidentEdges(v)if getLabel(e) = UNEXPLOREDw ← opposite(v,e)if getLabel(w) = UNEXPLOREDsetLabel(e, DISCOVERY)
setLabel(w, VISITED)
Li +1.insertLast(w)
else
setLabel(e, CROSS)
i ← i +1
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 Breadth-First Search 12
Example
CB
A
E
D
discovery edge
cross edge
A visited vertex
A unexplored vertex
unexplored edge
L0
L1
F
CB
A
E
D
L0
L1
F
CB
A
E
D
L0
L1
F
Breadth-First Search 9/14/2004 2:42 PM
3
9/14/2004 2:42 PM Breadth-First Search 13
Example (cont.)
CB
A
E
D
L0
L1
F
CB
A
E
D
L0
L1
FL2
CB
A
E
D
L0
L1
FL2
CB
A
E
D
L0
L1
FL2
9/14/2004 2:42 PM Breadth-First Search 14
Example (cont.)
CB
A
E
D
L0
L1
FL2
CB
A
E
D
L0
L1
FL2
CB
A
E
D
L0
L1
FL2
9/14/2004 2:42 PM Breadth-First Search 15
PropertiesNotation
Gs: connected component of s
Property 1BFS(G, s) visits all the vertices and edges of Gs
Property 2The discovery edges labeled by BFS(G, s) form a spanning tree Tsof Gs
Property 3For each vertex v in Li� The path of Ts from s to v has i
edges
� Every path from s to v in Gs has at least i edges
CB
A
E
D
L0
L1
FL2
CB
A
E
D
F
9/14/2004 2:42 PM Breadth-First Search 16
Analysis
Setting/getting a vertex/edge label takes O(1) time
Each vertex is labeled twice � once as UNEXPLORED
� once as VISITED
Each edge is labeled twice� once as UNEXPLORED
� once as DISCOVERY or CROSS
Each vertex is inserted once into a sequence LiMethod incidentEdges is called once for each vertex
BFS runs in O(n + m) time provided the graph is represented by the adjacency list structure
� Recall that ΣΣΣΣv deg(v) = 2m
9/14/2004 2:42 PM Breadth-First Search 17
Applications
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� 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
9/14/2004 2:42 PM Breadth-First Search 18
DFS vs. BFS
CB
A
E
D
L0
L1
FL2
CB
A
E
D
F
DFS BFS
√√√√Biconnected components√√√√Shortest paths
√√√√√√√√Spanning forest, connected components, paths, cycles
BFSDFSApplications
Breadth-First Search 9/14/2004 2:42 PM
4
9/14/2004 2:42 PM Breadth-First Search 19
DFS vs. BFS (cont.)
Back edge (v,w)
� w is an ancestor of v in the tree of discovery edges
Cross edge (v,w)
� w is in the same level as v or in the next level in the tree of discovery edges
CB
A
E
D
L0
L1
FL2
CB
A
E
D
F
DFS BFS