1 / 20

Breath First Searching & Depth First Searching

Breath First Searching & Depth First Searching. C and Data Structures Baojian Hua bjhua@ustc.edu.cn. Searching. The systematic way to traverse all vertex in a graph Two general methods: breath first searching (BFS) start from one vertex, first visit all the adjacency vertices

zalman
Download Presentation

Breath First Searching & Depth First Searching

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. Breath First Searching & Depth First Searching C and Data Structures Baojian Hua bjhua@ustc.edu.cn

  2. Searching • The systematic way to traverse all vertex in a graph • Two general methods: • breath first searching (BFS) • start from one vertex, first visit all the adjacency vertices • depth first searching (DFS) • eager method • These slides assume the adjacency list representation

  3. Sample Graph 1 2 3 4 5 6

  4. “graph” ADT in C: Interface // in file “graph.h” #ifndef GRAPH_H #define GRAPH_H typedef void (*tyVisitFn)(poly); typedef struct graph *graph; graph new (); void insertVertex (graph g, poly x); void insertEdge (graph g, poly from, poly to); void bfs (graph g, poly start, tyVisitFn visit); void dfs (graph g, poly start, tyVisitFn visit); // we’d see more later … #endif

  5. Sample Graph BFS bfs (g, 1, natOutput); 1 2 3 4 5 6

  6. Sample Graph BFS bfs (g, 1, natOutput); print 1; 1 2 3 4 5 6

  7. Sample Graph BFS bfs (g, 1, natOutput); print 1; print 2; (choice) 1 2 3 4 5 6

  8. Sample Graph BFS bfs (g, 1, natOutput); print 1; print 2; print 4; 1 2 3 4 5 6

  9. Sample Graph BFS bfs (g, 1, natOutput); print 1; print 2; print 4; print 5; 1 2 3 4 5 6

  10. Sample Graph BFS bfs (g, 1, natOutput); print 1; print 2; print 4; print 5; print 3; 1 2 3 4 5 6

  11. Sample Graph BFS bfs (g, 1, natOutput); print 1; print 2; print 4; print 5; print 3; print 6; 1 2 3 4 5 6

  12. Moral • BFS is very much like the level-order traversal on trees • Maintain internally a queue to control the visit order • Obtain a BFS forest when finished

  13. Sample Graph DFS dfs (g, 1, natOutput); 1 2 3 4 5 6

  14. Sample Graph DFS dfs (g, 1, natOutput); print 1; 1 2 3 4 5 6

  15. Sample Graph DFS dfs (g, 1, natOutput); print 1; print 2; (choice) 1 2 3 4 5 6

  16. Sample Graph DFS dfs (g, 1, natOutput); print 1; print 2; (choice) print 5; 1 2 3 4 5 6

  17. Sample Graph DFS dfs (g, 1, natOutput); print 1; print 2; (choice) print 5; print 4; 1 2 3 4 5 6

  18. Sample Graph DFS dfs (g, 1, natOutput); print 1; print 2; (choice) print 5; print 4; print 3; (choice) 1 2 3 4 5 6

  19. Sample Graph DFS dfs (g, 1, natOutput); print 1; print 2; (choice) print 5; print 4; print 3; (choice) print 6; 1 2 3 4 5 6

  20. Moral • DFS is very much like the pre-order traversal on trees • Maintain internally a stack to control the visit order • for recursion function, machine maintain the stack • Obtain a DFS forest when finished

More Related