1 / 69

Graph Traversal

Graph Traversal. Chapter 9. Problem. Given a directed or undirected graph G=(V,E), find a way to traverse all of its vertices. Solution. Depth First Search (DFS) Breadth First Search (BFS). DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS. DFS.

parker
Download Presentation

Graph Traversal

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. Graph Traversal Chapter 9

  2. Problem • Given a directed or undirected graph G=(V,E), find a way to traverse all of its vertices.

  3. Solution • Depth First Search (DFS) • Breadth First Search (BFS)

  4. DFS

  5. DFS

  6. DFS

  7. DFS

  8. DFS

  9. DFS

  10. DFS

  11. DFS

  12. DFS

  13. DFS

  14. DFS

  15. DFS

  16. DFS

  17. DFS

  18. DFS

  19. DFS

  20. DFS

  21. DFS And so on ...

  22. DFS • We need also to record • the birth time of any node: the time of the first visit to the node. • the death time: the time after all of its children are visited and ..also dead

  23. Algorithm: DFS Input:graph G=(V,E) directed or undirected Output:preordering of the vertices in DFS spanning tree (or forest) • predfn  0; postdfn  0; % global variables • For each vertex vV • mark v unvisited • End for • For each vV % for connected components • if v is unvisited then dfs(v) • End for

  24. Procedure dfs(v) • Mark v visited • predfn  predfn + 1 % birth time • For each edge (v,w) E • if w is marked unvisited then dfs(w) • End for • postdfn  postdfn + 1 % death time

  25. Definitions • In a directed graph, (u,v) is • a tree edge, if it is in the DFS tree, i.e., v is marked unvisited • back edge, if v is marked visited & ancestor of u • Forward edge, if v marked visited & descendant of u • Cross edge, if it is otherwise.

  26. Tree edge back edge Cross edge Forward edge

  27. Definitions • In an undirected graph, (u,v) is • a tree edge if it is in the DFS tree • back edges, otherwise.

  28. Example 1 a d e c f b

  29. Example 1 a d e c f 2 b

  30. Example 1 a d e c f 2 b 3

  31. Example 1 4 a d e c f 2 b 3

  32. Example 1 4 a d e c f 2 b 5 3

  33. Example 1 4 a d e Back edge c f 2 b 5 3

  34. Example 1 4 a d e Back edge c f 2 b 5, 1 3

  35. Example 6 1 4 a d e Back edge c f 2 b 5, 1 3

  36. Example 6 1 4 a d e Cross edge Back edge c f 2 b 5, 1 3

  37. Example 6 1 4 a d e Cross edge Back edge Back edge c f 2 b 5, 1 3

  38. Example 6, 2 1 4 a d e Cross edge Back edge Back edge c f 2 b 5, 1 3

  39. Example 6, 2 1 4, 3 a d e Cross edge Back edge Back edge c f 2 b 5, 1 3

  40. Example 6, 2 1 4, 3 a d e Cross edge Back edge Back edge c f 2 b 5, 1 3, 4

  41. Example 6, 2 1 4, 3 a d e Cross edge Back edge Back edge c f 2, 3 b 5, 1 3, 4

  42. Example 6, 2 Forward edge 1 4, 3 a d e Cross edge Back edge Back edge c f 2, 3 b 5, 1 3, 4

  43. Example 6, 2 Forward edge 1, 6 4, 3 a d e Cross edge Back edge Back edge c f 2, 5 b 5, 1 3, 4

  44. Analysis of DFS • Time = (m + n) Applications: • Testing acyclicity (is G a tree?) • Strongly connected components

  45. BFS

  46. BFS

  47. BFS

  48. BFS

  49. BFS

  50. BFS

More Related