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)
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 c b d
e 1
3 2 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), Figure 4.1:5 nodes, and 6 arcs graph.
• 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 c b d
e
Figure 4.3: dotted curve indicates the path
[a,b,e,d]
Figure 4.4: a rooted graph
a
c b d
The root
• 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 N
ito N
kthen
– 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
c b d
The root
e f g h i j
Figure 4.5: a rooted tree
• 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].
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
• 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.
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.
• 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.
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.
3.6 Basic Depth First Search (DFS)
/* OPEN and CLOSED are lists */
OPEN = Start node, CLOSED = empty While 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 OPEN
End while Note:
 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)
Consider the following segment of a search for a solution to 
the 8-Puzzle problem a.The initial state
b. After expanding that state
c. After expanding "last" successor generated
In depth first search, the "last" successor generated will be expanded next
.
Example: road map
• Consider the following road map
S
A B
C
D 3 G
4
4 5
2
3 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 B
D G
3.7 Basic Breadth First Search (BRFS)
/* OPEN and CLOSED are lists */
OPEN = Start node, CLOSED = empty While 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 OPEN
End while Note:
 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)
Example: road map
• Consider the following road map
S
A B
C
D 3 G
4
4 5
2
3 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 B
D G
/* OPEN and CLOSED are lists */
OPEN = Start node, CLOSED = empty While 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)
Example: road map
• Consider the following road map
S
A B
C
D 3 G
4
4 5
2 3
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 B
D G