You are on page 1of 58

Problem Solving by Searching :

Uninformed Search
Type of problems
• Games – Chess, Tic-Tac-Toe, 8-Queen etc.
• Real World Problems – Route Finding,
Travelling Salesman Problem, Robot
Navigation etc.
• Pattern Recognition – Face Recognition,
Clustering, Feature Selection etc.

Vandana, BITS Pilani, BITS F444/ CS


August 2016 2
F407
Types of problems solved using Searching
Methods
• The problems requiring to obtain a sequence
of actions to reach a goal state from the start
state are solved using these techniques.
• These techniques are based on systematically
exploring all possible states the problem can
be in available possible alternatives.
• The search is made by exploring the nodes of
the search tree systematically.

Vandana, BITS Pilani, BITS F444/ CS


August 2016 3
F407
Understanding the State of a Problem: Example 1:
Movement from one place to another
• State : A state of a problem can be defined as a
tuple of values describing the physical states the
problem can be in at a particular time.
State :1 State :2

State :3
State 4

State :0
State 5

Vandana, BITS Pilani, BITS F444/ CS


August 2016 4
F407
Example 2: Program state change due
to execution

int x=0; • Let the state of the


program be defined
int y=12; as values of x and y.
if(y>10)
y= y-10; (0,2)

(0,0) (0,12)
else
x=x+10; (10,12)

Vandana, BITS Pilani, BITS F444/ CS


August 2016 5
F407
State space search
• State space search is a process in which
successive configurations or states of an instance
are considered, with the goal of finding a goal
state with a desired property.
• Problems are often modeled as a state space, a
set of states that a problem can be in.
• The set of states forms a graph where two states
are connected if there is an operation that can be
performed to transform the first state into the
second.
Vandana, BITS Pilani, BITS F444/ CS
August 2016 6
F407
Define States of 8-puzzle problem as a
tuple of values
x1 x2 x3

• <x1,x2,x3,x4,x5,x6,x7,x8 x4 x5 x6
x7 x8 x9
,x9>
• Start state
<7,2,4,5,-1,6,8,3,1>
• Goal State
<-1, 1, 2, 3, 4, 5, 6, 7, 8>

Vandana, BITS Pilani, BITS F444/ CS


August 2016 7
F407
Problem Formulation
• Define state of the problem
• Identify Initial (s) and Goal (g) state
• Identify actions on current state f(s) = p (new
state)
• Goal test : if (p == g)
• Evaluation function to decide the action

Vandana, BITS Pilani, BITS F444/ CS


August 2016 8
F407
State transition
• Successor function is responsible for
generating the legal states from the four
actions :
– Blank moves Left, Right, Up or Down

• Goal Test: Checks with every move whether


the goal state has reached

• Path cost: one at each step


Vandana, BITS Pilani, BITS F444/ CS
August 2016 9
F407
State Space Search
• All possible transitions are modeled as a tree.
• The root of the tree is the start state
• The goal state lies as a node down in the tree.
• The tree is traversed to find the goal state.
• If the goal state is found then the solution is
the sequence of transitions from the root to
the goal state

Vandana, BITS Pilani, BITS F444/ CS


August 2016 10
F407
Solving 8-puzzle problem

1 2 3 1 2 3
7 5 4 5 6
8 4 6 7 8

Initial State Goal State

Vandana, BITS Pilani, BITS F444/ CS


August 2016 11
F407
Search Tree Construction 1 2 3
7 5 Initial State
8 4 6

Left Down
Right Up

1 2 3 1 2 3 1 3 1 2 3

7 5 7 5 7 2 5 7 4 5
8 4 6 8 4 6 8 4 6 8 6

Each possible action (such as move the Left key to the empty place)
takes the Goal agent to a different state

Vandana, BITS Pilani, BITS F444/ CS


August 2016 12
F407
Search Tree Construction 1 2 3
7 5 Initial State
8 4 6

Left
Down
Right Up

1 2 3 1 2 3 1 3 1 2 3

7 5 7 5 7 2 5 7 4 5
8 4 6 8 4 6 8 4 6 8 6

Left Right up

Expanding the next state 1 2 3 1 2 3 1 2 3


<1,2,3,7,4,5,8, -1, 6> further,
takes the Goal agent to other 7 4 5 7 4 5 7 5
possible three different states 8 6 8 6 8 4 6

