Upcoming SlideShare
×

# Graph Traversal Algorithm

3,864 views

Published on

BFS DFS

3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
3,864
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
160
0
Likes
3
Embeds 0
No embeds

No notes for slide

### Graph Traversal Algorithm

1. 1. GRAPH SEARCH ALGORITHMS <br />Dept Futures Studies 2010-11<br />1<br />
2. 2. Graph Search Algorithms<br /><ul><li> Systematic search of every edge and every vertex of a graph.
3. 3. Graph G=(V,E) is either directed or undirected.
4. 4. Applications
5. 5. Compilers
6. 6. Networks
7. 7. Routing, Searching, Clustering, etc.</li></ul>Dept Futures Studies 2010-11<br />2<br />
8. 8. Graph Traversal <br /><ul><li> Breadth First Search (BFS)
9. 9. Start several paths at a time, and advance in each one step at a time
10. 10. Depth First Search (DFS)
11. 11. Once a possible path is found, continue the search until the end of the path</li></ul> <br />Dept Futures Studies 2010-11<br />3<br />
12. 12. Dept Futures Studies 2010-11<br />4<br />Breadth-First Search<br /><ul><li>Breadth-first search starts at a given vertex s, which is at level 0.
13. 13. In the first stage, we visit all the vertices that are at the distance of one edge away. When we visit there, we paint as "visited," the vertices adjacent to the start vertex s - these vertices are placed into level 1.
14. 14. In the second stage, we visit all the new vertices we can reach at the distance of two edges away from the source vertex s. These new vertices, which are adjacent to level 1 vertices and not previously assigned to a level, are placed into level 2, and so on.
15. 15. The BFS traversal terminates when every vertex has been visited.</li></li></ul><li>Breadth-First Search<br />The algorithm maintains a queue Q to manage the set of vertices and starts with s, the source vertex<br />Initially, all vertices except s are colored white, and s is gray.<br />BFS algorithm maintains the following information for each vertex u:<br /> - color[u] (white, gray, or black) : indicates status<br />white = not discovered yet<br />gray = discovered, but not finished<br />black = finished<br /> - d[u] : distance from s to u<br /> - π[u] : predecessor of u in BF tree<br />Dept Futures Studies 2010-11<br />5<br />
16. 16. Each vertex is assigned a color.<br />In general, a vertex is white before we start processing it,it is gray during the period the vertex is being processed, and it is black after the processing of the vertex is completed.<br />Dept Futures Studies 2010-11<br />6<br />
17. 17. BFS Algorithm<br />Inputs: Inputs are a graph(directed or undirected) G =(V, E) and a source vertex s, where s is an element of V. The adjacency list representation of a graph is used in this analysis.<br />Dept Futures Studies 2010-11<br />7<br />
18. 18. Outputs: The outputs are a predecessor graph, which represents the paths travelled in the BFS traversal, and a collection of distances, which represent the distance of each of the vertices from the source vertex.<br />Dept Futures Studies 2010-11<br />8<br />
19. 19. Dept Futures Studies 2010-11<br />9<br />1.         for each u in V − {s}             2.             do color[u] ← WHITE3.                 d[u] ← infinity4.                 π[u] ← NIL5.         color[s] ← GRAY                 6.         d[s] ← 0                               7.         π[s] ← NIL                           8.         Q ← {}                              9.         ENQUEUE(Q, s)10        while Q is non-empty11.             dou ← DEQUEUE(Q)                    12.                 for each v adjacent to u                  13.                         do if color[v] ← WHITE       14.                                 then  color[v] ← GRAY15.                                          d[v] ← d[u] + 116.                                          π[v] ← u17.                                          ENQUEUE(Q, v)18.                 DEQUEUE(Q)19.         color[u] ← BLACK<br />BFS Algorithm<br />
20. 20. 1. Enqueue (Q,s) <br /> 2. while Q ≠ <br /> 3. do u Dequeue(Q)<br /> 4. for each v adjacent to u<br /> 5. doif color[v] = white<br /> 6. then color[v]  gray<br /> 7. d[v]  d[u] + 1<br /> 8. [v]  u<br /> 9. Enqueue(Q,v) <br />10. color[u]  black<br />Note: If G is not <br />connected, then <br />BFS will not visit the<br />entire graph (without<br />some extra provisions<br />in the algorithm)<br />Dept Futures Studies 2010-11<br />10<br />
21. 21. Graph G=(V, E)<br />s<br />u<br />r<br />t<br />v<br />w<br />x<br />y<br />Dept Futures Studies 2010-11<br />11<br />
22. 22. s<br />u<br />r<br />t<br />0<br />∞<br />∞<br />∞<br />Q<br />0<br />∞<br />∞<br />∞<br />∞<br />v<br />w<br />x<br />y<br />s<br />u<br />r<br />t<br />1<br />0<br />∞<br />∞<br />Q<br />1 1<br />∞<br />∞<br />1<br />∞<br />v<br />w<br />x<br />y<br />Dept Futures Studies 2010-11<br />12<br />
23. 23. 1<br />r<br />s<br />u<br />t<br />1<br />r<br />s<br />u<br />t<br />1<br />2<br />0<br />∞<br />2<br />1<br />0<br />∞<br />Q<br />Q<br />1 2 2<br />1 2 2<br />∞<br />2<br />∞<br />∞<br />2<br />1<br />2<br />v<br />w<br />x<br />y<br />v<br />w<br />x<br />y<br />Dept Futures Studies 2010-11<br />13<br />
24. 24. r<br />s<br />r<br />s<br />u<br />t<br />u<br />t<br />1<br />1<br />0<br />0<br />3<br />2<br />3<br />2<br />Q<br />Q<br />2 2 3<br />2 3 3<br />∞<br />2<br />1<br />2<br />3<br />2<br />1<br />2<br />v<br />w<br />x<br />y<br />v<br />w<br />x<br />y<br />Dept Futures Studies 2010-11<br />14<br />
25. 25. r<br />s<br />u<br />t<br />1<br />0<br />3<br />2<br />Q<br />3 3 <br />3<br />2<br />1<br />2<br />v<br />w<br />x<br />y<br />3<br />r<br />s<br />u<br />t<br />1<br />3<br />0<br />2<br />Q<br />3 <br />3<br />2<br />1<br />2<br />v<br />w<br />x<br />y<br />Dept Futures Studies 2010-11<br />15<br />
26. 26. 3<br />r<br />s<br />u<br />t<br />1<br />3<br />0<br />2<br />Q<br />Empty<br />3<br />2<br />1<br />2<br />v<br />w<br />x<br />y<br />r<br />s<br />u<br />t<br />1<br />3<br />0<br />2<br />Spanning Tree<br />3<br />2<br />1<br />2<br />v<br />w<br />x<br />y<br />Dept Futures Studies 2010-11<br />16<br />
27. 27. Complexity<br /><ul><li>each node enqueued and dequeued once = O(V) time
28. 28. each edge considered once (in each direction) = O(E) time</li></ul>Running Time of BFS<br />BFS (G, s) <br /> 0. For all i ≠ s, d[i]=∞; d[s]=0 <br /> 1. Enqueue (Q,s) <br /> 2. while Q ≠ <br /> 3. do u Dequeue(Q)<br /> 4. for each v adjacent to u<br /> 5. doif color[v] = white<br /> 6. then color[v]  gray<br /> 7. d[v]  d[u] + 1<br /> 8. [v]  u<br /> 9. Enqueue(Q,v) <br />10. color[u]  black<br />Dept Futures Studies 2010-11<br />17<br />
29. 29. Running Time of BFS<br />We will denote the running time by T(m, n)<br />where m is the number of edges and n is the number of vertices.<br />Let V ′ subset of V be the set of vertices enqueued on Q. Then,<br />T(m, n) = O(m+ n)<br />Dept Futures Studies 2010-11<br />18<br />
30. 30. Analysis of Breadth-First Search<br />Shortest-path distance (s,v) : minimum number of edges in any path from vertex s to v. If no path exists from s to v, then (s,v) = ∞ <br />The ultimate goal of the proof of correctness is to show that d[v] = (s,v) when the algorithm is done and that a path is found from s to all reachable vertices.<br />Dept Futures Studies 2010-11<br />19<br />
31. 31. Analysis of Breadth-First Search<br />Lemma 1:<br />Let G = (V, E) be a directed or undirected graph, and let s be an arbitrary vertex in G. Then for any edge (u, v)  E,<br />(s,v) (s,u) + 1 <br />Case 1: If u is reachable from s, then so is v. In this case, the shortest path from s to v can't be longer than the shortest path from s to u followed by the edge (u,v). <br />Case 2: If u is not reachable from s, then (s,u) = ∞ <br />In either case, the lemma holds. <br />Dept Futures Studies 2010-11<br />20<br />
32. 32. Show that d[v] = (s,v) for every vertex v.<br />Lemma 2:<br />Let G = (V, E) be a graph, and suppose that BFS is run from vertex s. Then for each vertex v, the value d[v] ≥ (s,v).<br />By induction on the number of enqueues (line 9). <br />Basis: When s is enqueued, d[s] = 0 = (s,s) and d[v] = ∞ ≥ (s,v) for all other nodes.<br />Ind.Step: Consider a white vertex v discovered from vertex u. implies that d[u] ≥ (s,u). Then <br /> d[v] = d[u] + 1 <br /> ≥ (s,u) + 1 <br /> ≥ (s,v). <br />Therefore, d[v] bounds (s,v) from above.<br />Dept Futures Studies 2010-11<br />21<br />
33. 33. Lemma 3:<br />For every vertex (v1,v2,...,vr) on Q during BFS (such that v1 is Q head and vr is Q rear), <br /> d[vr] ≤ d[v1] + 1 and d[vi] ≤ d[vi+1] for i = 1,2,..., r-1.<br />By induction on the number of queue operations. <br />Basis: Q contains only s, so lemma trivially holds.<br />Ind.Step:<br />Case 1: Show lemma holds after every dequeue. Suppose v1 is dequeued and v2 becomes new head. Then<br /> d[v1] ≤ d[v2] <br /> d[vr] ≤ d[v1] + 1 <br /> ≤ d[v2] + 1, so lemma holds after every dequeue. <br />Dept Futures Studies 2010-11<br />22<br />
34. 34. Lemma 3:<br />For every vertex (v1,v2,...,vr) on Q during BFS (such that v1 is Q head and vr is Q rear), d[vr] ≤ d[v1] + 1 and d[vi] ≤ d[vi+1] for i = 1,2,..., r-1<br />Case 2: Show lemma holds after every enqueue. Suppose v is enqueued, becoming vr+1 after vertex u is dequeued (i.e., v is adjacent to u). Then for the new head v1<br /> d[u] ≤ d[v1] <br /> d[vr] ≤ d[u] + 1 <br /> = d[v] <br /> = d[vr+1]<br />So after the enqueue, we have that d[vr] = d[v] = d[u] + 1 ≤ d[v1] + 1. <br />Also, d[vr] ≤ d[vr+1] and for all other nodes on Q, d[vi] ≤ d[vi+1] . <br />As a consequence of Lemma 3, if vi is enqueued before vj, then d[vi] ≤ d[vj] (also, if vi is enqueued before vj, then it is also dequeuedbefore vjby definition of a queue). <br />Dept Futures Studies 2010-11<br />23<br />
35. 35. Theorem 4: (Correctness of BFS)<br />Let G = (V, E) be a directed or undirected graph, and suppose that BFS is run from a given source vertex s  V. Then, during execution, BFS discovers every vertex v ≠ s that is reachable from the source s, and upon termination, d[v] = (s,v) for every reachable or unreachable vertex v.<br />Proof by contradiction. <br />Assume that for some vertex v that d[v] (s,v). Also, assume that v is the vertex with minimum(s,v) that receives an incorrect d value. By Lemma 2, it must be that d[v] > (s,v). <br />Dept Futures Studies 2010-11<br />24<br />
36. 36. Case 1: v is not reachable from s. Then (s,v) = ∞ = d[v]. This is a contradiction, and the Thm holds. <br />Case 2: v is reachable from s. Let u be the vertex immediately preceding v on a shortest path from s to v, so that (s,v) = (s,u) + 1. Because (s,u) < (s,v) and because v is the vertex with the minimum(s,v) that receives an incorrect d value, d[u] = (s,u). <br />So we have d[v] > (s,v) = (s,u) + 1 = d[u] + 1 (by L.1 and how we chose v as minimum(s,v) that receives an incorrect d value).<br />Dept Futures Studies 2010-11<br />25<br />
37. 37. Consider the time t when u is dequeued. At time t, v is either white, gray, or black. We can derive a contradiction in each of these cases.<br />Case 1: v is white. Then in line 7, d[v] = d[u]+1.<br />Case 2: v is black. Then v was already dequeued, and therefore d[v] ≤ d[u] (by L. 3).<br />Case 3: v is gray. Then v turned gray when it was visited from some vertex w, which was dequeued before u. <br />Then d[v] = d[w] + 1. Since d[w] ≤ d[u] (by L 3), d[v] ≤ d[u] + 1. <br />Each of these cases is a contradiction to d[v] > (s,v), so we conclude that d[v] = (s,v).<br />Dept Futures Studies 2010-11<br />26<br />
38. 38. Applications of BFS<br /><ul><li> Connected components
39. 39. Bipartite</li></ul>Dept Futures Studies 2010-11<br />27<br />
40. 40. Find the number of connected components of a graph.<br /> Describe with diagram.<br />Dept Futures Studies 2010-11<br />28<br />
41. 41. Bipartite Graph<br />A bipartite graph is an undirected graph G = (V, E) in which V can be partitioned into two sets V1 and V2 such that (u, v) E implies either u in V1 and v in V2 or u in V2 and v in V1. That is, all edges go between the two sets V1 and V2.<br />Dept Futures Studies 2010-11<br />29<br />
42. 42. whenever the BFS is at a vertex u and encounters a vertex v that is already 'gray' our modified BSF should check to see if the depth of both u and v are even, or if they are both odd. If either of these conditions holds which implies d[u] and d[v] have the same parity, then the graph is not bipartite. <br />Dept Futures Studies 2010-11<br />30<br />
43. 43. 31<br />Level 0<br />a<br />c<br />b<br />Level 1<br />Odd cycle <br />Level 2<br />d<br />e<br />f<br />Level 3<br />i<br />h<br />g<br />j<br />Queue E f g <br />2 2 3<br />Dept Futures Studies 2010-11<br />
44. 44. Dept Futures Studies 2010-11<br />32<br />Jyothimon C<br />Dept Futures Studies <br />University of KERALA<br />jyothimon86@gmail.com<br />Thank You<br />