treeSearching-sp10

treeSearching-sp10 - Tree Searches Source David Lee...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Tree Searches Source: David Lee Matuszek
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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 J I K F E D B C A Goal nodes
Background image of page 2
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 L M N O P G Q H J I K F E D B C A
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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-1)*n nodes When this method succeeds, it doesn’t give the path
Background image of page 4
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
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 16

treeSearching-sp10 - Tree Searches Source David Lee...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online