Professional Documents
Culture Documents
Problem Solving As State Space Search: Brian C.Williams 16.410-13 Sep 14, 2004
Problem Solving As State Space Search: Brian C.Williams 16.410-13 Sep 14, 2004
Brian C.Williams
16.410-13
Sep 14th, 2004
Slides adapted from:
6.034 Tomas Lozano Perez,
Russell and Norvig AIMA Brian Williams, Spring 04 1
Assignments
• Remember:
Problem Set #1: Simple Scheme and Search
due Monday, September 20th, 2003.
• Reading:
– Solving problems by searching: AIMA Ch. 3
Rover
• Astronaut + 1 item
allowed in the rover.
Can the astronaut get its supplies • Goose alone eats Grain
safely across the Martian canal? • Fox alone eats Goose
Astronaut
Fox
Astronaut
Goose Grain
Grain Fox
Fox
Astronaut Astronaut
Goose Goose
Grain
Fox
Goose
Grain Goose
Fox Fox
Astronaut Astronaut
Grain Brian Williams, Spring 04 7
Astronaut
Goose Grain Goose Astronaut Astronaut
Grain Grain Fox
Astronaut Astronaut
Goose Goose Astronaut Grain Goose
Fox Fox Grain
Grain Fox
Astronaut
Goose Fox Goose
Grain Goose Astronaut
Fox Fox Grain
Fox
Astronaut Astronaut
Goose Goose Astronaut Grain Goose
Fox Fox Grain
Grain Fox
Astronaut
Goose Fox Goose
Grain Goose Astronaut
Fox Fox Grain
Fox
6 1 8 8 4
7 3 2 7 6 5
Start Goal
• States: integer location for each tile AND …
• Operators: move empty square up, down, left, right
• Goal Test: goal state as given
Link Node
(edge) (vertex)
e Start Vertex
of Edge
d
End Vertex
of Edge
neighbors
(adjacent)
Directed Undirected
Incident to edge
Graph Graph
(one-way streets) (two-way streets)
b b
Out Degree (1)
Directed Undirected
Graph Graph
(one-way streets) (two-way streets)
Complete graph
All vertices are adjacent.
b b
a a
c c
e d
d e
Sub graph
Subset of vertices
edges between vertices in Subset
Wash DC
LA Dallas
Planning Actions B
Put B on C
C C
(graph of possible
states of the world) A B A
Put C on A
Put C on A
A B C
A
Put A on C
Put C on B C C
A B B
Brian Williams, Spring 04 19
A Graph
Bos
Wash DC
LA Dallas
end
Root
Branch Node
(Edge) (vertex)
Child
(Descendant)
Descendants
1
S
2 7
A B
8 11
3 6
D C D G
4 C 5 G 9 C G
10
1
S
2 3
A B
6 7
4 5
D C D G
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, Spring 04 32
Outline
• Problem Formulation: State space search
• Model: Graphs and search trees
• Reasoning Algorithms: DFS and BFS
– A generic search algorithm
– Depth-first search example
– Handling cycles
– Breadth-first search example
D C D G
How do we perform search?
• Repeatedly: C G C G
D C D G
C G C G
• The head of a partial path is the most recent node of the path,
• e.g., D.
• The Q is a list of partial paths,
• e.g. ((D A S) (C A S) …).
Brian Williams, Spring 04 35
Simple Search Algorithm
Let Q be a list of partial paths,
Let S be the start node and
Let G be the Goal node.
1
S
2 7
A B
8 11
3 6
D C D G
4 C 5 G 9 C G
10
Q C
1 (S) G
A
2
1 D
3 S
4 B
5
Q C
1 (S) G
A
2
1 D
3 S
4 B
5
Q C
1 (S) G
A
2 (A S)
1 D
3 S
4 B
5
Q C
1 (S) G
A
2 (A S) (B S)
1 D
3 S
4 B
5
Q C
1 (S) 2 G
A
2 (A S) (B S)
1 D
3 S
4 B
5
Q C
1 (S) 2 G
A
2 (A S) (B S)
1 D
3 (C A S) (D A S) (B S) S
4 B
5
Q C
1 (S) 2 G
A
2 (A S) (B S)
1 D
3 (C A S) (D A S) (B S) S
4 B
5
3
Q C
1 (S) 2 G
A
2 (A S) (B S)
1 D
3 (C A S) (D A S) (B S) S
4 B
5
3
Q C
1 (S) 2 G
A
2 (A S) (B S)
1 D
3 (C A S) (D A S) (B S) S
4 (D A S) (B S) B
5
3
Q C
1 (S) 2 G
A
2 (A S) (B S) 4
1 D
3 (C A S) (D A S) (B S) S
4 (D A S) (B S) B
5
3
Q C
1 (S) 2 G
A
2 (A S) (B S) 4
1 D
3 (C A S) (D A S) (B S) S
4 (D A S) (B S) B
(C D A S)(G D A S)
5
(B S)
3
Q C
1 (S) 2 G
A
2 (A S) (B S) 4
1 D
3 (C A S) (D A S) (B S) S
4 (D A S) (B S) B
(C D A S)(G D A S)
5
(B S)
3
Q C
1 (S) 2 G
A
2 (A S) (B S) 4
1 D
3 (C A S) (D A S) (B S) S
4 (D A S) (B S) B
(C D A S)(G D A S)
5
(B S)
6 (G D A S)(B S)
3
Q C
1 (S) 2 G
A
2 (A S) (B S) 4
1 D
3 (C A S) (D A S) (B S) S
4 (D A S) (B S) B
(C D A S)(G D A S)
5
(B S)
6 (G D A S)(B S)
G
A
D
S
1 (S) 2 G
A
2 (A S) (B S) 4
1 D
3 (C A S) (D A S) (B S) S
4 (D A S) (B S) B
(C D A S)(G D A S)
5
(B S) • C visited multiple times
6 (G D A S)(B S) • Multiple paths to C, D & G
e) Go to step 2.
Testing for the Goal
• This algorithm stops (in step 3) when head(N) = G.
• However, performing the test in step 6 will be incorrect for the optimal
searches we look at later. We have chosen to leave the test in step 3 to
maintain uniformity with these future searches.
1
S
2 3
A B
6 7
4 5
D C D G
8 C 9 G 10 C G
11
Q Visited
C
1 (S) S
G
2 A
3 1 D
S
4
5 B
Q Visited
C
1 (S) S
G
2 A
3 1 D
S
4
5 B
Q Visited
C
1 (S) S
G
2 (A S) (B S) A,B,S A
3 1 D
S
4
5 B
Q Visited
C
1 (S) S
2 G
2 (A S) (B S) A,B,S A
3 1 D
S
4
5 B
Q Visited
C
1 (S) S
2 G
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
S
4
5 B
Q Visited
C
1 (S) S
2 G
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
S
4
5 B
3
6
Q Visited
C
1 (S) S
2 G
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
S
4 (C A S) (D A S) (G B S)* G,C,D,B,A,S
5 B
3
6
* We could stop here, when the first path to the goal is generated.
Brian Williams, Spring 04 72
Breadth-First
Pick first element of Q; Add path extensions to end of Q
Q Visited 4
C
1 (S) S
2 G
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
S
4 (C A S) (D A S) (G B S)* G,C,D,B,A,S
5 B
3
6
* We could stop here, when the first path to the goal is generated.
Brian Williams, Spring 04 73
Breadth-First
Pick first element of Q; Add path extensions to end of Q
Q Visited 4
C
1 (S) S
2 G
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
S
4 (C A S) (D A S) (G B S)* G,C,D,B,A,S
5 (D A S) (G B S) G,C,D,B,A,S B
3
6
Q Visited 4
C
1 (S) S 6
2 G
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
S
4 (C A S) (D A S) (G B S)* G,C,D,B,A,S
5 (D A S) (G B S) G,C,D,B,A,S B
3
6 (G B S) G,C,D,B,A,S
Q Visited 4
C
1 (S) S 6
2 G
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
S
4 (C A S) (D A S) (G B S)* G,C,D,B,A,S
5 (D A S) (G B S) G,C,D,B,A,S B
3
6 (G B S) G,C,D,B,A,S
3
Q Visited C
5
1 (S) S 2 G
A
2 (A S) (B S) A, B, S 4
1 D
3 (C A S) (D A S) (B S) C,D,B,A,S S
4 (D A S) (B S) C,D,B,A,S B
5 (G D A S) (B S) G,C,D,B,A,S
For each search type, where do we place the children on the queue?
Brian Williams, Spring 04 78
What You Should Know
• Most problem solving tasks may be
formulated as state space search.
• Mathematical representations for search are
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, Spring 04 79
Appendix
Q
1 (S) C
2 G
A
3
1 D
4 S
5 B
6
7
Q
1 (S) C
2 (A S) (B S) 2 G
A
3
1 D
4 S
5 B
6
7
Added paths in blue
Q
1 (S) C
2 (A S) (B S) 2 G
A
3 (B S) (C A S) (D A S)
1 D
4 S
5 B
6 3
7
Added paths in blue
Q
4
1 (S) C
2 (A S) (B S) 2 G
A
3 (B S) (C A S) (D A S)
1 D
4 (C A S) (D A S) (D B S) (G B S)* S
5 B
6 3
7
Added paths in blue
Revisited nodes in pink
* We could have stopped here, when the first path to the goal was generated.
Brian Williams, Spring 04 84
Breadth-First (without Visited list)
Pick first element of Q; Add path extensions to end of Q
Q
4
1 (S) C
2 (A S) (B S) 2 G
A
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 3
Q
4
1 (S) C
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
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)