Tree Searching Breadth First Search Dept First Search.

13

Click here to load reader

Transcript of Tree Searching Breadth First Search Dept First Search.

Page 1: Tree Searching Breadth First Search Dept First Search.

Tree SearchingBreadth First Search

Dept First Search

Page 2: Tree Searching Breadth First Search Dept First Search.

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)

L M N O P

G

Q

H JI K

FED

B C

A

Goal nodes

Page 3: Tree Searching Breadth First Search Dept First Search.

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 JL M N O P

G

Q

H JI K

FED

B C

A

Page 4: Tree Searching Breadth First Search Dept First Search.

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

Page 5: Tree Searching Breadth First Search Dept First Search.

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

Page 6: Tree Searching Breadth First Search Dept First Search.

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.

Page 7: Tree Searching Breadth First Search Dept First Search.

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.

Page 8: Tree Searching Breadth First Search Dept First Search.

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 NL M N O P

G

Q

H JI K

FED

B C

A

Page 9: Tree Searching Breadth First Search Dept First Search.

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

Page 10: Tree Searching Breadth First Search Dept 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.

Page 11: Tree Searching Breadth First Search Dept First Search.

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;

Page 12: Tree Searching Breadth First Search Dept First Search.

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?

Page 13: Tree Searching Breadth First Search Dept First Search.

DFS vs. BFS

EF

G

B

CD

A start

destination

A DFS on A ADFS on BB

A

DFS on C

BC

AB Return to call on B

D Call DFS on D

AB

D

Call DFS on GG found destination - done!Path is implicitly stored in DFS recursionPath is: A, B, D, G

DFS Process