You are on page 1of 112

Informed Search

INFORMED SEARCH
• All the previous searches have been blind searches .They make no use of any
knowledge of the problem

• For instance, consider a rubik cube which have six faces and each face can be
rotated in three ways (90°, 180°, 270°). Therefore, the each node of rubik has
a branching factor of 18 (6X3).

• If the solution of the rubik cube lies at a depth 20, then nearly 1.25×1025
nodes are generated and searched. A system which can process 1 million
nodes per second will take nearly 40 billion centuries to search the goal node.

• This limitation of blind search algorithms can be overcome by using another


domain dependent function (other than MOVEGEN) called heuristic function
which measures the distance or closeness of each node from the goal node.
Heuristic
• The search algorithms whose path in the search space is guided by the value of heuristic
function are called informed or guided or heuristic search algorithms

• Heuristic functions in searching are human defined functions that is an estimate of the
distance to the goal from the node.

• The idea of using heuristic function is to guide the search, so that it has a tendency to
explore the region leading to the goal.

• A heuristic function is designed to help the algorithm to pick the candidate from the
OPEN list that is most likely to be on the path of goal.

• To incorporate heuristic values in search, the node pair values in the search, the node
pair representation will be augmented with the heuristic value such as
searchNode= (currentState, parentState, heuristicValue)
Heuristic
• There is no general theory for finding heuristics, because every problem is different

• Choice of heuristics depends on knowledge of the problem space

• A heuristic may find a sub-optimal solution or fail to find a solution since it uses limited
information

• In search algorithms, heuristic refers to a function that provides an estimate of solution


cost

• HEURISTIC INFORMATION: Information about the problem (the nature of the states,
the cost of transforming from one state to another , the promise of taking a certain path,
and the characteristics of the goals).can sometimes be used to help guide the search more
efficiently.

• They help to reduce the number of alternatives from an exponential number to a


polynomial number and , thereby , obtain a solution in a tolerable amount of time.
4
HEURISTIC FUNCTION EXAMPLE 1- ROUTE
FINDING PROBLEMS
In a route finding problem in a city, the heuristic function
could be measure of a distance of the node from the goal node.
If the location of each node is in terms of its co-ordinates,
then one heuristic function could be Euclidean distance of
the node from goal node.
H(n)=Euclidean Distance(n ,goal)=
This function gives an estimate of the distance and the actual
distance is more than the straight distance.
Another distance metric that could be used is the
Manhattan distance or the city block distance (assumes
edges from a grid as the streets in the Manhattan)
H(n)=Manhattan Distance(n ,goal)=|xgoal-xn|+|ygoal-yn|
Similarly, we can use other distance measure such as
Minkowski distance.
HEURISTIC FUNCTION EXAMPLE 2- EIGHT
PUZZLE PROBLEM
In a 8-puzzle problem, following heuristic functions can be defined:
H1(n): number of misplaced tiles in n as compared to the goal node.
H2(n): number of correctly placed tiles in n as compared to the goal
node.
H3(n):the sum of Manhattan distance of each tile from the destination.
For instance, for the node (n) and goal node given below,
2 3 1 2 3
n: 1 8 4 Goal: 8 4
7 6 5 7 6 5
The values of three heuristic function defined above are:
H1(n) = 3 (1,2, 8 are not at correct positions)
H2(n) = 5 (3,4,5,6,7 are at correct position)
H3(n) = 1 (for tile 1) + 1 (for tile 2) + 0 +0+0+0+0 +1 (for tile 8) =3
INFORMED SEARCHING ALGORITHMS
The various types of informed searching algorithms
are:
Best first Search
Hill Climbing
Simple Hill Climbing
Steepest Hill Climbing
Beam Search
A* Searching
Iterative Deepening A* Searching
HILL CLIMBING
The algorithm generally moves up in the direction of
better value of the heuristic function i.e. uphill/downhill
(uphill in maximization and downhill in minimization) .
It breaks its “moving up/down loop” when it reaches its
peak where no neighbor has a better value.
It does not maintain a search tree.
It only looks for the immediate neighbor of the current
state. It only stores the current node state and its
heuristic function value.
There are two variants of Hill Climbing: Simple Hill
Climbing and Steepest Hill Climbing.
SIMPLE HILL CLIMBING
It examines the neighboring nodes one by one and selects
the first neighboring node which optimizes the current
cost as next node.
Simple Hill Climbing algorithm works as follows:
Chooses the head node of OPEN as current node.
If it is a goal node then it returns success.
Otherwise, it randomly generates a new next node and checks
if its value is better than current node or not. If it is better
than current then it is added to OPEN otherwise a different
operator is applied.
The process continues until a new node is added to OPEN or
no operator is left to be applied.
SIMPLE HILL CLIMBING-ALGORITHM
ALGORITHM
Insert the start node in OPEN
Loop until success is returned or OPEN is empty
Node (N,P,HN)← Remove-Head(OPEN)
If N is goal node then return success & path to N
Else
Loop until a new node is added to OPEN or no operator is left
Apply an operator (not already applied) to generate NEXT node
If h(NEXT) is better than h(N) then
add NEXT to OPEN and N to CLOSED
End If
End Loop
End If
End Loop
SIMPLE HILL CLIMBING-FLOW CHART
Insert Start Node to OPEN

CURRENT ← Remove-Head(OPEN)

Goal Yes Report the solution and Exit


State
No

Apply an operator O to get NEXT state

Better YES Add NEXT to OPEN and


than
current CUURENT to CLOSED
NO

Another
YES O left?
NO

Report failure and quit


EXAMPLE – SIMPLE HILL CLIMBING
Consider the following solution space:

4
A

B C
5 7

D E F G
5 7 3 8

