Problem Solving Assignments as Search • Remember: Problem Set #1: Simple Scheme and Search Out today, due Session 4. • Reading: Brian C.Williams – Solving problems by searching: AIMA Ch. 3 16.410-13 Session 3 Slides adapted from: 6.034 Tomas Lozano Perez, Russell and Norvig AIMA Brian Williams, Fall 05 1 Brian Williams, Fall 05 2

Course Objective 1: Course Objective 1: Agent Architectures Agent Architectures Communicate and Interpret To understand the major types of agents and common architectures used to develop agents. Plan Map Locate in – Mission-oriented Agents World – Self-repairing Agents Monitor & Execute Plan Routes – Mobile Agents Diagnosis – Agile Agents – Communicating Agents Maneuver and Track

Brian Williams, Fall 05 3 Brian Williams, Fall 05 4

Course Objective 2: Principles of Agents Agent Building Blocks

16.410/13: To learn the modeling and • Activity Planning • Path Planning algorithmic building blocks for creating • Execution/Monitoring • Localization reasoning and learning agents: • Diagnosis • Map Building 1. To formulate reasoning problems in an • Repair • Trajectory Design appropriate formal representation. • Scheduling • Policy Construction 2. To describe, analyze and demonstrate the • Resource Allocation application of reasoning algorithms to �Most reasoning problems, like these, may be formulated solve these problem formulations. as state space search.

Brian Williams, Fall 05 5 Brian Williams, Fall 05 6

1 Course Objective 3: Implementing Agents

16.413: To appreciate the challenges of building a state of the art autonomous explorer: Complex missions must carefully: Fall 03: • Plan complex sequences of actions • Mars Exploration Rover shadow mode demonstration. • Schedule actions Fall 04: • Allocate tight resources • Gnu Robot competition. Fall 05: • Monitor and diagnose behavior • Model-based autonomy toolbox • Repair or reconfigure hardware. • The virtual solar system • Stay tuned for more. �Most AI problems, like these, may be formulated as Brian Williams, Fall 05 7 state space search. Brian Williams, Fall 05 8

Courtesy of Kanna Rajan, NASA Ames. Used with permission.

Astronaut Goose Outline Grain Fox

• Problem Formulation Rover – Problem solving as state space search • Mathematical Model • Astronaut + 1 item allowed in the rover. – Graphs and search trees Can the astronaut get its supplies • Goose alone eats Grain • Reasoning Algorithms safely across the Martian canal? • Fox alone eats Goose – Depth and breadth-first search Early AI: What are the universal problem solving methods? Simple Trivial

Brian Williams, Fall 05 9 Brian Williams, Fall 05 10

Courtesy of NASA.

Goose Problem Solving as Grain State Space Search Astronaut • Formulate Goal Fox – State • Astronaut, Fox, Goose & Grain across river Astronaut • Formulate Problem Goose Grain Grain Fox – States Fox • Location of Astronaut, Fox, Goose & Grain Astronaut at top or bottom river bank Astronaut Goose Goose – Operators Grain • Astronaut drives rover and 1 or 0 items Fox to other bank. Goose • Generate Solution Grain Goose Fox – Sequence of Operators (or States) Fox • Move(goose,astronaut), Move(astronaut), . . . Astronaut Astronaut Brian Williams, Fall 05 11 Grain Brian Williams, Fall 05 12

2 Astronaut Astronaut Goose Grain Goose Astronaut Astronaut Goose Grain Goose Astronaut Astronaut Grain Grain Fox Grain Grain Fox

Astronaut Astronaut Goose Goose Astronaut Astronaut Goose Goose Fox Goose Fox Grain Grain Fox Goose Fox Grain Grain Fox Fox Fox Fox

Astronaut Astronaut Astronaut Astronaut Astronaut Astronaut Goose Grain Grain Goose Goose Goose Grain Grain Goose Goose Grain Fox Fox Grain Fox Fox Fox Fox

Astronaut Astronaut Astronaut Astronaut Goose Goose Astronaut Grain Goose Goose Goose Astronaut Grain Goose Fox Fox Grain Fox Fox Grain Grain Fox Grain Fox

