# State Space Search Breadth First and Depth First

State Space Search: Breadth First and Depth First

Motivations • Many problems can be viewed as reaching a goal state from a given starting point, e. g. , the farmer-wolf-goat-cabbage problem. • Often there is an underlying state space successor function to proceed from one state to the next. • Search strategies are important methods to explore such a space to obtain the goal state.

Objectives 1. 2. 3. 4. 5. Top down search Bottom up search Breadth first search Depth first search Compare breadth first and depth first

State space for tic-tac-toe game • Represent a problem as a state space • Nodes represent discrete states, e. g. , configuration of game board. • Arcs (links, edges) represent transition between states e. g. , move in a game. • Analyze the structure and complexity of problem and search procedures using graph theory.

Breadth-first searching • A breadth-first search (BFS) A B D C E F H L M I N O G J P K Q 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 MNOPQ • J will be found before N

Breadth-first search algorithm Nodes are lining up to be visited in open. closed keeps track of all the nodes visited already.

A trace of breadth-first algorithm B is not the goal. Put his children onto the queue. Put him in closed. He is done. | | | Items between red bars are siblings. | | | goal is reached or open is empty.

Progress at iteration 6 open contains the nodes whose children have not been looked at yet. The queue is also called the frontier of the search. closed contains the nodes that have been visited, i. e. , nodes that have been opened or expanded. | | | |

Breadth-first searching summary A B D C E F H L M I N O G J P K Q

Depth-first searching • A depth-first search (DFS) A B D C E F H L M I N O G J P K Q 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 OPCFGJKQ • N will be found before J

The depth-first search algorithm This is the only difference between depth-first and breadth-first.

A trace of depth-first algorithm top of stack

Snap shot at iteration 6 frontier visited

Recursive depth-first search algorithm • DFS(node v) { } visit(v) for each child w of v, DFS(w) • Recursion uses a stack to store data for previous • • calls. The children nodes (w’s)are placed on the undeclared (implicit) stack. Breadth first uses a queue instead of a stack, no recursion. At any given time, the top of the stack contains only the node 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 goal node is found, the path can be extracted from the stack as the recursion unwinds.

Search sequences of depth-first and breadth-first Breadth first: A, B, C, … Depth first: 1, 2, 3, …

Comparing the ordering of search sequences • Determine the order of nodes (states) to be examined • Breadth-first search – When a state is examined, all of its children are examined, one after another – Explore the search space in a level-by-level fashion • Depth-first search – When a state is examined, all of its children and their descendants are examined before any of its siblings – Go deeper into the search space where possible

Summary • Graph representation – Nodes are problem solution states – Arcs are steps in problem solving • State Space Search – Find a solution path from start state to goal state. – Determine complexity of problem – Many problems (TSP) have exponential complexity, impossible to search exhaustively – Strategies to search large space need heuristic to reduce space and time complexity

- Slides: 17