-1 represents the <1,2,3,7,4,5,-1,8,6>


empty cell Vandana, BITS Pilani, BITS F444/ CS
August 2016 13
F407
Search Tree Construction
1 2 3
Expanding the state 7 4 5
<1,2,3,7,4,5,-1,8,6> 8 6
further Right
Up
1 2 3 1 2 3
7 4 5 4 5
8 6 7 8 6

Vandana, BITS Pilani, BITS F444/ CS


August 2016 14
F407
Search Tree Construction
1 2 3
7 4 5
8 6
Right
Up
1 2 3 1 2 3
7 4 5 4 5
8 6 7 8 6

Right Down
Up
1 2 3 2 3 1 2 3

4 5 1 4 5 7 4 5

7 8 6 7 8 6 8 6

Vandana, BITS Pilani, BITS F444/ CS


August 2016 15
F407
1 2 3
Search Tree Construction 7 5
Initial State
8 4 6

Left Down
Right Up
1 2 3 1 2 3 1 3 1 2 3
7 5 7 5 7 2 5 7 4 5
8 4 6 8 4 6 8 4 6 8 6

Left Right up
1 2 33 1 2 3 1 2 3
7 4 55 7 4 5 7 5
8 66 8 6 8 4 6
Right
Up
1 2 3 1 2 3
7 4 5 4 5
8 6 7 8 6

Right Up Down
11 2 33 2 3 1 2 3
44 55 1 4 5 7 4 5
77 8 66 7 8 6 8 6
Left Down
Right Up
1 2 3 1 2 3 1 3 1 2 3
4 5 4 5 4 2 5 4 8 5
7 8 6 7 8 6 7 8 6 7 6
Left Up Down
1 2 3 1 2 1 2 3
4 5 4 5 3 4 5 6
7 8 6 7 8 6 7 BITS
Partially constructed
8 Pilani, BITS F444/ CS
Search tree
Vandana,
August 2016 16
F407
Search Tree Construction
Complexity of the search space tree
• At level 0 : Maximum number of nodes = 1
• At level 1: Maximum number of nodes = 4
• At level 2: Maximum number of nodes = 42
• At level 3: Maximum number of nodes = 43
• At level 4: Maximum number of nodes = 44
• …….
• At level d: Maximum number of nodes = 4d

Vandana, BITS Pilani, BITS F444/ CS


August 2016 17
F407
Search Tree Construction

Repeated states along the path


• This leads to infinite depth of branching.
• An algorithm must be designed to keep a trace of
the states that are already constructed.
• A suitable data structure could be a simple
lookup table, which can be implemented as an
array of records
typedef struct
{
state s;
boolean explored;
} lookupTable[maxStates];
Vandana, BITS Pilani, BITS F444/ CS
August 2016 18
F407
Search Tree Construction

A better definition
typedef struct
{
state s;
boolean explored;
depth d;
action A; //to reach the state s at depth
} lookupTable[maxStates];
This table can be populated and propagated
down with every action along the branch of the
tree and a state is reached if it was not earlier.
Vandana, BITS Pilani, BITS F444/ CS
August 2016 19
F407
State (All possible explored depth action
states)

1,2,3,7,-1,5,8,4,6 NO 0 0

1,2,3,7,4,5,8,-1,6 NO 0 0

…..

Initial Table
Vandana, BITS Pilani, BITS F444/ CS
August 2016 20
F407
1 2 3
Search Tree Construction 7 5
Initial State
8 4 6

Left Down
Right Up
1 2 3 1 2 3 1 3 1 2 3
7 5 7 5 7 2 5 7 4 5
8 4 6 8 4 6 8 4 6 8 6

Left Right up
1 2 33 1 2 3 1 2 3
7 4 55 7 4 5 7 5
8 66 8 6 8 4 6
Need to handle Right
Up
1 2 3 1 2 3
repeated state 7 4 5 4 5
8 6 7 8 6

Right Up Down
11 2 33 2 3 1 2 3
44 55 1 4 5 7 4 5
77 8 66 7 8 6 8 6
Left Down
Right Up
1 2 3 1 2 3 1 3 1 2 3
4 5 4 5 4 2 5 4 8 5
7 8 6 7 8 6 7 8 6 7 6
Left Up Down
1 2 3 1 2 1 2 3
4 5 4 5 3 4 5 6
7 8 6 7 8 6 7 BITS
8 Pilani, BITS F444/ CS
Vandana,
August 2016 21
F407
State (All possible explored depth action
states)