Astronaut Astronaut Goose Goose Goose Fox Goose Astronaut Goose Fox Goose Astronaut Grain Fox Grain Fox Fox Fox Grain Fox Fox Grain

Astronaut Astronaut Astronaut Goose Astronaut Astronaut Astronaut Goose Grain GooseBrian Williams, FallGrain 05 Fox 13 Grain GooseBrian Williams, FallGrain 05 Fox 14 Grain Grain

Formulation Example: 8-Puzzle Example: Planning Discrete Actions

5 4 1 2 3 • Swaggert & Lovell work on Apollo 13 emergency rig 6 1 8 8 4 lithium hydroxide unit. – Assembly 7 3 2 7 6 5 • Mattingly works in ground simulator to identify new sequence handling severe Start Goal power limitations. • States: integer location for each tile AND … – Planning & Resource Allocation • Mattingly identifies novel • Operators: move empty square up, down, left, right reconfiguration, exploiting • Goal Test: goal state as given LEM batteries for power. courtesy of NASA – Reconfiguration and Repair

Brian Williams, Fall 05 15 Brian Williams, Fall 05 16

Courtesy of Kanna Rajan, NASA Ames. Used with permission.

Planning as State Space Search: STRIPS Action Assumptions STRIPS Operator Representation precondition: (and (clear hose) Initial state: (on-table hose) Available actions Strips operators (empty arm)) (and (hose a) (clamp b) • Atomic time. (hydroxide-unit c) precondition: (and (clear hose) (on-table a) (on-table hose) pickup hose (on-table b) (empty arm)) Note: strips doesn’t • Agent is omniscient (on-table c) allow derived effects; effect : (and (not (clear hose)) (clear a) you must be complete! (no sensing necessary). (not (on -table hose)) (clear b) pickup hose (not (empty arm)) (clear c) • Agent is sole cause of change. (holding arm hose))) (empty arm)) effect : (and (not (clear hose)) (not (on -table hose)) • Actions have deterministic effects. goal (partial state): (not (empty arm)) (and (connected a b) (holding arm hose)))} (attached b a))) • No indirect effects. � Effects specify how to A Plan is an operator sequence change the set of assertions. from the initial state to the goal. Brian Williams, Fall 05 17 Brian Williams, Fall 05 18

3 Succinct Encodings: STRIPS Action Schemata • Instead of defining: Outline pickup-hose and pickup-clamp and … • Problem Formulation • Define a single pickup schema (with variables ?v): – Problem solving as state space search (:operator pick-up :parameters ((hose ?ob1)) • Mathematical Model :precondition (and (clear ?ob1) – Graphs and search trees (on-table ?ob1) (empty arm)) • Reasoning Algorithms :effect (and (not (clear ?ob1)) – Depth and breadth-first search (not (on-table ?ob1)) (not (empty arm)) (holding arm ?ob1))) Brian Williams, Fall 05 19 Brian Williams, Fall 05 20

Problem Formulation: A Graph Problem Formulation: A Graph

Operator State

Edge Vertex In Degree (2) Degree (1) b b Out Degree (1)

e Tail Vertex of Edge d Head Vertex of Edge neighbors (adjacent) Undirected Undirected Directed Incident to edge Directed Graph Graph Graph Graph (one-way streets) (two-way streets) (one-way streets) (two -way streets)

Brian Williams, Fall 05 21 Brian Williams, Fall 05 22

Problem Formulation: A Graph Examples of Graphs

Strongly connected graph Connected graph Boston Directed path between all vertices. Path between all vertices. San Fran Roadmap Complete graph All vertices are adjacent. Wash DC LA b b Dallas a a c c Planning Actions B e d Put B on C (graph of possible C C d e Sub graph states of the world) A B A Subset of vertices Put C on A edges between vertices in Subset Put C on A A B C Directed Clique Undirected A A complete subgraph Graph Put A on C Graph (All vertices are adjacent). Put C on B C C (one-way streets) (two-way streets) A B B Brian Williams, Fall 05 23 Brian Williams, Fall 05 24

4 Formal Representation of a Graph A Solution is a State Sequence: Problem Solving Searches Paths Bos Given Graph G, with start vertex S, and goal G, SFO Airline Routes a solution is a simple path from S to G. S C Wash DC A

