1 / 16

Depth First and Breadth First Search

Depth First and Breadth First Search. Jecho and Donatus. Presentation Outline. Overview Illustration Pseudocode Summary. Overview. Searching in graphs is done to find which vertices can be reached from a starting vertex by following a path along the edges.

dacian
Download Presentation

Depth First and Breadth First Search

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. Depth First and Breadth First Search Jecho and Donatus

  2. Presentation Outline • Overview • Illustration • Pseudocode • Summary

  3. Overview • Searching in graphs is done to find which vertices can be reached from a starting vertex by following a path along the edges. • Depth-first search and Breadth-first search are two searching algorithms that operate on graphs. • Both of these functions start at a vertex and visit every vertex between it and a destination vertex. The algorithms find any paths that exist between two vertices. If a path exists, the path data, which is the order of the edges needed to get from start to finish, is built and stored for some meaningful purpose. Allen Sherrod. Data Structures and Algorithms for Game Developers -chapt 4, 2007

  4. Finding a path from the vertex Los Angeles to new York

  5. Depth-First Search Pseudocode • Depth-First Search The depth-first search is an algorithm that uses a stack data structure to start at a starting vertex and move until it reaches the destination, assuming it can be reached from that vertex. The steps of the algorithm are: • Declare two empty lists: Open and Closed. • Add Start node to our Open list. • While our Open list is not empty, loop the following: a. Remove the first node from our Open List. b. Check to see if the removed node is our destination. i. If the removed node is our destination, break out of the loop, add the node to our Closed list,  and return the value of our Closed list. ii. If the removed node is not our destination, continue the loop (go to Step c). c. Extract the neighbors of our above removed node. d. Add the neighbors to the beginning of our Open list, and add the removed node to our Closed list. Continue looping.

  6. Fig 1.0 Depth-First Search Allen Sherrod. Data Structures and Algorithms for Game Developers -chapt 11, 2007

  7. Illustration – Depth First Search Step 0 Open List: A Closed List: <empty> Step 1 Open List: B, C Closed List: A Step 2 Open List: D, E, C Closed List: A, B Kirupa.com - Depth First and Bread First Search

  8. Illustration – Depth First Search Step 3 Open List: E, C Closed List: A, B, D Step 4 Open List: F,G, C Closed List: A, B, D, E Step 5 Open List: G, C Closed List: A, B, D, E, F Kirupa.com - Depth First and Breadth First Search

  9. Breadth-First Search Pseudocode • In the breath-first search all adjacent vertices to the current vertex are checked before the algorithm moves forward, and it uses a queue instead of a stack. • Declare two empty lists: Open and Closed. • Add Start node to our Open list. • While our Open list is not empty, loop the following: a. Remove the first node from our Open List. b. Check to see if the removed node is our destination. i. If the removed node is our destination, break out of the loop, add the node to our Closed list,  and return the value of our Closed list. ii. If the removed node is not our destination, continue the loop (go to Step c). c. Extract the neighbors of our above removed node. d. Add the neighbors to the end of our Open list, and add the removed node to our Closed list. Kiruapa.com - Depth First and Bread First Search, and Allen Sherod - Data Structs and Algorithm Chapt 11, 2007

  10. Breadth-First Search Allen Sherrod. Data Structures and Algorithms for Game Developers -chapt 11, 2007

  11. Illustration – Breadth First Search Step 0 Open List: A Closed List: <empty> Step 1 Open List: B, C Closed List: A Step 2 Open List: C, D, E Closed List: A, B Kiruapa.com - Depth First and Bread First Search, and Allen Sherod - Data Structs and Algorithm Chapt 11, 2007

  12. Illustration – Breadth First Search Step 3 Open List: D, E Closed List: A, B, C Step 4 Open List: E Closed List: A, B, C, D Step 5 Open List: <empty> Closed List: A, B, C, D, E Kiruapa.com - Depth First and Bread First Search

  13. DFS Features • Worst case performance O( | V | + | E | ) for explicit graphs traversed without repetition, O(bd) for implicit graphs with branching factor b searched to depth d. • Worst case space complexity O( | V | ) if entire graph is traversed without repetition, O(longest path length searched) for implicit graphs without elimination of duplicate nodes Wikipedia/DFS

  14. BFS Features • Worst case performance O( | V | + | E | ) = O(bd) • Worst case space complexity O( | V | + | E | ) = O(bd) B- branching factor, d – graph depth | E| - the set of edges | V| - set of vertices Wikipedia/BFS

  15. Summary • Both search methods are considered blind searches (uninformed). They don't know anything about their future or where the target is. The paths the expand are mechanically defined. If a better path exists, they will not take it. If they are taking the wrong path, they won't know it. • With large graphs, you may run into cycles or loops • Another problem is, what if the left side of your tree has millions of nodes, but your destination is close to the origin on the right side of the tree. Depth first would waste numerous cycles exploring the left side before ever reaching the right side. • Breadth first search does not suffer from the same loop problems because it moves horizontally across each depth. Breadth first will always find a solution regardless of what type of search tree you have unless there are infinite nodes. • Memory is often a limiting factor. Having millions or billions of nodes, as is the case with searching the web.

  16. Thanks for your time! Questions?

More Related