Presentation is loading. Please wait.

Presentation is loading. Please wait.

Breadth-First Search (BFS)

Similar presentations


Presentation on theme: "Breadth-First Search (BFS)"— Presentation transcript:

1 Breadth-First Search (BFS)
Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been extracted, modified and updated from original slides of : Data Structures and Algorithms in Java, 5th edition. John Wiley& Sons, ISBN Data Structures and the Java Collections Framework by William J. Collins, 3rdedition, ISBN Both books are published by Wiley. Copyright © Wiley Copyright © 2010 Michael T. Goodrich, Roberto Tamassia Copyright © 2011 William J. Collins Copyright © Aiman Hanna All rights reserved

2 Coverage Graph Traversal Breath-First Search (BFS) L0 L1 L2 C B A E D
Breadth-First Search

3 Breadth-First Search (BFS)
BFS is less adventurous than DFS, in the sense that it does not travel far, at any point of time, from where it starts The idea is similar to travelling between cities. First go and visit all the cities that are closest to you. If this goes fine, think about travelling to cities that are further. If this goes fine, go and travel to the ones that are even further! By the time you are adventurous, there are no more cities to visit! Consequently, BFS subdivides the vertices into levels and goes “as if” in rounds to visit the vertices (Still, equipment needed are a rolled robe, and can of paint spray). Breadth-First Search

4 BFS BFS performs as follows:
Start at some vertex s, which is considered as level 0. This vertex is also referred to as the “anchor” (All following explorations will be based on a distance from that anchor) Unroll the robe with length equivalent to the length of one edge. Go to all the vertices that you can reach with that length. These vertices belong to level 1. Paint the vertices that you visit (to mark them as VISTED) Once all the vertices in level 1 are visited, unroll the robe further to twice the length of an edge. Similarly go an visit all the vertices that you can reach with that length. These vertices form level 2. If the exploration through an edge leads to an already visited vertex, mark that edge as “cross”; otherwise mark it as “discovery” Repeat the above operation, visiting further levels, until no more vertices are there to visit Breadth-First Search

5 BFS Example1 unexplored vertex visited vertex unexplored edge
C B A E D L0 L1 F A unexplored vertex A visited vertex unexplored edge discovery edge cross edge L0 L0 A A L1 L1 B C D B C D E F E F Breadth-First Search

6 BFS Example1 (cont.) L0 L1 L0 L1 L2 L0 L1 L2 L0 L1 L2 C B A E D F C B
Breadth-First Search

7 BFS Example1 (cont.) L0 L1 L2 L0 L1 L2 L0 L1 L2 C B A E D F A B C D E
Breadth-First Search

8 BFS Algorithm Algorithm BFS(G, s)
L0  new empty sequence L0.addLast(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.addLast(w) else setLabel(e, CROSS) i  i +1 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) Breadth-First Search

9 BFS Example2 1 discovery edge
A B C D E F G H discovery edge A visited vertex (Thicker one indicates the anchor) unexplored vertex unexplored edge I J K L M N O P cross edge 1 2 B A D C F E H G J I L K N M P O A B C D E F G H I J K L M N O P Breadth-First Search

10 BFS Example2 (cont.) 1 2 3 discovery edge
1 2 3 A B C D discovery edge A visited vertex (Thicker one indicates the anchor) unexplored vertex unexplored edge 4 E F G H I J K L M N O P cross edge 1 2 3 1 2 3 A B C D A B C D 4 E F G H E F G H I J K L I J K L 5 M N O P M N O P

11 BFS Example2 (cont.) 1 2 3 discovery edge
1 2 3 A B C D discovery edge A visited vertex (Thicker one indicates the anchor) unexplored vertex unexplored edge 4 E F G H I J K L 5 cross edge M N O P 1 2 3 A B C D Nothing is added to Level 6. So, terminate as Level 6 is empty 4 E F G H I J K L 5 M N O P

12 BFS Analysis Notice that:
Discovery edges form a spanning tree, which is referred to as BFS tree Edges to already visited vertices are called “cross” edges and not “back” edges since these edges do not connect vertices to their ancestors. In fact all of these (non-tree) edges neither connect a vertex to its ancestor, nor to its descendant on the BFS tree We never go back to the anchor; instead we always start from the nodes of leveli to go to leveli+1 Breadth-First Search

