200 likes | 503 Views
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.
E N D
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. • 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
Traversal example • If we were to enter this maze from the top, which direction should we try each time. • Left? • Right?
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
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.
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?
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.
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);
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.
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.
“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…
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.
Graph representation – undirected graph Adjacency list Adjacency matrix ref. Introduction to Algorithms by Thomas Cormen
Graph representation – directed graph Adjacency list Adjacency matrix ref. Introduction to Algorithms by Thomas Cormen