LA Dallas G A D D start A Graph G is represented as an (ordered) pair , where: S C • V is a set of vertices {v1 …} < {Bos, SFO, LA, Dallas, Wash DC} B end • E is a set of (directed) edges {e1, …} {, A (directed) path is a sequence of vertices An edge is a pair of vertices, where 1 n such that each successive pair is a (directed) edge in G • v2 is the head of the edge, A simple path has no repeated vertices. •and v1 is the tail of the edge A cycle is a subpath where start = end. Brian Williams, Fall 05 . . .} > 25 Brian Williams, Fall 05 26 Note: A set has no duplicate elements.

A Solution is a State Sequence: A Solution is a State Sequence: Problem Solving Searches Paths Problem Solving Searches Paths

S S C C A A B G G A D A D C D G D D S C G S C G C G

B B

Represent searched paths using a tree. Represent searched paths using a tree.

Brian Williams, Fall 05 27 Brian Williams, Fall 05 28

Search Trees Search Trees

Parent

Root Siblings

Branch Node Child (Edge) (Vertex)

Brian Williams, Fall 05 29 Brian Williams, Fall 05 30

5 Search Trees Search Trees

Ancestors

A tree T is a directed Graph, such that there exists exactly one Descendants directed path between any pair of vertices.

Brian Williams, Fall 05 31 Brian Williams, Fall 05 32

Classes of Search Outline Blind Depth-First Systematic exploration of whole tree (uninformed) Breadth-First until the goal is found. • Problem Formulation Iterative-Deepening – Problem solving as state space search Heuristic Hill-Climbing Uses heuristic measure of goodness • Mathematical Model (informed) Best -First of a node,e.g. estimated distance to. – Graphs and search trees Beam goal. • Reasoning Algorithms Optimal Branch&Bound Uses path “length” measure. Finds – Depth and breadth-first search (informed) A* “shortest” path. A* also uses heuristic

Brian Williams, Fall 05 33 Brian Williams, Fall 05 34

Classes of Search Depth First Search (DFS)

Blind Depth-First Systematic exploration of whole tree Idea: After visiting node (uninformed) Breadth-First until the goal is found. •Visit children, before siblings •Visit siblings left to right Iterative-Deepening 1 S 2 7 A B 3 6 8 11 D C D G

4 C 5 G 9 C G 10

Brian Williams, Fall 05 35 Brian Williams, Fall 05 36

6 Breadth First Search (BFS) Elements of Algorithm Design Idea: After visiting node Description: (today) • Visit siblings, before children – stylized pseudo code, sufficient to analyze and implement the al gorithm (implementation next Wednesday). • Visit relatives left to right (top to bottom) Analysis: (following Monday) 1 S • Soundness: 2 3 A B – when a solution is returned, is it guaranteed to be correct? 4 5 6 7 • Completeness: D C D G – is the algorithm guaranteed to find a solution when there is one?

8 C 9 G 10 C G 11 • Time complexity: – how long does it take to find a solution? • Space complexity: – how much memory does it need to perform search? Brian Williams, Fall 05 37 Brian Williams, Fall 05 38

Simple Outline Going Meta: How do we maintain the search state? Search is State Space Search • Problem Formulation: State space search • A set of partial paths explored thus far. State Space • An ordering, specifying which partial path to expand next S • Model: Graphs and search trees • (called aqueue Q.) A B • Reasoning Algorithms: DFS and BFS How do we perform search? D C D G – A generic search algorithm • Repeatedly: C G C G – Depth-first search example • Select next partial path from Q. – Handling cycles • Expand it. Operator – Breadth-first search example • Add expansions to Q. • Terminate when goal found. Goal-Test Brian Williams, Fall 05 39 Brian Williams, Fall 05 40

Simple Search Algorithm Simple Search Algorithm Let Q be a list of partial paths, • S denotes the start node Let S be the start node and • G denotes the goal node. Let G be the Goal node. • A partial path is a path from S to some node D, 1. Initialize Q with partial path • e.g., note reverse order. S 2. If Q is empty, fail. Else, pick a partial path N from Q A B 3. If head(N) = G, return N (goal reached!) D C D G 4. Else: C G C G a) Remove N from Q • The head of a partial path is the most recent visited node of the path, b) Find all children of head(N) and • e.g., D. create all one-step extensions of N to each child. • The Q is a sequence of partial paths, c) Add all extended paths to Q • e.g. (, …>. d) Go to step 2. Brian Williams, Fall 05 41 Brian Williams, Fall 05 42

