1 / 54

CSC 172 DATA STRUCTURES

Understand how Depth-First Search (DFS) and Breadth-First Search (BFS) work in graph traversal. Learn the step-by-step process, pseudocode, properties, and runtime analysis of both methods. Explore examples and marking strategies for efficient traversal.

johnmarion
Download Presentation

CSC 172 DATA STRUCTURES

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. CSC 172 DATA STRUCTURES

  2. Traversing graphs Depth-First Search like a pre- or post-order traversal of a tree (use stack) Breath-First Search like a level-order traversal (use queue)

  3. Exploring a Maze A depth-first search (DFS) in an undirected graph G is like wandering in a maze with a string and a can of paint – you can prevent yourself from getting lost.

  4. Example B C D A F G H E J K L I N O P M

  5. DFS Start at vertex s Tie the end of the string to s and mark “visited” on s Make s the current vertex u Travel along an arbitrary edge (u,v) unrolling string If edge(u,v) leads to an already visited vertex v then return to u else mark v as “visited”, set v as current u, repeat @ step 2 When all edges lead to visited vertices, backtrack to previous vertex (roll up string) and repeat @ step 2 When we backtrack to s and explore all its edges we are done

  6. DFS Pseudocode (labels edges) DFS( Vertex v) for each edge incident on v do: if edge e is unexplored then let w be the other endpoint of e if vertex w is unexplored then label e as a discovery edge recursively call DFS(w) else label e as a backedge

  7. Example B C D A F G H E J K L I N O P M

  8. Example B C D A F G H E J K L I N O P M

  9. Example B C D A F G H E J K L I N O P M

  10. Example B C D A F G H E J K L I N O P M

  11. Example B C D A F G H E J K L I N O P M

  12. Example B C D A F G H E J K L I N O P M

  13. Example B C D A F G H E J K L I N O P M

  14. Example B C D A F G H E J K L I N O P M

  15. Example B C D A F G H E J K L I N O P M

  16. Example B C D A F G H E J K L I N O P M

  17. Example B C D A F G H E J K L I N O P M

  18. Example B C D A F G H E J K L I N O P M

  19. Example B C D A F G H E J K L I N O P M

  20. Example B C D A F G H E J K L I N O P M

  21. Example B C D A F G H E J K L I N O P M

  22. Example B C D A F G H E J K L I N O P M

  23. Example B C D A F G H E J K L I N O P M

  24. Example B C D A F G H E J K L I N O P M

  25. Example B C D A F G H E J K L I N O P M

  26. Example B C D A F G H E J K L I N O P M

  27. Example B C D A F G H E J K L I N O P M

  28. Example B C D A F G H E J K L I N O P M

  29. Example B C D A F G H E J K L I N O P M

  30. DFS Tree B C D A F G H E J K L I N O P M

  31. DFS Properties Starting at s The traversal visits all the vertices in the connected component of s The discovery edges form a spanning tree of the connected component of s

  32. DFS Runtime DFS is called on each vertex exactly once Every edge is examined exactly twice (once from each of its vertices) So, for ns vertices and ms edges in the connected component of the vertex s, the DFS runs in O(ns+ms) if: - The graph data structure methods take constant time - Marking takes constant time - There is a systematic way to examine edges (avoiding redundancy)

  33. Marking Vertices Extend vertex structure to support variable for marking Use a hash table mechanism to log marked vertices

  34. Breadth-First Search Starting vertex has level 0 (anchor vertex) Visit (mark) all vertices that are only one edge away mark each vertex with its “level” One edge away from level 0 is level 1 One edge away from level 1 is level 2 Etc. . . .

  35. Example B C D A F G H E J K L I N O P M

  36. Example 0 B C D A F G H E J K L I N O P M

  37. Example 0 B C D A F G H E J K L I N O P M

  38. Example 0 1 B C D A F G H E J K L I N O P M

  39. Example 0 1 B C D A F G H E J K L I N O P M

  40. Example 2 0 1 B C D A F G H E J K L I N O P M

  41. Example 2 0 1 B C D A F G H E J K L I N O P M

  42. Example 2 3 0 1 B C D A F G H E J K L I N O P M

  43. Example 2 3 0 1 B C D A F G H E J K L I N O P M

  44. Example 2 3 0 1 B C D A F G H E J K L 4 I N O P M

  45. Example 2 3 0 1 B C D A F G H E J K L 4 I N O P M

  46. Example 2 3 0 1 B C D A F G H E J K L 4 I N O P M

  47. Example 2 3 0 1 B C D A F G H E J K L 4 I N O P M

  48. Example 2 3 0 1 B C D A F G H E J K L 4 I 5 N O P M

  49. Example 2 3 0 1 B C D A F G H E J K L 4 I 5 N O P M

  50. BFS Tree 2 3 0 1 B C D A F G H E J K L 4 I 5 N O P M

More Related