3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The...

18
3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using graphs 3.5 Performing a State Space Search 3.6 Basic Depth First Search (DFS) 3.7 Basic Breadth First Search (DFS) 3.8 Best First Search (DFS)

Transcript of 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The...

Page 1: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

3.0 State Space Representation of Problems

3.1 Graphs

3.2 Formulating Search Problems3.3 The 8-Puzzle as an example3.4 State Space Representation using graphs3.5 Performing a State Space Search3.6 Basic Depth First Search (DFS)3.7 Basic Breadth First Search (DFS)3.8 Best First Search (DFS)

Page 2: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

3.1 Graphs• Definitions:• a graph consists of:

– A set of nodes N1, N2, N3,…Nn.

– A set of arcs that connect pairs of nodes.

• A directed graph has an indicated direction for traversing each arc.

• A labeled graph has its nodes labeled.

• A labeled directed graph is shown in figure 4.2

a

bc

d

e

1

23

4

5

Figure 4.2: Labeled directed graph

Nodes {a,b,c,d,e}

Arcs:{(a,b),(b,e),(c,a),(c,b), (d,c),(e,d)}

Figure 4.1:5 nodes, and 6 arcs graph.

Page 3: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

• A path through a graph connects a sequence of nodes through successive arcs. It is represented by an ordered list of the nodes representing the path. – For example in figure 4.3, [a, b, e,

d] is a path through nodes a ,b ,e , d.

• A rooted graph has a unique node (called the root ) such that there is a path from the root to all nodes within the graph. i.e. all paths originate from the root ( figure 4.4).

a

bc

d

e

Figure 4.3: dotted curve indicates the path [a,b,e,d]

Figure 4.4: a rooted graph

a

bcd

The root

Page 4: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

• A tree is a graph in which each two nodes have at most one path between them.– Figure 4.5 is an example of a

rooted tree.

• If a directed arc connects Ni to Nk then – Ni is the parent of Nk and– Nk is the child of Ni..– In figure 4.5: d is the parent

of e and f.– e and f are called siblings.

a

bcd

The root

e f g h i j

Figure 4.5: a rooted tree

Page 5: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

• In a graph:1. An ordered sequence of nodes [ N1, N2, N3 ..,

Nn], where each Ni, Ni+1 in the sequence represent an arc (Ni,Ni+1), is called a path of length n-1.

2. If a path contains any node more than once it said to contain a cycle or loop.

3. Two nodes in a graph are said to be connected if there is a path that includes them both.

4. On a path on a rooted graph, a node is said to be the ancestor of all nodes positioned after it ( to its right) as well as descendent of all nodes before it ( to its left) -For example, in figure 4.5, d is the ancestor of e,

while it is the descendent of a in the path [a, d, e].

Page 6: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

3.2 Formulating Search Problems • All search problems can be cast into the

following general form: – Starting State

E.g. • starting city for a route

– Goal State (or a test for goal state)E.g.

• destination city – The permissible operators

E.g. • go to city X

• A state is a data structure which captures all relevant information about the problem.E.g. – a node on a partial path

Page 7: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

• 3.3 The 8-Puzzle as an example The eight puzzle consists of a 3 x 3 grid with 8

consecutively numbered tiles arranged on it. Any tile adjacent to the space can be moved on it. A number of different goal states are used.

5 4 .

6 1 8

7 3 2

1 2 3

8 . 4

7 6 5

Start State Goal State

A state for this problem needs to keep track of the position of all tiles on the game board, with 0 representing the blank position (space) on the board

The initial state could be represented as: ( (5,4,0), (6,1,8), (7,3,2) )

The final state could be represented as: ( (1,2,3) (8,0,4), (7,6,5) )

The operators can be thought of in terms of the direction that the blank space effectively moves. i.e.. up, down, left, right.

Page 8: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

3.4 State Space Representation Using Graphs

• In the state space representation of a problem:– nodes of a graph correspond to partial

problem solution states.– arcs correspond to steps (application of

operators) in a problem solving process.– The root of the graph corresponds to the

initial state of the problem.– The goal node which may not exist, is a leaf

node which corresponds to a goal state.

• State Space Search is the process of finding a solution path from the start state to a goal state.