7 Depth First Search (DFS) Outline Idea: • Visit children, then siblings • Problem Formulation: State space search • Visit siblings left to right, (top to bottom).

• Model: Graphs and search trees 1 S • Reasoning Algorithms: DFS and BFS 2 7 A B – A generic search algorithm 3 6 8 11 D C D G – Depth-first search example 4 C 5 G 9 C G 10 – Handling cycles – Breadth-first search example Assuming that we pick the first element of Q, Then where do we add path extensions to the Q?

Brian Williams, Fall 05 43 Brian Williams, Fall 05 44

Simple Search Algorithm Depth-First Let Q be a list of partial paths, Let S be the start node and Pick first element of Q; Add path extensions to front of Q Let G be the Goal node.

1. Initialize Q with partial path (S) Q C 2. If Q is empty, fail. Else, pick a partial path N from Q 1 (S) G A 2 3. If head(N) = G, return N (goal reached!) 1 D 3 S 4. Else: 4 a) Remove N from Q B 5 b) Find all children of head(N) and create all the one-step extensions of N to each child. c) Add all extended paths to Q d) Go to step 2. Brian Williams, Fall 05 45 Brian Williams, Fall 05 46

Simple Search Algorithm Depth-First Let Q be a list of partial paths, Let S be the start node and Pick first element of Q; Add path extensions to front of Q Let G be the Goal node.

1. Initialize Q with partial path (S) Q C 2. If Q is empty, fail. Else, pick a partial path N from Q 1 (S) G A 2 3. If head(N) = G, return N (goal reached!) 1 D 3 S 4. Else: 4 a) Remove N from Q B 5 b) Find all children of head(N) and create all the one-step extensions of N to each child. c) Add all extended paths to Q d) Go to step 2. Brian Williams, Fall 05 47 Brian Williams, Fall 05 48

8 Depth-First Depth-First

Pick first element of Q; Add path extensions to front of Q Pick first element of Q; Add path extensions to front of Q

Q C Q C

1 (S) G 1 (S) G A A 2 (A S) 2 (A S) (B S) 1 D 1 D 3 S 3 S

4 B 4 B 5 5

Added paths in blue Added paths in blue

Brian Williams, Fall 05 49 Brian Williams, Fall 05 50

Simple Search Algorithm Depth-First Let Q be a list of partial paths, Let S be the start node and Pick first element of Q; Add path extensions to front of Q Let G be the Goal node.

1. Initialize Q with partial path (S) Q C 2. If Q is empty, fail. Else, pick a partial path N from Q 1 (S) 2 G A 2 (A S) (B S) 3. If head(N) = G, return N (goal reached!) 1 D 3 S 4. Else: 4 a) Remove N from Q B 5 b) Find all children of head(N) and create all the one-step extensions of N to each child. Added paths in blue c) Add all extended paths to Q d) Go to step 2. Brian Williams, Fall 05 51 Brian Williams, Fall 05 52

Depth-First Depth-First

Pick first element of Q; Add path extensions to front of Q Pick first element of Q; Add path extensions to front of Q

Q C Q C

1 (S) 2 G 1 (S) 2 G A A 2 (A S) (B S) 2 (A S) (B S) 1 D 1 D 3 (C A S) (D A S) (B S) S 3 (C A S) (D A S) (B S) S

4 B 4 B 5 5

Added paths in blue Added paths in blue

Brian Williams, Fall 05 53 Brian Williams, Fall 05 54

9 Depth-First Depth-First

Pick first element of Q; Add path extensions to front of Q Pick first element of Q; Add path extensions to front of Q

3 3 Q C Q C

1 (S) 2 G 1 (S) 2 G A A 2 (A S) (B S) 2 (A S) (B S) 1 D 1 D 3 (C A S) (D A S) (B S) S 3 (C A S) (D A S) (B S) S

4 B 4 (D A S) (B S) B 5 5

Added paths in blue Added paths in blue