1,2,3,7,-1,5,8,4,6 YES 0 0

1,2,3,7,4,5,8,-1,6 YES 1 DOWN

1,2,3,7,4,5,-1,8,6 YES 2 LEFT

NO

YES …..

Table
Vandana, BITS Pilani, BITS F444/ CS
August 2016 22
F407
Use of appropriate data
1 2 3
structure and a suitable 7 5
Initial State
algorithm can avoid 8 4 6
expanding the node
Left
unnecessarily Right Up
Down

1 2 3 1 2 3 1 3 1 2 3
7 5 7 5 7 2 5 7 4 5
8 4 6 8 4 6 8 4 6 8 6

Left Right up
1 2 33 1 2 3 1 2 3
7 4 55 7 4 5 7 5
8 66 8 6 8 4 6
Right
Up
1 2 3 1 2 3
7 4 5 4 5
8 6 7 8 6

Right Up Down
11 2 33 2 3 1 2 3
44 55 1 4 5 7 4 5
77 8 66 7 8 6 8 6
Left Down
Right Up
1 2 3 1 2 3 1 3 1 2 3
4 5 4 5 4 2 5 4 8 5 Same states
7 8 6 7 8 6 7 8 6 7 6
Left Up Down
1 2 3 1 2 1 2 3
4 5 4 5 3 4 5 6
7 8 6 7 8 6 7 BITS
8 Pilani, BITS F444/ CS
Vandana,
August 2016 23
F407
Redefine a Node structure
struct node
{
state s;
lookupTable T;
struct node *left;
struct node *right;
struct node *up;
struct node *down;
}

Vandana, BITS Pilani, BITS F444/ CS


August 2016 24
F407
1 2 3
7 5
Initial State
8 4 6

Left Down
Right Up
1 2 3 1 2 3 1 3 1 2 3
7 5 7 5 7 2 5 7 4 5
8 4 6 8 4 6 8 4 6 8 6

Left Right up
1 2 33 1 2 3 1 2 3
7 4 55 7 4 5 7 5
8 66 8 6 8 4 6
Right
Up
1 2 3 1 2 3
7 4 5 4 5
8 6 7 8 6

Right Up Down
11 2 33 2 3 1 2 3
44 55 1 4 5 7 4 5
77 8 66 7 8 6 8 6
Left Down
Right Up
1 2 3 1 2 3 1 3 1 2 3
4 5 4 5 4 2 5 4 8 5
7 8 6 7 8 6 7 8 6 7 6
Left Up Down
1 2 3 1 2 1 2 3
4 5 4 5 3 4 5 6
7 8 6 7 8 6 7 BITS
8 Pilani, BITS F444/ CS
Vandana,
August 2016 25
F407
A node and its size

s T left right up down

9 integers maxStates x 4 pointers


size of each
table record

Home Work: Compute maxStates (Use combinations!!)

Vandana, BITS Pilani, BITS F444/ CS


August 2016 26
F407
Size of the search tree
• At depth d = maximum number of nodes x size
of node
= 4d x constant = K.4d
• Total number of nodes in the tree of height h=
(1 + 4 + 42 + 43 + ……..+ 4h) .K

Space complexity = O(bh)


Time complexity for construction of the search tree
proportional to the number of nodes created.
NOTE: once the tree is created then search complexity is much less.

Vandana, BITS Pilani, BITS F444/ CS


August 2016 27
F407
Uninformed Search Strategies

• Breadth First Search


• Depth First Search
• Depth Limited Search
• Iterative Deepening Search

Vandana, BITS Pilani, BITS F444/ CS


August 2016 28
F407
8-puzzle problem
1 2 3
7 5
Initial State
8 4 6

Left Down
Right Up
1 2 3 1 2 3 1 3 1 2 3
7 5 7 5 7 2 5 7 4 5
8 4 6 8 4 6 8 4 6 8 6

Left Right up
Recall: 1 2 33 1 2 3 1 2 3
4 55 7 4 5 7 5
1. State 7
8 66 8 6 8 4 6
2. Action Right
Up
3. Goal Test 1 2 3 1 2 3
7 4 5 4 5
4. Path cost 8 6 7 8 6