Let A is the initial node with heuristic function vale 4 and suppose
the better state is the one where heuristic value is greater than its
parent state.
EXAMPLE – SIMPLE HILL CLIMBING CONTD..
First, successor B is produced by applying an operator. It will be
checked for goal state. If it is yes , it will be reported else its
heuristic function value will be checked. Its heuristic function
value is 5. It is better than parent node. So it will be new state.
Successor C will not be generated.
The successor D is produced after applying an operator on node
B. It will be checked for goal state. If it is yes , it will be reported
else its heuristic function value will be checked. Its heuristic
function value is 5. It is equal to parent node. So, the state will be
rejected.
Another successor E is produced after applying another operator
on node B. It will be checked for goal state. If it is yes , it will be
reported else failure will be returned.
If goal state of the problem is G, then simple hill climbing will
not produce the solution with the selected heuristic function.
Thus, the algorithm will find the goal node depending upon the
heuristic function. One heuristic may find it and other may not.
(It is depicted in the example given in next slide)
PROBLEM 1
Find the solution to the following blocks world
problem using simple hill method. State the
heuristics used.

A D

D C

C B

B A

Initial State Final State


SOLUTION-PROBLEM 1
Let the heuristic be +1 if the block is resting on the
correct block and -1 if it is resting on the incorrect
block.
HF(Initial State, S) = -1(A)-1 (B) +1(C) +1 (D) =0
Successor of Initial state
Step 1:I state:
D
C
A HF = = 1(A)-1 (B) +1(C) +1 (D) =2
B

Better than initial state. Therefore Current state=First State

Step 2: II state
HF = 1(A)-1 (B) +1(C) -1 (D) =0
C
B A D

HF(II) < HF(I). So state rejected. Apply another operator on state I.


SOLUTION-PROBLEM 1 CONTD…
III state:

C D HF = 1(a) -1(b) +1(c) -1(d) = 0


B A

Again HF(III)< HF(I) and no other operator is possible on state I. Therefore, return
fail.
The contents of OPEN and CLOSED at each step according to first
heuristic is:
Step OPEN CLOSED
1 {(S,Φ,0)} { }
2 {(1,S,2)} {(S,Φ)}
3 {} {(S,Φ),(1,S)}
SOLUTION-PROBLEM 1 CONTD…

Let another heuristic be :


+n for block which is resting on the current support
structure and n is equal to number of blocks
below it.
- n for block which is resting on the incurrent
support structure and n is equal to number of
blocks below it.
HF (Initial State,S) = -3 (A) -0 (B)-1 (C) -2 (D) = -6
SOLUTION-PROBLEM I CONTD…
Step 1: I state
D
C
B A
HF(I) = 0 (A) -0 (B) -1 (C) -2 (D) = -3
HF(I) > HF(Initial State). Therefore I state is current
state and an operator is applied on it.
Step2: II state

C
D B A
HF(II) = 0 (A) -0 (B) -1 (C) -0 (D) = -1
HF(II) > HF(I). Therefore II state is current state and an
operator is applied on it.
SOLUTION-PROBLEM I CONTD….
Step 3: III state

C D B A

HF(III) = 0 (A) -0 (B) -0 (C) -0 (D) = 0


HF(III) > HF(II). Therefore III state is current state and
an operator is applied on it.
Step 4: IV state
B

D C A

HF(II) = 0 (A) +1 (B) -0(C) -0 (D) = 1


HF(IV) > HF(III). Therefore IV state is current state and
an operator is applied on it.
SOLUTION-PROBLEM I CONTD…
Step 5: V state C
B
D A

HF(V) = 0 (A) + 1 (B) + 2 (C) -0 (D) = 3


HF(V) > HF(IV). Therefore V state is current state and
an operator is applied on it.
Step 6: VI state D
C
B
A

VI state is goal state. So report it and stop.


SOLUTION-PROBLEM I CONTD…
The contents of OPEN and CLOSED according to
second heuristic are as follows:

Step OPEN CLOSED


1 {(S,Φ,-6)} { }
2 {(I,S,-3)} {(S,Φ)}
3 {(II,I,-1)} {(S,Φ)(I,S)}
4 {(III,II,0)} {S,Φ)(I,S)(II,I}
5 {(IV,III,1)} {(S,Φ)(I,S)(II,I)(III,II)}
6 {(V,IV,3)} {(S,Φ)(I,S)(II,I)(III,II)(IV,III)}
7 {(VI,V,6)} {(S,Φ)(I,S)(II,I)(III,II)(IV,III)(V,IV)}
8 { } {(S,Φ)(I,S)(II,I)(III,II)(IV,III)(V,IV)(VI,V)}
PROBLEM - II
Solve the following 8-puzzle problem using simple hill
climbing problem. State the heuristic used.

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

Initial State Final State


SOLUTION- PROBLEM II
Let the heuristic be number of misplaced tiles from goal
state. Any value of HF less than the parent value is a
better state.
Step1: Initial State(S):
2 3
1 8 4
7 6 5 HF = 1 (for 1) +1 (for 2) + 1 (for 8) = 3

Step 2: I state:

2 3
1 8 4
7 6 5
HF = 1 (for 1) + 1 (for 8) =2
HF(I) < HF (initial state) . Therefore I state, is current state and new state is
produced after applying a valid operator.
SOLUTION- PROBLEM II CONTD….
Step 3: II State:
1 2 3
8 4
7 6 5 HF = 1 (for 8) = 1

HF(II) < HF(I) . Therefore II state, is current state and new state is produced after
applying a valid operator.

Step 4: III state


1 2 3
8 4
7 6 5
Goal State. Therefore report it and stop
SOLUTION-PROBLEM I CONTD…
The contents of OPEN and CLOSED are as follows:

Step OPEN CLOSED


