1 / 16

Overview of DFS and BFS

Overview of DFS and BFS. Traversal a “path”. Generally we search a “path” to find an answer. The path can as simple as tree structure to some more complex as circuit (a path with cycles into) It can be a directed or undirected graph as well. Order of traversal.

iden
Télécharger la présentation

Overview of DFS and BFS

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. Overview of DFS and BFS

  2. Traversal a “path” • Generally we search a “path” to find an answer. • The path can as simple as tree structure to some more complex as circuit (a path with cycles into) • It can be a directed or undirected graph as well.

  3. Order of traversal. • Most of the time, we use a preorder or inorder traversal • Preorder: visit each node before its children • Inorder: visit left subtree, node, right subtree (for binary trees only) • Postorder: vist each node after its children

  4. Traversal example • If we were to enter this maze from the top, which direction should we try each time. • Left? • Right?

  5. BFS and DFS • DFS is depth first search • Start down one path • Example 1, 2, 3, 4,5, 6, 7, etc. • BFS is breath first search • Search down the “levels” • 1, 2,7,8, 3,6,9,12

  6. Uses • Either can be used in most cases. • Find the depth of the tree (4) • Find short branch (1) • Find a path from node 1 to node 10 • The maze, find the exit. • Many others as well.

  7. Dfs and backtracking. • Using a recursive version, it allows you to backtrack. • Using the maze below, say we go down first • We’ll fail to find an exit. So we would backup until we get back to start and choose to go right. • BFS doesn’t need Backtracking. Why?

  8. DFS and BFS • Visiting all nodes or not following cycles • We would need to make the node that we visit so that go on infinitely.

  9. implementation • There a number of implementations • Simplest (likely least efficient too) • Dfs (Node) { if (visit (Node) ==“answer”) done else mark current node as visited. foreach node connected below this node { dfs(new node to search) //if no nodes are the answer, it falls back to try another node • Initial call: Dfs(start point);

  10. Implementation (2) • Dfs with a stack (works with cycles as well) • Push starting node onto stack • Pop stack • Check to make sure node has not be visited • If visited, pop again, until find unvisited node • Visit node. • Mark node visited and push “adjacent” unvisited nodes onto the stack (order matters here). • Go to 2.

  11. Implementations (3) • BFS search • Harder to implement a “simple” version, because you need to kept track of the “levels” • Easiest way is using queue • Insert start node on queue • Pop front node on queue. Visit node • If not the answer, mark node as visited • push all push “adjacent” unvisited nodes onto back of queue. • Go to 2.

  12. “Edge” costs. • So far the cost between nodes, has had a cost of 1. • If we have edges, then that can change the order. • Short path can be found with dfs and bfs but which path we take is based on the edge cost as the order. Ie take the path with a cost of 1 first, then 2, then 3, etc…

  13. Iterative deepening • Iterative deepening • Use dfs, but only to a depth and call again with next depth, …, until solution is found. • Depth 1, then 2, 3, 4, … until solution is found or max depth is reach. • Can look a lot like bfs, except it has “cut off” point.

  14. Graph representation – undirected graph Adjacency list Adjacency matrix ref. Introduction to Algorithms by Thomas Cormen

  15. Graph representation – directed graph Adjacency list Adjacency matrix ref. Introduction to Algorithms by Thomas Cormen

More Related