Brian Williams, Fall 05 55 Brian Williams, Fall 05 56

Depth-First Depth-First

Pick first element of Q; Add path extensions to front of Q Pick first element of Q; Add path extensions to front of Q

3 3 Q C Q C

1 (S) 2 G 1 (S) 2 G A A 2 (A S) (B S) 4 2 (A S) (B S) 4 1 D 1 D 3 (C A S) (D A S) (B S) S 3 (C A S) (D A S) (B S) S

4 (D A S) (B S) B 4 (D A S) (B S) B 5 (C D A S)(G D A S) 5 (B S) Added paths in blue

Brian Williams, Fall 05 57 Brian Williams, Fall 05 58

Depth-First Depth-First

Pick first element of Q; Add path extensions to front of Q Pick first element of Q; Add path extensions to front of Q

3 3 Q C Q C

1 (S) 2 G 1 (S) 2 G A A 2 (A S) (B S) 4 2 (A S) (B S) 4 1 D 1 D 3 (C A S) (D A S) (B S) S 3 (C A S) (D A S) (B S) S

4 (D A S) (B S) B 4 (D A S) (B S) B (C D A S)(G D A S) (C D A S)(G D A S) 5 5 (B S) (B S) 6 (G D A S) (B S)

Brian Williams, Fall 05 59 Brian Williams, Fall 05 60

10 Depth-First Simple Search Algorithm Let Q be a list of partial paths, Pick first element of Q; Add path extensions to front of Q Let S be the start node and Let G be the Goal node. 3 Q C 1. Initialize Q with partial path (S) 1 (S) 2 G 2. If Q is empty, fail. Else, pick a partial path N from Q A 2 (A S) (B S) 4 1 D 3. If head(N) = G, return N (goal reached!) 3 (C A S) (D A S) (B S) S 4. Else: 4 (D A S) (B S) B a) Remove N from Q (C D A S)(G D A S) 5 (B S) b) Find all children of head(N) and 6 (G D A S) (B S) create all the one-step extensions of N to each child. c) Add all extended paths to Q d) Go to step 2. Brian Williams, Fall 05 61 Brian Williams, Fall 05 62

Issue: Starting at S and moving top to bottom, Outline will depth-first search ever reach G?

• Problem Formulation: State space search C

• Model: Graphs and search trees G A • Reasoning Algorithms: DFS and BFS D – A generic search algorithm S

– Depth-first search example B – Handling cycles – Breadth-first search example

Brian Williams, Fall 05 63 Brian Williams, Fall 05 64

Depth-First How Do We Avoid Repeat Visits? Effort can be wasted in more mild cases Idea: 3 Q C • Keep track of nodes already visited. 1 (S) 2 G • Do not place visited nodes on Q. A 2 (A S) (B S) 4 1 D 3 (C A S) (D A S) (B S) S Does this maintain correctness?

4 (D A S) (B S) B • Any goal reachable from a node that was visited a second (C D A S)(G D A S) time would be reachable from that node the first time. 5 (B S) • C visited multiple times 6 (G D A S) (B S) • Multiple paths to C, D & G Does it always improve efficiency? • Visits only a subset of the original paths, such that How much wasted effort can be incurred in the worst case? each node appears at most once at the head of a path in Q. Brian Williams, Fall 05 65 Brian Williams, Fall 05 66

11 How Do We Modify The Simple Search Algorithm? Simple Search Algorithm Let Q be a list of partial paths, Let Q be a list of partial paths, Let S be the start node and Let S be the start node and Let G be the Goal node. Let G be the Goal node. 1. Initialize Q with partial path (S) as only entry; 1. Initialize Q with partial path (S) as only entry; set Visited = {} 2. If Q is empty, fail. Else, pick some partial path N from Q 2. If Q is empty, fail. Else, pick some partial path N from Q 3. If head(N) = G, return N (goal reached!) 3. If head(N) = G, return N (goal reached!) 4. Else 4. Else a) Remove N from Q a) Remove N from Q b) Find all children of head(N) and b) Find all children of head(N) not in Visited and create all the one-step extensions of N to each child. create all the one-step extensions of N to each child. c) Add to Q all the extended paths; c) Add to Q all the extended paths; d) Go to step 2. d) Add children of head(N) to Visited Brian Williams, Fall 05 67 Brian Williams, Fall 05 68 e) Go to step 2.