Right Up Down
11 2 33 2 3 1 2 3
44 55 1 4 5 7 4 5
77 8 66 7 8 6 8 6
Left Down
Right Up
1 2 3 1 2 3 1 3 1 2 3
4 5 4 5 4 2 5 4 8 5 Same states
7 8 6 7 8 6 7 8 6 7 6
Left Up Down
1 2 3 1 2 1 2 3
4 5 4 5 3 4 5 6
7 8 6 7 8 6 7 BITS
8 Pilani, BITS F444/ CS
Vandana,
August 2016 29
F407
Tree Data Structure
• What is a Data Structure?
• Why is memory required?
• What are different logical segments of
memory?
• Use of malloc() function in C
• What is a tree data structure?
• What is its actual layout in the memory?
• How is tree different from linked list?

Vandana, BITS Pilani, BITS F444/ CS


August 2016 30
F407
Tree data structure
• Use of hierarchical information.
• Access time is proportional to the height of
the tree (equal to hops to the starting of the
chunk of memory allocated to each node)
• Time is required for constructing node each
time at appropriate place.
• Memory is required for each new node till the
goal is met.
Vandana, BITS Pilani, BITS F444/ CS
August 2016 31
F407
Uninformed Search
• A search tree is generated with its root as initial
state.
• Nodes of the tree represent the states to which
the goal agent can be in at any time.
• The tree is expanded for each created node till
the goal state is reached.
• A problem which can be in say ‘b’ different states
from one state by ‘b’ different actions, is said to
have a branching factor ‘b’
• The amount of memory required is extremely
huge.
Vandana, BITS Pilani, BITS F444/ CS
August 2016 32
F407
A complete search tree

• It is a tree expanded fully across all nodes from the


root node till the level containing the goal state.
Initial
State

Goal
Tree is expanded to depth = 2
State
Number of nodes = O(bd+1)

Vandana, BITS Pilani, BITS F444/ CS


August 2016 33
F407
Memory and Time requirements for 8-
puzzle problem
• If a tree is expanded completely, the total number of
nodes is O(bd+1)
• For b=4 and d = 8 , the number of nodes created is of
the order 49= 262144
• If a node that represents the state requires 1KB of
memory, then the total memory requirement is of
256MB.
• Time to generate each node, compare each state with
the goal state is also proportional to the number of
nodes. If 1024 nodes can be generated in 1 second,
then the total time required =49/210seconds =
28seconds = 256 seconds

Vandana, BITS Pilani, BITS F444/ CS


August 2016 34
F407
Time and space complexities for
branching factor = 10
Assumption: 10000 nodes can be created per second
Each node takes 1000 bytes of memory

Depth Nodes Time space

4 O(10 5) 100 s 106 MB

10 O(10 11) 129 days 101 terabytess

14 O(10 15) 3,523 years 1 exabyte

Vandana, BITS Pilani, BITS F444/ CS


August 2016 35
F407
Search Strategy
• The need is to reduce the memory requirements
and time taken to expand all nodes to reach the
goal state.
• A strategy can be formulated to expand the node
without depending on factors related to the
problem, such as cost, weight etc (uninformed
search)
• The strategy depends on the order of traversal.
• The search strategy determines the choice of the
state to be expanded.
Vandana, BITS Pilani, BITS F444/ CS
August 2016 36
F407
Breadth First Search

• The root node is expanded first and then all its children
are expanded.
• The nodes at the next level are expanded only after all
the nodes in the previous level are expanded.

Vandana, BITS Pilani, BITS F444/ CS


August 2016 37
F407
Vandana, BITS Pilani, BITS F444/ CS
August 2016 38
F407
Vandana, BITS Pilani, BITS F444/ CS
August 2016 39
F407
A QUEUE (FIFO List) maintains the
Front of the nodes to be expanded
Queue

Vandana, BITS Pilani, BITS F444/ CS


August 2016 40
F407
Front of the
Queue Each expansion carries forward the
sequence of actions to reach the current
state from the initial state

A node must also maintain information


about the states already in the path.

Vandana, BITS Pilani, BITS F444/ CS


August 2016 41
F407
Depth First Search
• Always expands the deepest node in the
current fringe (store) of the search tree.
• The search proceeds down to the successors
of the current node immediately.
• A LIFO(last in first out) list, is used to maintain
the unexpanded nodes.
• The last node in the above list (also called as
stack) is expanded first.
Vandana, BITS Pilani, BITS F444/ CS
August 2016 42
F407
Root node