Page 9: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

• The task of a search algorithm is to find a solution path through such a problem space.

• The generation of new states ( expansion of nodes) along the path is done by applying the operators (such as legal moves in a game).

• A goal may describe – a statea winning board in a simple game.– or some property of the solution path itself

(length of the path) shortest path for example.

Page 10: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

3.5 Performing a State Space Search

State space search involves finding a path from the initial state of a search problem to a goal state.

To do this, 1-build a search graph, starting from the initial state (or the

goal state) 2- expand a state by applying the search operators to that

state, generating ALL of its successor states. These successors are in the next level down of the search

graph 3-The order in which we choose states for expansion is

determined by the search strategy Different strategies result in (sometimes massively) different

behaviour KEY CONCEPT: We want to find the solution while realizing

in memory as few as possible of the nodes in the search space.

Page 11: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

3.6 Basic Depth First Search (DFS)

/* OPEN and CLOSED are lists */OPEN = Start node, CLOSED = emptyWhile OPEN is not empty do Remove leftmost state from OPEN, call it X If X is a goal return success Put X on CLOSED Generate all successors of X Eliminate any successors that are already on OPEN or CLOSED put remaining successors on LEFT end of OPENEnd whileNote: For depth first put successors on LEFT (i.e. acts like a STACK) For breadth first put successors on Right (i.e. acts like a QUEUE)

Page 12: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

Consider the following segment of a search for a solution to the 8-Puzzle problem

a.The initial state

b. After expanding that state

Page 13: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

c. After expanding "last" successor generated

In depth first search, the "last" successor generated will be expanded next .

Page 14: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

Example: road map• Consider the following road map

S

AB

C

D

G3

4

4

5

23

3

4

Applying the DFS

1-Open=[S], closed=[]

2-Open=[AC], closed=[S]

3-Open=[BC], closed=[AS]

4-Open=[DC], closed=[BAS]

5-Open=[GC], closed=[DBAS]

6-Open=[C], closed=[GDBAS]

Report success

Path is SABDG

A

S

C

B C A D

D D BG

G G G

D

Page 15: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

3.7 Basic Breadth First Search (BRFS)

/* OPEN and CLOSED are lists */OPEN = Start node, CLOSED = emptyWhile OPEN is not empty do Remove leftmost state from OPEN, call it X If X is a goal return success Put X on CLOSED Generate all successors of X Eliminate any successors that are already on OPEN or CLOSED put remaining successors on RIGHT end of OPENEnd whileNote: For depth first put successors on LEFT (i.e. acts like a STACK) For breadth first put successors on RIGHT (i.e. acts like a QUEUE)

Page 16: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

Example: road map• Consider the following road map

S

AB

C

D

G3

4

4

5

23

3

4

Applying the BRFS

1-Open=[S], closed=[]

2-Open=[AC], closed=[S]

3-Open=[CB], closed=[AS]

4-Open=[BD], closed=[CAS]

5-Open=[D], closed=[CAS]

6-Open=[G], closed=[DCAS]

7-Open=[], closed=[GDCAS]

Report success

Path is SCDG

C

A

S

C

C A D

D BG

G G G

D

Page 17: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

/* OPEN and CLOSED are lists */OPEN = Start node, CLOSED = emptyWhile OPEN is not empty do Remove leftmost state from OPEN, call it X If X is a goal return success Put X on CLOSED Generate all successors of X Eliminate any successors that are already on OPEN or CLOSED put remaining successors on OPEN sorted

according to their heuristic distance to the goal

( ascending from left to right) End while

3.8 Best First Search (BFS)

Page 18: 3.0 State Space Representation of Problems 3.1 Graphs 3.2 Formulating Search Problems 3.3 The 8-Puzzle as an example 3.4 State Space Representation using.

Example: road map• Consider the following road map

S

AB

C

D

G3

4

4

5

23

3

4

Applying the BFS

1-Open=[S], closed=[]

2-Open=[C7A8], closed=[S]

3-Open=[D3A8], closed=[CS]

4-Open=[G0B3A8], closed=[DCS]

5-Open=[B3A8], closed=[GDCS]

Report success

Path is SCDG

C

A

S

C

C A D

D BG

G G G

D