1 {(S,Φ,3)} { }
2 {(I,S,2)} {(S,Φ)}
3 {(II,I,1)} {(S,Φ)(I,S)}
4 {(III,II,0)} {S,Φ)(I,S)(II,I}
5 {} {(S,Φ)(I,S)(II,I)(III,II)}
STEEPEST HILL CLIMBING
Simple hill climbing method, selects the first
state which is better then the current state
and does not explore the rest of the states at
that level which could be even better than the
selected better state.
This problem is overcome in steepest hill
climbing that selects the best among the
children state as the current state.
Steepest hill climbing is also called gradient
search.
STEEPEST HILL CLIMBING
It first examines all the neighboring nodes and then
selects the node closest to the solution state as next node.
Simple Hill Climbing algorithm works as follows:
Chooses the head node of OPEN as current node and add it to
CLOSED.
If it is a goal node then it returns success.
Otherwise, it applies all operators on the current state and
picks up the best node. If it the best node is better than
current then it is added to OPEN.
The process continues until OPEN is empty or success is
returned.
STEEPEST HILL CLIMBING-ALGORITHM
ALGORITHM
Insert the start node in OPEN
Loop until success is returned or OPEN is empty
Node (N,P,HN)← Remove-Head(OPEN) and add it to CLOSED
If N is goal node then return success & path to N
Else
Apply all operators to generate NEW nodes and pick the best node
i.e. NEXT ← maxh(append(NEW, OPEN))
If h(NEXT) is better than h(N) then add NEXT to OPEN
End If
End Loop
STEEPEST HILL CLIMBING-FLOW CHART
Insert Start Node to OPEN

CURRENT ← Remove-Head(OPEN) and


add it to CLOSED

Goal Yes Report the solution and Exit


State
No

Apply operators (0ne by one) to get


successors and pick best node as NEXT
node

Better YES
than Add NEXT to OPEN
current

NO

Report failure and quit


EXAMPLE – STEEPEST HILL CLIMBING
Consider the following solution space:

4
A

B C
5 7

D E F G
5 7 3 8

Let A is the initial node with heuristic function vale 4 and suppose
the better state is the one where heuristic value is greater than its
parent state.
EXAMPLE – STEEPEST HILL CLIMBING CONTD..
First, successors B and C will be produced by applying
both operators. C has a higher value of heuristic
function. So it will be selected. It will be checked for
goal state. If it is yes , it will be reported else C will be
set as new current state.
The successor F and G are produced after applying
operators on node C. G has higher value of heuristic
function. So it will be selected. It will be checked for
goal state. If it is yes , it will be reported else failure
will be reported.
If goal state of the problem is D or E, then steepest
hill climbing will not produce the solution with the
selected heuristic function.
PROBLEM - I
Solve the following 8-puzzle problem using steepest hill
climbing problem. State the heuristic used.

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

Initial State Final State


SOLUTION- PROBLEM I

2 3

(A,Φ
Φ,3) 1 8 4 Step 1:OPEN={(A,Φ
Φ,3)} CLOSED ={ }
7 6 5

(B,A,2) (C,A,4) (D,A,3)


2 3 2 3 2 8 3 Step 2: OPEN ={(B,A,2)}
1 8 4 1 8 4 1 4 CLOSED={(A,Φ
Φ)}
7 6 5 7 6 5 7 6 5

(E,B,1)
1 2 3 Step 3: OPEN={(E,B,1)} CLOSED=((A,Φ
Φ),(B,A)}
8 4
7 6 5
(F,E,0) (G,E,2)

1 2 3 1 2 3
Step 4: OPEN={(F,E,0)} CLOSED=((A,Φ
Φ),(B,A)(E,B)}
8 4 7 8 4
Step 5: OPEN={ } CLOSED=((A,Φ
Φ),(B,A)(E,B)(F,E)}
7 6 5 6 5
PROBLEM- II
Find the solution to the following blocks world
problem using steepest hill method. State the
heuristics used.

C
A B
C B A

Initial State Final State


SOLUTION- PROBLEM-II

Let heuristic be :
+ score for block which is resting on the current
support structure and that score is equal to
number of blocks below it.
- score for block which is resting on the incurrent
support structure and that score is equal to
number of blocks below it.
SOLUTION- PROBLEM -II
HF= -1-0-0 = -1 A State 1
C B
B
A
HF=0 State 2 HF= -3
C B A C

A B B C C
C B A C A C A B A B
HF= -1 HF= 1
State 3 HF= -1 HF= -1 HF= -1

C
B
A State 4
SOLUTION- PROBLEM -II
The contents of OPEN and CLOSED are as
follows:

Step OPEN CLOSED


1 {(1,Φ,-1)} { }
2 {(2,1,0)} {(1,Φ)}
3 {(3,2,1)} {(1,Φ)(2,1)}
4 {(4,3,3)} {(1,Φ),(2,1),(3,2)}
5 { } {(1,Φ),(2,1),(3,2)(4,3)}
LIMITATIONS OF HILL CLIMBING ALGORITHMS

Both simple hill climbing and steepest hill climbing


methods may fail to provide a solution by reaching a
state from which no subsequent improvement can be
made and this state is not the solution (goal state).
This will happen if the program has reached local
maximum, a plateau or a ridge.
Local Maximum is a state that is better than all its
neighbor but is not better than some other father state.
At local maximum, all moves appear to make things
worse.
Local
Maximum
LIMITATIONS OF HILL CLIMBING ALGORITHMS
CONTD….
Solution to problem of local maximum can be
backtracking to some earlier node and try going to a
different direction.
This is particularly reasonable if at that node there was
another direction that looked as promising as the one
that was earlier chosen.
To implement this strategy, maintain a list of paths
taken and go back to one of them is the path that was
taken leads to dead end.
LIMITATIONS OF HILL CLIMBING ALGORITHMS
CONTD….

A plateau is another problem in hill climbing


algorithms . A plateau is a flat area of the search space
in which a whole set of neighboring states have the
same value
On a plateau, it is not possible to determine the best
direction in which to move by making local
comparisons.
Plateau

If we are caught in this type of problem, then make a


