Download presentation
Presentation is loading. Please wait.
Published byFrancis Terry Modified over 9 years ago
1
Tree Searching Breadth First Search Dept First Search
2
2 Tree searches A tree search starts at the root and explores nodes from there, looking for a goal node (a node that satisfies certain conditions, depending on the problem) For some problems, any goal node is acceptable ( N or J ); for other problems, you want a minimum-depth goal node, that is, a goal node nearest the root (only J ) LM N OP G Q H J IK FED BC A Goal nodes
3
3 Depth-first searching A depth-first search (DFS) explores a path all the way to a leaf before backtracking and exploring another path For example, after searching A, then B, then D, the search backtracks and tries another path from B Node are explored in the order A B D E H L M N I O P C F G J K Q N will be found before J LM N OP G Q H J IK FED BC A
4
4 How to do depth-first searching Put the root node on a stack; while (stack is not empty) { remove a node from the stack; if (node is a goal node) return success; put all children of node onto the stack; } return failure; At each step, the stack contains some nodes from each of a number of levels The size of stack that is required depends on the branching factor b While searching level n, the stack contains approximately b*n nodes When this method succeeds, it doesn’t give the path
5
5 Recursive depth-first search search(node): if node is a goal, return success; for each child c of node { if search(c) is successful, return success; } return failure; The (implicit) stack contains only the nodes on a path from the root to a goal The stack only needs to be large enough to hold the deepest search path When a solution is found, the path is on the (implicit) stack, and can be extracted as the recursion “unwinds” print node and print c and
6
Dept first search (DFS) useful for Find a path from one vertex to another Whether or not graph is connected. Computing a spanning tree if a connected graph. It uses the Backtracking technique. 6
7
Applications of DFS Algorithm Testing whether graph is connected Computing a spanning forest of a graph Computing a path between two vertices of graph or equivalently reporting that no such path exist Computing a cycle in graph or equivalently reporting that no such cycle exists. 7
8
8 Breadth-first searching A breadth-first search (BFS) explores nodes nearest the root before exploring nodes further away For example, after searching A, then B, then C, the search proceeds with D, E, F, G Node are explored in the order A B C D E F G H I J K L M N O P Q J will be found before N LM N OP G Q H J IK FED BC A
9
9 How to do breadth-first searching Put the root node on a queue; while (queue is not empty) { remove a node from the queue; if (node is a goal node) return success; put all children of node onto the queue; } return failure; Just before starting to explore level n, the queue holds all the nodes at level n-1 In a typical tree, the number of nodes at each level increases exponentially with the depth Memory requirements may be infeasible When this method succeeds, it doesn’t give the path There is no “recursive” breadth-first search equivalent to recursive depth-first search
10
Applications of BFS Testing whether graph is connected Computing a spanning forest of a graph Computing a cycle in graph or reporting that no such cycle exists. Computing, for every vertex in graph, a path with minimum number of edges between start vertex and current vertex or reporting that no such path exists. Prim’s MST and Dijkstra’s SSP algorithm uses BFS. 10
11
11 Comparison of algorithms Depth-first searching: Put the root node on a stack; while (stack is not empty) { remove a node from the stack; if (node is a goal node) return success; put all children of node onto the stack; } return failure; Breadth-first searching: Put the root node on a queue; while (queue is not empty) { remove a node from the queue; if (node is a goal node) return success; put all children of node onto the queue; } return failure;
12
12 Depth- vs. breadth-first searching When a breadth-first search succeeds, it finds a minimum-depth (nearest the root) goal node When a depth-first search succeeds, the found goal node is not necessarily minimum depth For a large tree, breadth-first search memory requirements may be excessive For a large tree, a depth-first search may take an excessively long time to find even a very nearby goal node How can we combine the advantages (and avoid the disadvantages) of these two search techniques?
13
DFS vs. BFS E F G B C D A start destination A DFS on A A DFS on B B A DFS on C B C A B Return to call on B D Call DFS on D A B D Call DFS on G G found destination - done! Path is implicitly stored in DFS recursion Path is: A, B, D, G DFS Process
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.