Testing for the Goal Outline • This algorithm stops (in step 3) when head(N) = G.

• We could have performed this test in step 6 as each extended path is added to Q. This would catch termination earlier and be perfectly correct • Problem Formulation: State space search for all the searches we have covered so far. • Model: Graphs and search trees • However, performing the test in step 6 will be incorrect for th e optimal searches we look at later. We have chosen to leave the test in step 3 to • Reasoning Algorithms: DFS and BFS maintain uniformity with these future searches. – A generic search algorithm – Depth-first search example – Handling cycles – Breadth-first search example

Brian Williams, Fall 05 69 Brian Williams, Fall 05 70

Breadth First Search (BFS) Breadth-First w Visited Pick first element of Q; Add path extensions to end of Q Idea: • Visit siblings before their children Q Visited • Visit relatives left to right C 1 (S) S G 1 2 A S 2 3 3 1 D A B S 4 4 5 6 7 D C D G 5 B

8 C 9 G 10 C G 11 6

Assuming that we pick the first element of Q, Then where do we add path extensions to the Q?

Brian Williams, Fall 05 71 Brian Williams, Fall 05 72

12 Breadth-First w Visited Breadth-First w Visited Pick first element of Q; Add path extensions to end of Q Pick first element of Q; Add path extensions to end of Q

Q Visited Q Visited C C 1 (S) S 1 (S) S G G 2 A 2 (A S) (B S) A,B,S A 3 1 D 3 1 D S S 4 4 5 B 5 B 6 6

Brian Williams, Fall 05 73 Brian Williams, Fall 05 74

Breadth-First w Visited Breadth-First w Visited Pick first element of Q; Add path extensions to end of Q Pick first element of Q; Add path extensions to end of Q

Q Visited Q Visited C C 1 (S) S 1 (S) S 2 G 2 G 2 (A S) (B S) A,B,S A 2 (A S) (B S) A,B,S A 3 1 D 3 (B S) (C A S) (D A S) C,D,B,A,S 1 D S S 4 4 5 B 5 B 6 6

Brian Williams, Fall 05 75 Brian Williams, Fall 05 76

Breadth-First w Visited Breadth-First w Visited Pick first element of Q; Add path extensions to end of Q Pick first element of Q; Add path extensions to end of Q

Q Visited Q Visited C C 1 (S) S 1 (S) S 2 G 2 G 2 (A S) (B S) A,B,S A 2 (A S) (B S) A,B,S A 3 (B S) (C A S) (D A S) C,D,B,A,S 1 D 3 (B S) (C A S) (D A S) C,D,B,A,S 1 D S S 4 4 (C A S) (D A S) (G B S)* G,C,D,B,A,S B B 5 3 5 3 6 6

* We could stop here, when the first path to the goal is generated. Brian Williams, Fall 05 77 Brian Williams, Fall 05 78

13 Breadth-First w Visited Breadth-First w Visited Pick first element of Q; Add path extensions to end of Q Pick first element of Q; Add path extensions to end of Q

Q Visited 4 Q Visited 4 C C 1 (S) S 1 (S) S 2 G 2 G 2 (A S) (B S) A,B,S A 2 (A S) (B S) A,B,S A 5 3 (B S) (C A S) (D A S) C,D,B,A,S 1 D 3 (B S) (C A S) (D A S) C,D,B,A,S 1 D S S 4 (C A S) (D A S) (G B S)* G,C,D,B,A,S 4 (C A S) (D A S) (G B S)* G,C,D,B,A,S B B 5 3 5 (D A S) (G B S) G,C,D,B,A,S 3 6 6

* We could stop here, when the first path to the goal is generated. Brian Williams, Fall 05 79 Brian Williams, Fall 05 80

Breadth-First w Visited Breadth-First w Visited Pick first element of Q; Add path extensions to end of Q Pick first element of Q; Add path extensions to end of Q