big jump to get new section of the search space.
If the only rules available describe single small steps,
apply them several times in same direction.
LIMITATIONS OF HILL CLIMBING ALGORITHMS
CONTD….
A ridge is another problem in hill climbing algorithms.
A ridge is a special kind of local maximum. It is an area of the
search space that is higher that the surrounding areas and that
itself has a slope.
But the orientation of the high region, compared to the set of
available moves and the directions in which they move, makes it
impossible to traverse a ridge by single moves.
In order to get through this problem, apply two or more rules
before doing the test and this corresponds to moving in special
directions.
PERFORMANCE OF HILL CLIMBING ALGORITHM
Completeness:
The hill climbing algorithms are not complete.
The completeness of the hill climbing algorithm depends
upon the quality of heuristic function.
A heuristic function may take the search to the global
maxima or minima or may stuck into local
maxima/minima, plateau, or ridge.
Optimality:
Like Best First Search, the optimality of hill climbing
algorithm depends upon the heuristic function.
Search Complexity:
This is the strongest feature of hill climbing.
It requires a constant amount of space as it need to store
only copy of the current state.
PERFORMANCE OF HILL CLIMBING ALGORITHM
CONTD….

Time Complexity:
In hill climbing algorithm, each node generate at the
most b nodes.
Therefore, the time complexity grows linearly and is of
the order O(bd) where b is the branching factor and d is
the depth at which solution lies.
Best-First Search
• Depth-first search: not all competing branches
having to be expanded.

• Breadth-first search: not getting trapped on dead-


end paths.
 Combining the two is to follow a single path
at a time, but switch paths whenever some
competing path look more promising than the
current one.

28
Best-First Search
A A A

B C D B C D
3 5 1 3 5
E F
4 6
A A

B C D B C D
5 5
G H E F G H E F
6 5 4 6 6 5 6
I J
2 1
29
Best-first Vs Hill Climbing
hill climbing best-first search
• In hill climbing, one move • one move is selected, but
is selected and all the others the others are kept around so
are rejected and are never that they can be revisited
reconsidered. later if the selected path
becomes less promising

• It stop if there are no • the best available state is


successor states with better selected in best-first search,
values than the current state. even if that state has a value
that is lower than the value
of the state that was just
explored.
BEST FIRST SEARCH
The best first search algorithm picks the most
promising node (in terms of heuristic value) from the
OPEN list in every iteration until the goal node is
found.
It maintains the OPEN list as a priority queue sorted
on the heuristic value, so that the node with the best
heuristic value automatically comes to the head of the
list.
The algorithm then picks up the node from the head of
OPEN as before.
Best-First Search
• To get the path information, Each node will contain, in
addition to a description of the problem state it represents,
– an Indication of how promising it is,
– a parent link that points back to the best node from which it
came,
– a list of the nodes that were generated from it.

• Once the goal is found the parent link will make it possible to
recover the path to the goal.

• The list of successors will make it possible, if a better path is


found to an already existing node, to propagate the
improvement down to its successors.
31
Best First Search
• We will call a graph of this sort an OR graph, since each of its
branches represents an alternative problem-solving path.

• To implement such a graph-search procedure, we will need to use


two lists of nodes:
• OPEN: nodes that have been generated, but have not examined. This
is organized as a priority queue in which the elements with the
highest priority are those with the most promising value of the
heuristic function

• CLOSED: nodes that have already been examined. It used,


whenever a new node is generated, check whether it has been
generated before.
Best-First Search
Algorithm
1. Start with OPEN containing just the initial state.
2. Until a goal is found or there are no nodes left on OPEN do:
I. Pick the best node on OPEN.
II. Generate its successors.
III. For each successor do:
a. If it has not been generated before, evaluate it, add it to
OPEN, and record its parent.
b. If it has been generated before, change the parent if this
new path is better than the previous one. In that case,
update the cost of getting to this node and to any
successors that this node may already have.

33
BEST FIRST SEARCH-ALGORITHM
OPEN- Priority Queue with generated nodes of the form
(Node,Parent,Heuristic) sorted on the values of Heuristic.
CLOSED- Queue of expanded nodes; initially empty
ALGORITHM
Insert the start node in OPEN
Loop until success is returned or OPEN is empty
Node (N,P,HN)← Remove-Head(OPEN) and add it to CLOSED
If N is goal node return success & path to N
Else
Generate NEW successors of N, append it to OPEN
and sort the list in order of heuristic values
i.e. OPEN←sorth(append(NEW,OPEN))
where NEW= {MOVEGEN(N)\{OPEN∪CLOSED}}
End If
End Loop
Best-First Search
• Best-First Search: use a heuristic function of following form:
f(n) = g(n) + h(n)
 f(n) = measures the value of the current state (its “goodness”).
 h(n) = the estimated cost of the cheapest path from node n to a goal state.
 g(n) = the exact cost of the cheapest path from the initial state to node n.

34
Greedy Best First Search

• This search is the basic or simple best first


search strategy which ignore the g(n)

• Greedy search:
f(n)=h(n) ; i.e. g(n) = 0. it means the estimated
cost of the cheapest path from node n to a goal
state.
Neither optimal nor complete

35
Example-1
f(n) = g(n) + h (n)
Start g(n): is the exact cost to reach node n from the
A 75
118 initial state.
B State Heuristic: h(n)
C 140
111 A 366
E
B 374
D 80 99
C 329
G F D 244
97 E 253
H F 178
211
101 G 193
I H 98
Goal
I 0
36
Greedy Best First Search
This search ignore g(n), hence
f(n) = h (n) = straight-line distance heuristic
Start
A

B
C State Heuristic: h(n)

E A 366
D B 374
C 329
G F
D 244
E 253
H
F 178
G 193
I
Goal H 98

graph used by greedy Best First Search I 0 37


Greedy Best First Search
Start State Heuristic: h(n)
A
A 366

C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
38
Greedy Best First Search
Start State Heuristic: h(n)
A
A 366

C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
39
Greedy Best First Search
Start State Heuristic: h(n)
A
A 366

C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
40
Greedy Best First Search
Start State Heuristic: h(n)
A
A 366

C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
41
Greedy Best First Search
Start State Heuristic: h(n)
A
A 366

