1 / 29

Breadth First Search

Breadth First Search. Another graph search algorithm is Breadth First Search (bfs). In addition to finding connected components, the BFS approach enables us to calculate the distance from the start to every reachable vertex in its connected component.

Télécharger la présentation

Breadth First Search

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Breadth First Search Another graph search algorithm is Breadth First Search (bfs). In addition to finding connected components, the BFS approach enables us to calculate the distance from the start to every reachable vertex in its connected component. BFS uses a queue to hold vertices that have been visited but have not yet been used as a current vertex. When searching from a vertex u, we look at every vertex adjacent to u, one after another; for each of these vertices, if it is unvisited, we mark it as visited and place it on the queue of pending vertices. After inspecting all the vertices adjacent to u, we remove the next vertex from the queue and explore from there.

  2. We start by marking the start vertex as visited, in this algorithm by setting its distance to 0; then we append it to the queue. Then: while the queue is not empty: Serve a vertex from the queue to be the current vertex; for each vertex w adjacent from current: if w has not been visited (distance is NIL): mark w as visited; make current the predecessor of w; append w to the queue of pending vertices; set the distance of w to be 1 + the distance of current;

  3. We illustrate the BFS algorithm execution with the following graph We assume that we process the adjacent vertices of our current vertex in alphabetical order. Our initial call will be bfs(a,…).

  4. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current a back front

  5. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current a back front

  6. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current e a back front

  7. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current f e a back front

  8. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current f e back front

  9. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current d c b f e back front

  10. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current d c b f back front

  11. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current g d c b f back front

  12. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current g d c b back front

  13. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current g d c back front

  14. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current g d back front

  15. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current h g d back front

  16. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current h g back front

  17. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current h back front

  18. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current j i h back front

  19. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current j i back front

  20. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current j back front

  21. black vertices have distance -1 arrow points to the current vertex teal vertices have been completely explored and distance is valid. Queue of pending vertices current j back front

  22. Code for BFS • Input parameters: adj, start Output parameters: none • bfs( adj, start ) { n = adj.last // number of vertices for i = 1 to n visit[i] = false visit[start] = true// q is an initially empty queue q.enqueue(start) while (!q.empty() ) { current = q.front( ) q.dequeue( ) trav = adj[current] // get pointer to first node of adjacency list of current while ( trav != null ) { v = trav.ver // get vertex from adjacency list of current if ( ! visit[v] ) { visit[v] = true q.enqueue(v) } trav = trav.next } }

  23. Running Time Analysis for BFS • Assume the graph has n vertices and m edges • The for-loop in bfs requires (n) steps • Again, the worst-case for the nested while-loops is when each vertex in each adjacency list is visited. • There are 2m such vertex occurrences • Therefore BFS is (n+m)

  24. Connectedness and Shortest Paths from a Given Vertex • It is easy to use BFS, starting at vertex s, to determine if there is a path in graph G from s to v and, if so, to output a shortest path from s to v. • The reason for the latter is that if vertex w is discovered from vertex v during BFS starting at s, the distance from s to w is 1 plus the distance from s to v. • Thus we could have set dist[s] = 0 and after that, set dist[w] = 1+dist[v] whenever w is discovered from v.

  25. Bipartite and 2-Colorable Graphs • Theorem A graph G is bipartite if and only if G is 2-colorable • Thus determining if G is bipartite is the same as determining if G is 2-colorable • Theorem If G is a graph with a 2-coloring : V(G)  {R,B}, then the vertex colors on any path in G must alternate between the two colors R and B. • Corollary If G is a 2-colorable graph, then G does not contain an odd-length cycle.

  26. Bipartite if and only if No Odd Cycles • Theorem If G does not contain an odd-length cycle, then G is 2-colorable If the theorem is true for all connected graphs, then it is clearly true for arbitrary graphs, so we assume G = (V,E) is connected. Choose a vertex u of G and define V0 = { v  V | dist(u,v) is even } and V1 = { v  V | dist(u,v) is odd}. Clearly V = V0  V1 and V0  V1 = . Thus V0 ,V1 is a partition of V. Claim: no edge of G connects vertices in the same partition set.

  27. Proof Continued • Claim: no edge of G connects vertices in the same partition set. Suppose not and let v,w be adjacent vertices in G whose distances from u are either both odd or both even. Let Qv be a shortest path from u to v and Qw a shortest path to w from u. Let x be the last vertex of Qw encountered when traversing Qv. The length of the segments of the two paths from u to x must be the same as otherwise we could replace the longer by the shorter and get a shorter path to its target. Pv Pw Let Pv and Pw be the segments of Qv and Qw from x to their targets. Then the lengths of Pv and Pw are either both odd or both even, so the sum of their lengths is even. But this means that Pv Pw  {v,w} is an odd cycle.

  28. Application of BFS: 2-Colorability • Determining if a connected graph is 2-colorable: Run BFS with the following modifications: Assign color WHITE to all vertices of G Assign color RED to the start vertex s and put s in the queue While the queue is not empty: u  dequeued element of the queue for each vertex w adjacent to u: if color(w) == color(u) return FALSE if color(w) == WHITE if color(u) = RED set color of w to BLUE else set color of w to RED Enqueue w return TRUE

  29. Homework • Page 187: # 3, 5, 6

More Related