1 / 50

Lecture 8

Lecture 8. Depth First Search (DFS) Breadth First Search (BFS). Topics. Reference: Introduction to Algorithm by Cormen Chapter 23: Elementary Graph Algorithm. Graph Search (traversal). How do we search a graph? At a particular vertices, where shall we go next? Two common framework:

nanji
Download Presentation

Lecture 8

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. Lecture 8 • Depth First Search (DFS) • Breadth First Search (BFS) Topics Reference: Introduction to Algorithm by Cormen Chapter 23: Elementary Graph Algorithm Data Structure and Algorithm

  2. Graph Search (traversal) • How do we search a graph? • At a particular vertices, where shall we go next? • Two common framework: • the depth-first search (DFS) • the breadth-first search (BFS) and • In DFS, go as far as possible along a single path until reach a dead end (a vertex with no edge out or no neighbor unexplored) then backtrack • In BFS, one explore a graph level by level away (explore all neighbors first and then move on) Data Structure and Algorithm

  3. Depth-First Search (DFS) d a c b e f g h i j • The basic idea behind this algorithm is that it traverses the graph using recursion • Go as far as possible until you reach a deadend • Backtrack to the previous path and try the next branch • The graph below, started at node a, would be visited in the following order: a, b, c, g, h, i, e, d, f, j Data Structure and Algorithm

  4. DFS: Color Scheme • Vertices initially colored white • Then colored gray when discovered • Then black when finished Data Structure and Algorithm

  5. DFS: Time Stamps • Discover time d[u]: when u is first discovered • Finish time f[u]: when backtrack from u • d[u] < f[u] Data Structure and Algorithm

  6. DFS Example sourcevertex Data Structure and Algorithm

  7. DFS Example sourcevertex d f 1 | | | | | | | | Data Structure and Algorithm

  8. DFS Example sourcevertex d f 1 | | | 2 | | | | | Data Structure and Algorithm

  9. DFS Example sourcevertex d f 1 | | | 2 | | 3 | | | Data Structure and Algorithm

  10. DFS Example sourcevertex d f 1 | | | 2 | | 3 | 4 | | Data Structure and Algorithm

  11. DFS Example sourcevertex d f 1 | | | 2 | | 3 | 4 5 | | Data Structure and Algorithm

  12. DFS Example sourcevertex d f 1 | | | 2 | | 3 | 4 5 | 6 | Data Structure and Algorithm

  13. DFS Example sourcevertex d f 1 | | | 2 | 7 | 3 | 4 5 | 6 | Data Structure and Algorithm

  14. DFS Example sourcevertex d f 1 | 8 | | 2 | 7 | 3 | 4 5 | 6 | Data Structure and Algorithm

  15. DFS Example sourcevertex d f 1 | 8 | | 2 | 7 9 | 3 | 4 5 | 6 | Data Structure and Algorithm

  16. DFS Example sourcevertex d f 1 | 8 | | 2 | 7 9 |10 3 | 4 5 | 6 | Data Structure and Algorithm

  17. DFS Example sourcevertex d f 1 | 8 |11 | 2 | 7 9 |10 3 | 4 5 | 6 | Data Structure and Algorithm

  18. DFS Example sourcevertex d f 1 |12 8 |11 | 2 | 7 9 |10 3 | 4 5 | 6 | Data Structure and Algorithm

  19. DFS Example sourcevertex d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 | Data Structure and Algorithm

  20. DFS Example sourcevertex d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 14| Data Structure and Algorithm

  21. DFS Example sourcevertex d f 1 |12 8 |11 13| 2 | 7 9 |10 3 | 4 5 | 6 14|15 Data Structure and Algorithm

  22. DFS Example sourcevertex d f 1 |12 8 |11 13|16 2 | 7 9 |10 3 | 4 5 | 6 14|15 Data Structure and Algorithm

  23. DFS: Algorithm • DFS(G) • for each vertex u in V, • color[u]=white; p[u]=NIL • time=0; • for each vertex u in V • if (color[u]=white) • DFS-VISIT(u) Data Structure and Algorithm

  24. DFS: Algorithm (Cont.) sourcevertex • DFS-VISIT(u) • color[u]=gray; • time = time + 1; • d[u] = time; • for each v in Adj(u) do • if (color[v] = white) • p[v] = u; • DFS-VISIT(v); • color[u] = black; • time = time + 1; f[u]= time; Data Structure and Algorithm

  25. DFS: Complexity Analysis Initialization complexity is O(V) DFS_VISIT is called exactly once for each vertex And DFS_VISIT scans all the edges which causes cost of O(E) Overall complexity is O(V + E) Data Structure and Algorithm

  26. DFS: Application • Topological Sort • Strongly Connected Component Data Structure and Algorithm

  27. Breadth-first Search (BFS) • Search for all vertices that are directly reachable from the root (called level 1 vertices) • After mark all these vertices, visit all vertices that are directly reachable from any level 1 vertices (called level 2 vertices), and so on. • In general, level k vertices are directly reachable from a level k – 1 vertices Data Structure and Algorithm

  28. BFS: the Color Scheme • White vertices have not been discovered • All vertices start out white • Grey vertices are discovered but not fully explored • They may be adjacent to white vertices • Black vertices are discovered and fully explored • They are adjacent only to black and gray vertices • Explore vertices by scanning adjacency list of grey vertices Data Structure and Algorithm

  29. An Example A B C D E F G H I J K L M N O P Data Structure and Algorithm

  30. A B C D 0 E F G H I J K L M N O P Data Structure and Algorithm

  31. A B C D 0 1 E F G H 1 1 I J K L M N O P Data Structure and Algorithm

  32. A B C D 0 2 1 E F G H 1 1 I J K L 2 M N O P Data Structure and Algorithm

  33. A B C D 0 3 2 1 E F G H 1 1 3 3 I J K L 2 M N O P 3 3 Data Structure and Algorithm

  34. A B C D 0 3 2 1 E F G H 1 1 3 4 3 I J K L 2 4 4 M N O P 3 3 Data Structure and Algorithm

  35. A B C D 0 3 2 1 E F G H 1 1 3 4 3 I J K L 2 4 4 5 M N O P 3 3 5 Data Structure and Algorithm

  36. A B C D 0 3 2 1 E F G H 1 1 3 4 3 I J K L 2 4 4 5 M N O P 3 3 5 Data Structure and Algorithm

  37. A B C D 0 3 2 1 E F G H 1 1 3 4 3 I J K L 2 4 4 5 M N O P 3 3 5 Data Structure and Algorithm

  38. BFS: Algorithm BFS(G, s) • For each vertex u in V – {s}, • color[u] = white; • d[u] = infinity; • p[u] = NIL • color[s] = GRAY; d[s] = 0; p[s] = NIL; Q = empty queue • ENQUEUE(Q,s) • while (Q not empty) • u = DEQUEUE(Q) • for each v  Adj[u] • if color[v] = WHITE • then color[v] = GREY • d[v] = d[u] + 1; p[v] = u • ENQUEUE(Q, v); • color[u] = BLACK; Data Structure and Algorithm

  39. Example r s t u         v w x y Data Structure and Algorithm

  40. Example r s t u  0       v w x y Q: s Data Structure and Algorithm

  41. Example r s t u 1 0    1   v w x y Q: w r Data Structure and Algorithm

  42. Example r s t u 1 0 2   1 2  v w x y Q: r t x Data Structure and Algorithm

  43. Example r s t u 1 0 2  2 1 2  v w x y Q: t x v Data Structure and Algorithm

  44. Example r s t u 1 0 2 3 2 1 2  v w x y Q: x v u Data Structure and Algorithm

  45. Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: v u y Data Structure and Algorithm

  46. Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: u y Data Structure and Algorithm

  47. Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: y Data Structure and Algorithm

  48. Example r s t u 1 0 2 3 2 1 2 3 v w x y Q: Ø Data Structure and Algorithm

  49. BFS: Complexity Analysis • Queuing time is O(V) and scanning all edges requires O(E) • Overhead for initialization is O (V) • So, total running time is O(V+E) Data Structure and Algorithm

  50. BFS: Application • Shortest path problem Data Structure and Algorithm

More Related