C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
42
Greedy Best First Search
Start State Heuristic: h(n)
A
A 366

C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
43
Greedy Best First Search
Start State Heuristic: h(n)
A
A 366

C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
44
Greedy Best First Search
Start State Heuristic: h(n)
A
A 366

C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
45
Greedy Best First Search
Start State Heuristic: h(n)
A
A 366

C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
46
Greedy Best First Search State h(n)
A 366
B 374
Start C 329
A
D 244
E 253
F 178
G 193
H 98
I 0

47
Greedy Search: Tree Search State h(n)
A 366
B 374
Start C 329
A
D 244
[329] [374] B E 253
C

[253] E F 178
G 193
H 98
I 0

48
Greedy Search: Tree State h(n)
Search A 366
B 374
Start C 329
A
D 244
[329] [374] B E 253
C

[253] E F 178
G 193

[193] H 98
G F [178]
[366] A I 0

49
Greedy Search: Tree Search
State h(n)
A 366
Start
A B 374
C 329
[329] [374] B
C D 244
[253] E E 253
F 178
[193] [178] G 193
G F
[366] A H 98
I 0

[253] I [0]
E

Goal

50
Greedy Search: Tree Search State h(n)
A 366
B 374
Start C 329
A
D 244
[329] [374] B E 253
C

[253] E F 178
G 193

[193] H 98
G F [178]
[366] A I 0

[253] I [0]
E

Goal
Exact cost to reach to goal: dist(A-E-F-I) = 140 + 99 + 211 = 450 51
Greedy Best First Search :
Optimal ? NO
Start State Heuristic: h(n)
A
A 366

C
B B 374
C 329
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
dist(A-E-G-H-I) =140+80+97+101=418 52
Greedy Best First Search :
If for some reason, lets say straight distance from C is 250, then what happened?
Also suppose we do not maintain CLOSE list

Start State Heuristic: h(n)


A
A 366

C
B B 374
** C 250
E
D D 244
E 253
G F
F 178
G 193
H H 98
I 0
I
Goal f(n) = h (n) = straight-line distance heuristic
53
Greedy Search: Tree Search
State h(n)

Start A 366
A B 374
C 250
D 244
E 253
F 178
G 193
H 98
I 0

54
Greedy Search: Tree Search
State h(n)
Start
A A 366
B 374
[250] [374] B
C
C 250
[253] E D 244
E 253
F 178
G 193
H 98
I 0

55
Greedy Search: Tree Search
State h(n)
Start
A A 366
B 374
[250] [374] B
C C 250
[253] E D 244
E 253
[244] D
F 178
G 193
H 98
I 0

56
Greedy Search: Tree Search
State h(n)
Start
A A 366
B 374
[250] [374] B
C
C 250
[253] E D 244

[244] D E 253
F 178
Infinite Branch !
G 193
[250] C
H 98
I 0

57
Greedy Search: Tree Search
Start State h(n)
A
A 366
[250] [374] B B 374
C

[253] E C 250
D 244
[244] D
E 253
Infinite Branch ! F 178
[250] C G 193
Not Complete H 98
I 0
[244] D