Q Visited 4 Q Visited 4 C C 1 (S) S 6 1 (S) S 6 2 G 2 G 2 (A S) (B S) A,B,S A 2 (A S) (B S) A,B,S A 5 5 3 (B S) (C A S) (D A S) C,D,B,A,S 1 D 3 (B S) (C A S) (D A S) C,D,B,A,S 1 D S S 4 (C A S) (D A S) (G B S)* G,C,D,B,A,S 4 (C A S) (D A S) (G B S)* G,C,D,B,A,S B B 5 (D A S) (G B S) G,C,D,B,A,S 3 5 (D A S) (G B S) G,C,D,B,A,S 3 6 (G B S) G,C,D,B,A,S 6 (G B S) G,C,D,B,A,S

Brian Williams, Fall 05 81 Brian Williams, Fall 05 82

Depth-first with visited list Depth First Search (DFS) S Pick first element of Q; Add path extensions to front of Q A B Depth-first: Add path extensions tofront of Q D C D G 3 C Pick first element of Q Q Visited C G C G 5 1 (S) S 2 G A 2 (A S) (B S) A, B, S 4 1 D Breadth First Search (BFS) S 3 (C A S) (D A S) (B S) C,D,B,A,S S Breadth-first: 4 (D A S) (B S) C,D,B,A,S B A B 5 (G D A S) (B S) G,C,D,B,A,S Add path extensions to back of Q D C D G Pick first element of Q C G C G

For each search type, where do we place the children on the queue?

Brian Williams, Fall 05 83 Brian Williams, Fall 05 84

14 What You Should Know • Most problem solving tasks may be formulated as state space search. • Mathematical representations for search are Appendix graphs and search trees. • Depth-first and breadth-first search may be framed, among others, as instances of a generic search strategy. • Cycle detection is required to achieve efficiency and completeness.

Brian Williams, Fall 05 85 Brian Williams, Fall 05 86

Breadth-First (without Visited list) Breadth-First (without Visited list) Pick first element of Q; Add path extensions to end of Q Pick first element of Q; Add path extensions to end of Q

Q Q 1 (S) C 1 (S) C

2 G 2 (A S) (B S) 2 G A A 3 3 1 D 1 D 4 S 4 S

5 B 5 B 6 6 7 7 Added paths in blue

Brian Williams, Fall 05 87 Brian Williams, Fall 05 88

Breadth-First (without Visited list) Breadth-First (without Visited list) Pick first element of Q; Add path extensions to end of Q Pick first element of Q; Add path extensions to end of Q

Q Q 4 1 (S) C 1 (S) C

2 (A S) (B S) 2 G 2 (A S) (B S) 2 G A A 3 (B S) (C A S) (D A S) 3 (B S) (C A S) (D A S) 1 D 1 D 4 S 4 (C A S) (D A S) (D B S) (G B S)* S

5 B 5 B 6 3 6 3 7 7 Added paths in blue Added paths in blue Revisited nodes in pink * We could have stopped here, when the first path to the goal was generated. Brian Williams, Fall 05 89 Brian Williams, Fall 05 90

15 Breadth-First (without Visited list) Breadth-First (without Visited list) Pick first element of Q; Add path extensions to end of Q Pick first element of Q; Add path extensions to end of Q

Q Q 4 4 1 (S) C 1 (S) C

2 (A S) (B S) 2 G 2 (A S) (B S) 2 G A A 6 3 (B S) (C A S) (D A S) 5 3 (B S) (C A S) (D A S) 5 1 D 1 D 4 (C A S) (D A S) (D B S) (G B S)* S 4 (C A S) (D A S) (D B S) (G B S)* S

5 (D A S) (D B S) (G B S) B 5 (D A S) (D B S) (G B S) B 6 3 6 (D B S) (G B S) (C D A S) (G D A S) 3 7 7

Brian Williams, Fall 05 91 Brian Williams, Fall 05 92

Breadth-First (without Visited list) Pick first element of Q; Add path extensions to end of Q

Q 4 1 (S) C 7 2 (A S) (B S) 2 G A 6 3 (B S) (C A S) (D A S) 5 1 D 4 (C A S) (D A S) (D B S) (G B S)* S

5 (D A S) (D B S) (G B S) B 6 (D B S) (G B S) (C D A S) (G D A S) 3 7 (G B S) (C D A S) (G D A S)

Brian Williams, Fall 05 93

16