Download presentation

Presentation is loading. Please wait.

Published byMadilyn Sulton Modified over 2 years ago

1
Jecho and Donatus Depth First and Breadth First Search

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: i. Declare two empty lists: Open and Closed. ii. Add Start node to our Open list. iii. 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 Open List: A Closed List: Open List: B, C Closed List: A Open List: D, E, C Closed List: A, B Step 0 Step 1 Step 2 Kirupa.com - Depth First and Bread First Search

8
Illustration – Depth First Search Open List: G, C Closed List: A, B, D, E, F Open List: F,G, C Closed List: A, B, D, E Open List: E, C Closed List: A, B, D Step 3 Step 4 Step 5 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. i. Declare two empty lists: Open and Closed. ii. Add Start node to our Open list. iii. 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 Open List: A Closed List: Open List: B, C Closed List: A Open List: C, D, E Closed List: A, B Step 0 Step 1 Step 2 Kiruapa.com - Depth First and Bread First Search, and Allen Sherod - Data Structs and Algorithm Chapt 11, 2007

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

13
DFS Features Worst case performance O( | V | + | E | ) for explicit graphs traversed without repetition, O(b d ) 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(b d ) Worst case space complexity O( | V | + | E | ) = O(b d ) 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
Questions? Thanks for your time!

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google