58
Greedy Search: Time and Space
Complexity ?
Start
A
• Greedy search is not optimal.
B
C
• Greedy search is incomplete
E
without systematic checking of
D
repeated states.(it means, if we do
G F not maintain close list.
• In the worst case, the Time and
H
Space Complexity of Greedy
I
Search are both O(bm)
Goal Where b is the branching factor and m the maximum
path length 59
PROBLEM - I
Solve the following 8-puzzle problem using Best First
Search. State the heuristic used.

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

Initial State Final State


SOLUTION- PROBLEM I
Let the MOVEGEN function generates successors by moving the
blank tile LEFT, RIGHT, UP, DOWN (in the same sequence).
Let the heuristic function be number of misplaced tiles.
The OPEN and CLOSED list while exploring the search space
(shown in next slide) is as follows:

Step OPEN CLOSED


1 {(A,Φ,3)} { }
2 {(B,A,2), (D,A,3), (C,A,4)} {(A, Φ)}
3 {(E,B,1),(D,A,3)(C,A,4)} {(A, Φ), (B,A)}
4 {(F,E,0),(G,E,2),(D,A,3)(C,A,4)} ((A, Φ),(B,A)(E,B)}
5 {(G,E,2),(D,A,3)(C,A,4)} ((A, Φ),(B,A)(E,B)(F,E)}
Therefore, the path is A→B → E → F
SOLUTION- PROBLEM I

2 3

(A,Φ
Φ,3) 1 8 4
7 6 5

(B,A,2) (C,A,4) (D,A,3)


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

(E,B,1)
1 2 3
8 4
7 6 5
(F,E,0) (G,E,2)

1 2 3 1 2 3
8 4 7 8 4
7 6 5 6 5
PERFORMANCE OF BEST FIRST SEARCH
Completeness
The best first search is complete, at least for finite search space.
This is because it explores the nodes in search space until goal
is found or OPEN is empty. The only difference is it explore
nodes in order of their heuristic value.
For infinite search space, the completeness of best first search
depends upon the heuristic function. If the heuristic function is
good enough, the search will end up in goal state.
Optimality
The optimality of the best first algorithm also depends upon the
heuristic function.
The heuristic function may find the optimal path or sub optimal
path to problem.
If two nodes have same heuristic value but one node is
expensive to achieve, the best first search algorithm has no way
of discriminating it.
PERFORMANCE OF BEST FIRST SEARCH CONTD…

Space Complexity
The OPEN (search frontier) for the Best First Search
depends upon the accuracy of heuristic function.
If the heuristic function is accurate then the search will
find goal directly, and the OPEN will grow linearly.
Otherwise, the search algorithm may go down some path,
change its mind, and move to another branch in search
tree.
For most interesting problems, it is difficult to devise
accurate heuristic functions, and thus space grows
exponentially (depicted in the figure in next slide).
Time Complexity
Like space complexity, time complexity is also dependent
on the accuracy of heuristic function and time tends to
grow exponentially for most of the problems.
A* Algorithm
• A* ( pronounced as A- star search) is the most widely and improved
form of the best-first search.

• It evaluates each node n using a function f(n) which is combination


of g(n) and h(n).
f(n) = g(n) + h(n)
– g(n) is the exact cost to reach node n from the initial state. Cost so far
up to node n.
– h(n) is an estimation of the remaining cost to reach the goal.

• The algorithm also maintains OPEN as a priority queue where the


nodes are prioritized according to their f-values.

• Each node in OPEN is maintained as (node, parent,f(node))


A* (A Star)
h(n) is defined by the heuristic function and would never change for any node n. Thus h(n) is
the property of the node.
o g(n) is the estimated cost to reach n from S from a known path. As more and more paths
are learnt, this cost will keep on changing. Thus g(n) changes while the A* algorithm explores
the search space. In general g(n) ³ g*(n) where g*(n) denotes optimized cost to reach n from
S.
S

g(n)

f(n) = g(n)+h(n) n

h(n)

2
OPTIMALITY OF A* ALGORITHM
The necessary conditions for A* algorithm to be
optimal is:
1) The branching factor of the search space is finite.
2) The cost to move from one node to another should be
greater than or equal to zero i.e. cost(i,j) ≥ 0 for all i, j.
3) The heuristic is an admissible heuristic. An
admissible heuristic h(n) is a heuristic that
always underestimates the cost to reach the goal
i.e. h(n) ≤ h*(n) where h*(n) is the optimized cost
to reach goal from n.
In other words, for every node n, the cost to reach the
goal provided by the heuristic function is never greater
than the optimized cost to reach the goal.
If the above mentioned conditions are not satisfied
then the algorithm is A algorithm but not A* (* for
any search algorithm denotes optimal algorithm).
A* ALGORITHM PSEUDOCODE
Insert start node (S,φ,f(S)) to OPEN
Loop until OPEN is empty or success is returned
(n,p,f(n)) ← remove-head(OPEN) and add it CLOSED
If n is a goal node return success and path to n.
else
successors ← MOVEGEN(n)
for each successor m do
switch case
case 1: if m ∉ OPEN and m ∉ CLOSED
compute g(m) = g(n) + cost (n,m) and h(m)
compute f(m)=g(m) + h(m)
add (m,n,f(m)) to OPEN according to priority of f(m)
case 2: if m ∈ OPEN
compute newg(m)=g(n)+cost(n,m)
if newg(m)<g(m) then
update g(m) =newg(m) and f(m)=g(m)+h(m)
update (m,n,f(m)) to OPEN according to priority of f(m)
end if
case 3: if m ∈ CLOSED
compute newg(m)=g(n)+cost(n,m)
if newg(m)<g(m) then
update g(m) = newg(m) and f(m)=g(m)+h(m)
update (m,n,f(m)) to CLOSED according to priority of f(m)
propogateimprovement(m)
end if
end for
end if
end loop
A* ALGORITHM PSEUDOCODE CONTD….
propagateimprovement(m)
for each successor s of m
compute newg(s)=g(m) + cost(m,s)
if newg(s) < g(s) then
update g(s)=newg(s)
update f(s)=g(s) + h(s)
update (s,m,f(s)) in OPEN or CLOSED
if s ∈ CLOSED then
propogateimprovement(s)
end if
end if
end for
A* Search
Start State Heuristic: h(n)
A 75
118 A 366
140 B B 374
C
111 C 329
E
D 80 99 D 244
E 253
G F
F 178
97
G 193
H 211 H 98
101 I 0
I
Goal f(n) = g(n) + h (n)
g(n): is the exact cost to reach node n from the initial state. 3
A* Search: Tree Search
State h(n)

A 366 A Start

B 374

C 329

D 244

E 253

F 178

G 193

H 98

I 0

4
A* Search: Tree Search
State h(n)

A 366 A Start

B 374 118 75
140
C 329
E [393] B [449]
D 244 [447] C
E 253

F 178

G 193

H 98 C[447] = 118 + 329


I 0 E[393] = 140 + 253
B[449] = 75 + 374

5
A* Search: Tree Search
State h(n)

A 366 A Start

B 374 118 75
140
C 329
E [393] B [449]
D 244 [447] C
80 99
E 253

F 178 [413] G F [417]


G 193

H 98

I 0

6
A* Search: Tree Search
State h(n)

A 366 A Start

B 374 118 75
140
C 329
E [393] B [449]
D 244 [447] C
80 99
E 253

F 178 [413] G F [417]


G 193
97
H 98
[415] H
I 0

7
A* Search: Tree Search
State h(n)

A 366 A Start

B 374 118 75
140
C 329
E [393] B [449]
D 244 [447] C
80 99
E 253

F 178 [413] G F [417]


G 193
97
H 98
[415] H
I 0
101
Goal I [418]

8
A* Search: Tree Search
State h(n)

A 366 A Start

B 374 118 75
140
C 329
E [393] B [449]
D 244 [447] C
80 99
E 253

F 178 [413] G F [417]


G 193
97
H 98
[415] H I [450]
I 0
101
Goal I [418]

9
A* Search: Tree Search
State h(n)

A 366 A Start

B 374 118 75
140
C 329
E [393] B [449]
D 244 [447] C
80 99
E 253

F 178 [413] G F [417]


G 193
97
H 98
[415] H I [450]
I 0
101
Goal I [418]

10
A* Search: Tree Search
State h(n)

A 366 A Start

B 374 118 75
140
C 329
E [393] B [449]
D 244 [447] C
80 99
E 253

F 178 [413] G F [417]


G 193
97
H 98
[415] H I [450]
I 0
101
Goal I [418]

11
EXAMPLE I
Consider the following search space with start node S
and goal node G.

100 A 40

S G
50
100 B

Consider following two heuristics defined for the problem:


