# Breadth-First Search by TIQp06x7

VIEWS: 6 PAGES: 12

• pg 1
```									  Breadth-First Search
L0
A

L1
B            C       D

L2
E       F

2/29/2012 3:22 AM   Breadth-First Search                   1
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

2/29/2012 3:22 AM        Breadth-First Search   2
Breadth-First Search
Breadth-first search                       BFS on a graph with n
(BFS) is a general                         vertices and m edges
technique for traversing                   takes O(n + m ) time
a graph
BFS can be further
A BFS traversal of a
graph G                                    extended to solve other
   Visits all the vertices and           graph problems
edges of G                                Find and report a path
   Determines whether G is                    with the minimum
connected                                  number of edges
   Computes the connected                     between two given
components of G                            vertices
   Computes a spanning                       Find a simple cycle, if
forest of G                                there is one
2/29/2012 3:22 AM             Breadth-First Search                             3
Example
L0
A
A        unexplored vertex
A        visited vertex                   L1
B       C           D
unexplored edge
discovery edge                             E       F
cross edge

L0                                         L0
A                                         A

L1                                             L1
B          C        D                      B        C           D

E        F                                E       F

2/29/2012 3:22 AM           Breadth-First Search                     4
Example (cont.)
L0                                          L0
A                                        A

L1                                            L1
B           C         D                     B       C           D

L2
E        F                               E       F

L0                                          L0
A                                        A

L1                                            L1
B           C         D                     B       C           D

L2                                           L2
E        F                               E       F

2/29/2012 3:22 AM           Breadth-First Search                    5
Example (cont.)
L0                                           L0
A                                         A

L1                                          L1
B            C        D                      B       C       D

L2                                            L2
E         F                               E       F

L0
A

L1
B            C        D

L2
E         F

2/29/2012 3:22 AM           Breadth-First Search                    6
BFS Algorithm
The algorithm uses a               Algorithm BFS(G, s)
mechanism for setting and            L0  new empty sequence
getting “labels” of vertices         L0.insertLast(s)
and edges                            setLabel(s, VISITED)
i0
Algorithm BFS_all(G)                    while Li.isEmpty()
Input graph G                           Li +1  new empty sequence
Output labeling of the edges            for all v  Li.elements()
and partition of the                   for all e  G.incidentEdges(v)
vertices of G                             if getLabel(e) = UNEXPLORED
for all u  G.vertices()                          w  opposite(v,e)
setLabel(u, UNEXPLORED)                          if getLabel(w) = UNEXPLORED
for all e  G.edges()                                setLabel(e, DISCOVERY)
setLabel(w, VISITED)
setLabel(e, UNEXPLORED)
Li +1.insertLast(w)
for all v  G.vertices()                          else
if getLabel(v) = UNEXPLORED                         setLabel(e, CROSS)
BFS(G, v)                           i  i +1
2/29/2012 3:22 AM             Breadth-First Search                         7
Properties
Notation                                                  A
Gs: connected component of s
Property 1                                           B        C       D
BFS(G, s) visits all the vertices and
edges of Gs
E       F
Property 2
The discovery edges labeled by
BFS(G, s) form a spanning tree Ts
L0
of Gs                                                 A
Property 3
For each vertex v in Li                   L1
B       C       D
   The path of Ts from s to v has i
edges                                       L2
   Every path from s to v in Gs has at              E       F
least i edges
2/29/2012 3:22 AM              Breadth-First Search                    8
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 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
   Recall that   Sv deg(v) = 2m
2/29/2012 3:22 AM              Breadth-First Search       9
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

2/29/2012 3:22 AM        Breadth-First Search            10
DFS vs. BFS
Applications                             DFS          BFS
Spanning forest, connected
        
components, paths, cycles
Shortest paths                                         

L0
A                                                A

L1
B              C        D                       B                C       D

L2
E            F                                   E            F

DFS                                       BFS
2/29/2012 3:22 AM             Breadth-First Search                                 11
DFS vs. BFS (cont.)
Back edge (v,w)                            Cross edge (v,w)
   w is an ancestor of v in                   w is in the same level as
the tree of discovery                       v or in the next level in
edges                                       the tree of discovery
edges

L0
A                                            A

L1
B              C        D                       B         C          D

L2
E            F                               E           F

DFS                                    BFS
2/29/2012 3:22 AM             Breadth-First Search                             12

```
To top