Breadth-First Search

Document Sample
Breadth-First Search Powered By Docstoc
					  Breadth-First Search
                                L0
                                           A

                         L1
                                  B            C       D

                                L2
                                           E       F




2/29/2012 3:20 AM   Breadth-First Search                   1
Outline and Reading
     Breadth-first search
         Algorithm
         Example
         Properties
         Analysis
         Applications
     DFS vs. BFS
         Comparison of applications
         Comparison of edge labels


2/29/2012 3:20 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:20 AM             Breadth-First Search                             3
BFS Algorithm
   The algorithm uses a
   mechanism for setting and
   getting “labels” of vertices
   and edges




2/29/2012 3:20 AM             Breadth-First Search   4
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:20 AM           Breadth-First Search                     5
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:20 AM           Breadth-First Search                    6
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:20 AM           Breadth-First Search                    7
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:20 AM              Breadth-First Search       8
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:20 AM        Breadth-First Search             9
DFS vs. BFS
            Applications                             DFS          BFS
            Spanning forest, connected
                                                                  
            components, paths, cycles
            Shortest paths                                         

            Biconnected components                        

                                                     L0
             A                                                A

                                           L1
      B              C        D                       B                C       D

                                                     L2
             E            F                                   E            F

                    DFS                                       BFS
2/29/2012 3:20 AM             Breadth-First Search                                 10
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:20 AM             Breadth-First Search                             11

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:29
posted:2/29/2012
language:
pages:11