Heuristic 1: h1(S)=110, h1(A)= 80, h1(B) =70
Heuristic 2: h2(S)=110, h2(A)= 30, h2(B) =20
Which of the two heuristic is admissible?
Using A* algorithm find the path from S to G using
both heuristics and show that the path found using
admissible heuristic is the optimal one?
EXAMPLE 1 – SOLUTION
Node Cost to reach goal from different paths Optimal Cost
S 140, 150 140 = h*(S)
A 40, 250 40 = h*(A)
B 50, 240 50 = h*(B)

As given,
Heuristic 1: h1(S)=110, h1(A)= 80, h1(B) =70
Heuristic 2: h2(S)=110, h2(A)= 30, h2(B) =20
For every node, heuristic 2 underestimates the optimal cost to reach
goal whereas heuristic 1 is overestimating the optimal cost for nodes A
and B.
Therefore, heuristic 2 is admissible and heuristic 1 is not
EXAMPLE 1 – SOLUTION CONTD….

Path using heuristic 1


g(S)=0 , h1(S)=110 ; f(S)= 0+110
Add (S,φ,0+110) to OPEN and CLOSED is empty
Iteration OPEN CLOSED
0 {(S,φ,0+110)} {}
Iteration I
Remove head node (S,φ,0+110) from OPEN and add to CLOSED.
Since S is not goal node, therefore successors of S, A and B are
produced (case 1: both are new not in OPEN and CLOSED)
Successors of S:
A :g(A)=g(S)+cost(S,A) = 0+100 =100; h1(A) = 80 , f(A) = 100+80
B : g(B)=g(S)+cost(S,B) = 0+100 =100; h1(B) = 70 , f(B) = 100+70
Iteration OPEN CLOSED
0 {(S,φ,0+110)} {}
1 {(B,S,100+70)(A,S,100+80)} {(S,φ,0+110)}
EXAMPLE 1 – SOLUTION CONTD….

Iteration II
Remove head node (B,S,100+70) from OPEN and add to CLOSED.
Since B is not goal node, therefore successors of B i.e. S and G are
produced (for S it is case 3 as it is already in CLOSED and for G it is
case 1 which is not in OPEN and CLOSED)
Successors of B:
S :newg(S)=g(B)+cost(B,S) = 100+100 =200;
since newg(S) is not less than g(S), so this successor is ignored
G: g(G)=g(B)+cost(B,G) = 100+50=150; h1(G) = 0 , f(G) = 150 + 0
Iteration OPEN CLOSED
0 {(S,φ,0+110)} {}
1 {(B,S,100+70)(A,S,100+80)} {(S,φ,0+110)}
2 {(G,B,150+0)(A,S,100+80)} {(S,φ,0+110) (B,S,100+70)}
EXAMPLE 1 – SOLUTION CONTD….

Iteration III
Remove head node (G,B,150+0) from OPEN and add to CLOSED.
Since G is goal node, therefore algorithm will stop and path
S→B → G is returned with path cost 150.
EXAMPLE 1 – SOLUTION CONTD….

Path using heuristic 2


g(S)=0 , h2(S)=110 ; f(S)= 0+110
Add (S,φ,0+110) to OPEN and CLOSED is empty
Iteration OPEN CLOSED
0 {(S,φ,0+110)} {}
Iteration I
Remove head node (S,φ,0+110) from OPEN and add to CLOSED.
Since S is not goal node, therefore successors of S, A and B are
produced (case 1: both are new not in OPEN and CLOSED)
Successors of S:
A :g(A)=g(S)+cost(S,A) = 0+100 =100; h2(A) = 30 , f(A) = 100+30
B : g(B)=g(S)+cost(S,B) = 0+100 =100; h2(B) = 20 , f(B) = 100+20
Iteration OPEN CLOSED
0 {(S,φ,0+110)} {}
1 {(B,S,100+20)(A,S,100+30)} {(S,φ,0+110)}
EXAMPLE 1 – SOLUTION CONTD….

Iteration II
Remove head node (B,S,100+20) from OPEN and add to CLOSED.
Since B is not goal node, therefore successors of B i.e. S and G are
produced (for S it is case 3 as it is already in CLOSED and for G it is
case 1 which is not in OPEN and CLOSED)
Successors of B:
S :newg(S)=g(B)+cost(B,S) = 100+100 =200;
since newg(S) is not less than g(S), so this successor is ignored
G : g(G)=g(B)+cost(B,G) = 100+50=150; h2(G) = 0 , f(G) = 150 + 0
Add (G,B,150+0) to OPEN after (A,S,100+30)

Iteration OPEN CLOSED


0 {(S,φ,0+110)} {}
1 {(B,S,100+20)(A,S,100+30)} {(S,φ,0+110)}
2 {(A,S,100+30) (G,B,150+0)} {(S,φ,0+110) (B,S,100+20)}
EXAMPLE 1 – SOLUTION CONTD….

Iteration III
Remove head node (A,S,100+30) from OPEN and add to CLOSED.
Since A is not goal node, therefore successors of A i.e. S and G are
produced (for S it is case 3 as it is already in CLOSED and for G it is
case 2 which is already in OPEN)
Successors of A:
S: newg(S) = g(A)+cost(A,S)= 100 + 100 = 200
since newg(S) is not less than g(S), so this successor is ignored
G : newg(G)=g(A)+cost(A,G) = 100+40=140; h2(G) = 0 , f(G) = 140 + 0
since newg(G) is less than g(G), therefore update (G,A,140+0) in
OPEN
Iteration OPEN CLOSED
0 {(S,φ,0+110)} {}
1 {(B,S,100+20)(A,S,100+30)} {(S,φ,0+110)}
2 {(A,S,100+30) (G,B,150+0)} {(S,φ,0+110) (B,S,100+20)}
3 {(G,A,140+0)} {(S,φ,0+110) (B,S,100+20) (A,S,100+30) }
EXAMPLE 1 – SOLUTION CONTD….