Root node

stack

Vandana, BITS Pilani, BITS F444/ CS


August 2016 43
F407
Root node
expanded
node1
Root node

stack

Vandana, BITS Pilani, BITS F444/ CS


August 2016 44
F407
Root node
expanded
node1
Root node

node1 node2 node3 node4 stack

Vandana, BITS Pilani, BITS F444/ CS


August 2016 45
F407
Root node
expanded
node1
Not yet
generated nodes

node2 node 2
node1
If node2 is goal Root node
state, terminate
search
stack

Vandana, BITS Pilani, BITS F444/ CS


August 2016 46
F407
Root node
expanded
node1
Not yet
generated nodes

node2 node 2
node1
Root node

stack

Vandana, BITS Pilani, BITS F444/ CS


August 2016 47
F407
Root node
expanded
node1
Not yet
generated nodes

Node 3
node2 node 2
node1
Root node

stack
node3
Vandana, BITS Pilani, BITS F444/ CS
August 2016 48
F407
Memory requirement
• It requires very less memory
• It maintains nodes only in one path.
• The maximum size of the stack determines the
total memory requirements for DFS.
• A node whose all children are expanded and
explored fully then the node address is popped
off from the top of the stack. Now its next sibling
gets expanded.
• For a depth ‘d’, the only requirement is of ‘d’
nodes to be preserved along the path/branch.

Vandana, BITS Pilani, BITS F444/ CS


August 2016 49
F407
Comparison of BFS and DFS

BFS DFS
Space O(bd+1) O(bm)
time O(bd+1) O(bm)
Complete? Yes No
Optimal? Yes No

Vandana, BITS Pilani, BITS F444/ CS


August 2016 50
F407
Depth Limited Search
• The DFS is applied with a limit on the depth
for expanding the node.
• This solves the infinite path problem as is
sometimes the case with simple DFS .
• The goal state may or may not be reached.

Vandana, BITS Pilani, BITS F444/ CS


August 2016 51
F407
Iterative Deepening DFS

• DFS is performed to expand to depth d, with d


initially taken as 1, then 2, then 3 and so on.

Vandana, BITS Pilani, BITS F444/ CS


August 2016 52
F407
Level 1

The root node is expanded and the


goal test is made.

If the goal state is found then Root node


terminate search
Stack
Else perform DFS for two levels
starting from the root

Vandana, BITS Pilani, BITS F444/ CS


August 2016 53
F407
Level 2
Root node

Node 1

Node 2
Node 2 does not expand at this
iteration as already level two is
reached.

Node 2 is popped off, second child of Node 2


node 1 is considered. Node 1

Goal test is performed. Root node

Process is repeated till all children of Stack


the root node Vandana,
are expanded
BITS Pilani, or the
BITS F444/ CS
August 2016 54
goal state is reached F407
Iterative deepening search
• It is a combination of DFS and BFS.
• At each iteration the search is analogous to BFS in that
it explores a complete layer.
• The nodes are generated again if the next iteration is
concerned, and may appear a waste of effort.
• But the nodes at the last level are generated only once.
• Total number of nodes (N) generated
= db +(d-1)b2++(d-2)b3+…+1bd = O(bd)
• For b=10, d=5 N(IDS) = 123,450, N(BFS) = 1,111,100
(9 times more than that of IDS)

Vandana, BITS Pilani, BITS F444/ CS


August 2016 55
F407
Heuristic search strategies
• The uninformed search techniques explore the
nodes systematically and may find a solution, but
are expensive in terms of time and space.
• The informed search strategies are more efficient
as they use problem specific knowledge.
• A heuristic based method chooses the node to
expand next among all nodes to be expanded yet.
• A simplest approach is to choose the best node to
further expand and explore.
Vandana, BITS Pilani, BITS F444/ CS
August 2016 56
F407
Best node

Best node is selected on the basis of the evaluation


function computed for all nodes yet to be expanded

Vandana, BITS Pilani, BITS F444/ CS


August 2016 57
F407
Next Class
• Informed Search (Heuristics Based)
Techniques

Vandana, BITS Pilani, BITS F444/ CS


August 2016 58
F407

You might also like