13 BFS Analysis Using a BFS traversal over a graph G, it is possible to solve the following problems: (note: that is actually done using algorithms and subroutines that are slightly different than the above simplified shown algorithm) Visit all the vertices and edges of G Determine whether G is connected Compute spanning tree of G is G is connected Compute the spanning forest of G (all spanning trees) if G is a non-connected graph Compute the connected components of G Find a cycle of G, or report that G has no cycles Given a start vertex s of G, compute for every vertex v of G, a path with the minimum number of edges between s and v, or report that no such path exists However, what is the complexity of BFS? Breadth-First Search

14 BFS Analysis BFS on a graph with n vertices and m edges takes O(n + m ) time since: 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 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 The other problems that BFS can solve can also be achieved in O(n + m ) Breadth-First Search

15 BFS Properties Notation Property 1 Property 2 Property 3
Gs: connected component of s Property 1 BFS(G, s) visits all the vertices and edges of Gs Property 2 The discovery edges labeled by BFS(G, s) form a spanning tree Ts of Gs Property 3 For 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 the least number of edges connecting s to v (i.e., shortest path from s to v ) A B C D E F L0 A L1 B C D L2 E F Breadth-First Search

16 DFS vs. BFS Applications DFS BFS DFS BFS
Spanning forest, connected components, paths, cycles Shortest paths Biconnected components (See next slide for more details) C B A E D L0 L1 F L2 A B C D E F DFS BFS Breadth-First Search

17 DFS vs. BFS (cont.) Biconnected components: Two biconnected components of a graph share at most one vertex in common. A vertex is an articulation point if and only if it is common to more than one biconnected component. Example*: Each color corresponds to a biconnected component. Multi-colored vertices are cut vertices, and thus belong to multiple biconnected components* *Source: wikipedia.org

18 DFS vs. BFS (cont.) Biconnected components
An articulation vertex (or cut vertex) is a vertex whose removal increases the number of connected components. A graph is biconnected if it has no articulation vertices. Articulation points are important for networks since they represent single point of failure. DFS can be used to find the articulation points in a graph and compute its disconnected components in O(n + m) time. Click here for more details (see also the notes section of this slide). Many other good sources are also available both online and offline. Details of how atriculation points can be found using DFS [reference “The idea is to run a depth-first search while maintaining the following information: the depth of each vertex in the depth-first-search tree (once it gets visited), and for each vertex v, the lowest depth of neighbors of all descendants of v in the depth-first-search tree, called the lowpoint. The depth is standard to maintain during a depth-first search. The lowpoint of v can be computed after visiting all descendants of v (i.e., just before v gets popped off the depth-first-search stack) as the minimum of the depth of v, the depth of all neighbors of v (other than the parent of v in the depth-first-search tree) and the lowpoint of all children of v in the depth-first-search tree. The key fact is that a nonroot vertex v is a cut vertex (or articulation point) separating two biconnected components if and only if there is a child y of v such that lowpoint(y) ≥ depth(v). This property can be tested once the depth-first search returned from every child of v (i.e., just before v gets popped off the depth-first-search stack), and if true, v separates the graph into different biconnected components. This can be represented by computing one biconnected component out of every such y (a component which contains y will contain the subtree of y, plus v), and then erasing the subtree of y from the tree. The root vertex must be handled separately: it is a cut vertex if and only if it has at least two children. Thus, it suffices to simply build one component out of each child subtree of the root (including the root).” The graph has three biconnected components: {B, C, G}, {C, F, D}, and {A, G} C & G are articulation points C B G A D F

19 DFS vs. BFS (cont.) Back edge (v,w) Cross edge (v,w) DFS BFS
w is an ancestor of v in the tree of discovery edges. For example, A is an ancestor of C (there is a back edge form C to A) Cross edge (v,w) w is in the same level as v or in the next level. w and w are neither ancestors nor descendent in the BFS tree C B A E D L0 L1 F L2 A B C D E F DFS BFS Breadth-First Search


Download ppt "Breadth-First Search (BFS)"

Similar presentations


Ads by Google