Iteration IV
Remove head node (G,A,140+0) from OPEN and add to CLOSED.
Since G is goal node, therefore algorithm will stop and path
S→A → G is returned with path cost 140.

Thus, the path returned by heuristic 2 (admissible heuristic) is


optimal.
EXAMPLE 2

For the search space shown below, find the optimal


path from S to D using the heuristic values defined in
table.

S
Node Heuristic Value
1
4 S 7
A 6
A 2 B B 2
5 2 C 1
12
D 0
D C
3
EXAMPLE 2 – SOLUTION

g(S)=0 , h(S)=7 ; f(S)= 0+7


Add (S,φ,0+7) to OPEN and CLOSED is empty
Iteration OPEN CLOSED
0 {(S,φ,0+7)} {}
Iteration I
Remove head node (S,φ,0+7) from OPEN and add to CLOSED.
Since S is not goal node, therefore successors of S i.e. A and B are
produced (case 1: both are new not in OPEN and CLOSED)
Successors of S:
A :g(A)=g(S)+cost(S,A) = 0+1 =1; h(A) = 6 , f(A) = 1+6
B : g(B)=g(S)+cost(S,B) = 0+4 =4; h(B) = 2 , f(B) = 4+2
Iteration OPEN CLOSED
0 {(S,φ,0+7)} {}
1 {(B,S,4+2) (A,S,1+6)} {(S,φ,0+7)}
EXAMPLE 2 – SOLUTION CONTD….
Iteration II
Remove head node (B,S,4+2) from OPEN and add to CLOSED.
Since B is not goal node, therefore successors of B i.e. S ,A and C are produced (for S it
is case 3 as it is already in CLOSED, for A it is case 2 as it is already in OPEN and for
C it is case 1 which is not in OPEN and CLOSED)
Successors of B:
S :newg(S)=g(B)+cost(B,S) = 4+4=8;
since newg(S) is not less than g(S), so this successor is ignored
A : newg(A)=g(B)+cost(B,A) = 4+2=6;
since newg(A) is not less than g(A), so this successor is ignored
C: g(C)=g(B) +cost(B,C) = 4 + 2 =6, h(C) =1 f(C)=6+1
Add (C,B,6+1) after (A,S,1+6) to OPEN [node generated before has higher priority in
case of same f values]

Iteration OPEN CLOSED


0 {(S,φ,0+7)} {}
1 {(B,S,4+2) (A,S,1+6)} {(S,φ,0+7)}
2 {(A,S,1+6)(C,B,6+1)} {(S,φ,0+7) (B,S,4+2)}
EXAMPLE 2 – SOLUTION CONTD….
Iteration III
Remove head node (A,S,1+6) from OPEN and add to CLOSED.
Since A is not goal node, therefore successors of A i.e. S,B,D and C are produced (for S and B it is case 3
as it is already in CLOSED, for C it is case 2 which is already in OPEN, and for D it is case 1 as it is not
in OPEN or CLOSED)
Successors of A:
S: newg(S) = g(A)+cost(A,S)= 1 + 1 = 2
since newg(S) is not less than g(S), so this successor is ignored
B : newg(B)=g(A)+cost(A,B) = 1+2=3; h(B) = 2 , f(G) = 3+2
since newg(B) is less than g(B), therefore update (B,A,3+2) in CLOSED and propagate improvement to
Child C as (C,B,5+1) in OPEN
C: newg(C)= g(A) + cost (A,C) = 1+5 =6
since newg(C) is not less than g(C), so this successor is ignored
D: g(D) =g(A) +cost(A,D) = 1+12 =13, h(D)= 0, f(D)=13+0
ADD (D,A,13+0) after (C,B,5+1) to OPEN

Iteration OPEN CLOSED


0 {(S,φ,0+7)} {}
1 {(B,S,4+2) (A,S,1+6)} {(S,φ,0+7)}
2 {(A,S,1+6)(C,B,6+1)} {(S,φ,0+7) (B,S,4+2)}
3 {(C,B,5+1) (D,A,13+0)} {(S,φ,0+7) (B,A,3+2) (A,S,1+6) }
EXAMPLE 2 – SOLUTION CONTD….
Iteration IV
Remove head node (C,B,5+1) from OPEN and add to CLOSED.
Since C is not goal node, therefore successors of C i.e. A ,B and D are produced (for A and B it is case 3 as
it is already in CLOSED, for D it is case 2 which is already in OPEN)
Successors of C:
A: newg(A) = g(C)+cost(C,A)= 5 + 5 = 10
since newg(A) is not less than g(A), so this successor is ignored
B : newg(B)=g(C)+cost(C,B) = 5+2=7;
since newg(B) is not less than g(B), so this successor is ignored
D: g(D) =g(C) +cost(C,D) = 5+3 =8, h(D)= 0, f(D)=8+0
UPDATE (D,C,8+0) to OPEN

Iteration OPEN CLOSED


0 {(S,φ,0+7)} {}
1 {(B,S,4+2) (A,S,1+6)} {(S,φ,0+7)}
2 {(A,S,1+6)(C,B,6+1)} {(S,φ,0+7) (B,S,4+2)}
3 {(C,B,5+1(D,A,13+0)} {(S,φ,0+7) (B,A,3+2) (A,S,1+6) }
4 {(D,C,8+0)} {(S,φ,0+7) (B,A,3+2) (A,S,1+6) (C,B,5+1)}
EXAMPLE 2 – SOLUTION CONTD….

Iteration V
Remove head node (D,C,8+0) from OPEN and add to CLOSED.
Since D is goal node, therefore algorithm will stop and path
S→A → B → C → D is returned with path cost 8.
PERFORMANCE OF A*
Completeness and Optimality:
For finite, positive path costs, and admissible heuristics
A* algorithm is always complete and optimal.
Space and Time Complexity
Depends upon heuristic function.
For most of the problems, heuristics are never perfect.
There time and space usually grows exponentially.

You might also like