You are on page 1of 54

Artificial Intelligence:

Search Methods for Problem Solving

B&B - A* - wA* - Best First


A First Course in Artificial Intelligence: Chapter 5

Deepak Khemani
Department of Computer Science & Engineering
IIT Madras

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
The Pull and the Push
Consider the function
f(n) = g(n) + w×h(n)
where w determines how much weight we give to the heuristic function.

At one end of the spectrum is w = 0


with the pull of the source
and only the shortest path in mind

At the other end of the spectrum w à ∞


with the push towards the goal
and only speedy termination in mind

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
An illustrative example Best First and wA* take the
upper path

B&B and A* explore both paths


w=2
156 148 140 132 124
visiting nodes alternately
Manhattan distance 60 50 40 30
96 98 100 102 104
12
36 48 60 72 84 24

108
start goal
24
w=1 70 60 50 40 30 36
94 96 98 100 102
36 48 60 72 g-values
164

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
The problem graph for wA* The values outside the nodes
220 13
are 2×Manhattan Distance
280 200 160
A B C D
23
23
240
33
24
33 These are used in wA* where
37 100
22
E
35
F
160
G f(n) = g(n) + 2×h(n)
29 22
200 180 64
H
240
22 start
I 12
J 12
K
160
Next we trace the progress of
22 21 21 33
this weighted A* algorithm
120 80
23 200 21
L M N
23 33 21
21
200 160 140 80
O 22 P 12
Q R
32 43

21 160 100
44
S T
21 35 33

120 40 0
41
U V 22
W
10 goal

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Weighted A*: f(n) = g(n) + 2×h(n) With weight = 2 the search
220
algorithm gives greater
257 importance to the heuristic
value. Observe the
240 37
269 2×h(n) values in cyan.
29

262
240 22 start
200
200
12
180
192 We take up the action after
22 it has expanded the start
222 200
21 node.

160
181

0
W
10 goal

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Weighted A*: f(n) = g(n) + 2×h(n) Initially the behavior is
220 simililar both to A* and the
257
Best First algorithms
240 37
269
29

24022 start 200 180


12
262 200 192
22

21
222 200

21

200 160 140


243 22 181 12 173

0
W
10 goal

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Weighted A*: f(n) = g(n) + 2×h(n) Unlike Best First Search the
Weighted A* algorithm does
220
257 look back to consider the g-
values and prefers this node
240 37
269 to its child which is closer to
29
the goal.
24022 start 200 180
12 12 160
262 200 192 226
22 It will in fact find a better path
222 200
21 to its child than the one
21
33 already found
200 160 140
243 22 181 12 173

0
W
10 goal

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Weighted A*: f(n) = g(n) + 2×h(n)
220
257 Observe that the value of
this node has reduced
240 37
269 from 226 to 184.
29

24022 start 200 180


12 12 160
262 200 192 184
22

21
222 200
33
21

200 160 140


243 22 181 12 173

0
W
10 goal

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Weighted A*: f(n) = g(n) + 2×h(n)
220
All algorithms explore
257 this dead-end because it
240 37
appears to be closest to
269
35
206
160
the goal.
29 22

24022 start 200 180


12 12 160
262 200 192 184
22 21

21 120
222 200 165
33
21

200 160 140


243 22 181 12 173

0
W
10 goal

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Weighted A*: f(n) = g(n) + 2×h(n)
220
257

240 37
35 160
269 206
29 22

24022 start 200 180


12 12 160
262 200 192 184
22 21 21

21 120
222 200 165
33
21

200 160 140


243 22 181 12 173

0
W
10 goal

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Weighted A*: f(n) = g(n) + 2×h(n)
220
257 270
200 The rapidly decreasing
23
weighted heuristic pushes
24
269
240 37
35
206
160 the search faster towards the
29 22 goal, exploring less of the
24022 start
200
200
12
180
12
184
160
64
state space than the A*
262 192
22 21 21 algorithm.
21 120 80
222 200 165 190
33
21

200 160 140


243 22 181 12 173

0
W
10 goal

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Weighted A8 finds the goal faster than A*
220
200
257 270
23

240 37 24
35 160 100
269 206 245
29 22
200 180 64
24022 start 12 12 160
262 200 192 184
22 21 21 33

21 120 80
222 200 165 190
33 21
21

200 160 140 80


243 22 181 12 173 211
43

0
153
10 goal

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Weighted A*: f(n) = g(n) + 2×h(n) Weighted A* finds a path
220
to the goal after
200
257 270
23
inspecting 9 nodes.
240 37 24
35 160 100
269 206 245 But it finds a more
29 22
64
expensive path to the
24022 200 18012
262
start
200
12
192 184
160
goal than A*.
22 21 21 33

222 200
21
165
120
190
80
Cost of path = 153
33 21
21

200 160 140 80


243 22 181 12 173 211
43

0
153
10 goal

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Path found by Best First Search
110 13
100 8 nodes inspected
23 Cost of Solution = 195
33
120 37 24
35 80 50
6
29 22

120 100 90 64
22 start 12 12 80
1 4
22 21 21 33

21 60 40
23 100
5 7
23 33 21
21
100 80 70 40
22 2 12 3
43

44

8
0
W
10 goal

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Branch & Bound : f(n) = g(n) + 0×h(n) B&B explores
the entire graph before
13
44 37 50 73 finding the optimal path
23
23
33
24
33 to the goal node.
37
22 35
29 46 106
29 22 No looking ahead!
64
22 start 12 12
22 0 12 24
22 21 21 33 Nodes are labeled with
23
22
21
45 110
g*(n) values.
23 33 21
21

43 22 21 33 131
12
32 43

21 44
64 120
21 35 33

41
85 126 22 148
10 goal

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
A*: The Solution
14 nodes inspected
110 13
184
140
147 150
100
Cost of solution = 148
33
120 37 24
22 35 80
149 126
29 22

120 100 90 64
22 start 12 12 80
142 100 102 104
22 21 21

21 60 40
23 100
122 105 150
23 33
21
100 80 70
143 22 101 103
12

21 80 50
144 170
21 35 33

60 41 20 0
145 146 22 148
10 goal

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
f(n) = g(n) + w×h(n): B&B - A* - wA* - Best First

Weight Algorithm Nodes Nodes Cost of


w generated inspected path
0 Branch & Bound 23 23 148
1 A* 19 14 148
2 Weighted A* 17 9 153
∞1 Best First Search 17 8 195

Note1 : This is equivalent to saying that


the weight of g(n) is zero, that is, negligible
Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
f(n) = g(n) + w×h(n): B&B - A* - wA* - Best First

B&B wA*
Nodes 23 Nodes 9
Cost 148 Cost 153

A* Best First
Nodes 14 Nodes 8
Cost 148 Cost 195

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Next
Admissible Variations on A*
Leaner, meaner versions...

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Complexity A2 A1

Goal
Start

The search spaces for A1 and A2 when h2(n) > h1(n).

Here h2 was more informed than h1

In weighted A* we can shrink this further but lose admissibility.


Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Search Frontiers (recap) We saw that A* in general needs more space
and time than Best First Search
DFS frontier
grows linearly

Best First search


frontier depends upon
the heuristic function
Breadth First
frontier grows
exponentially

The Search Frontier is an indication of space requirement


Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Saving on space
o While studying Best First Search we had observed that the space
complexity of the algorithm is often exponential
• depending upon how good the heuristic function is

o This prompted us to look at local search algorithms like Hill Climbing,


Beam Search, Tabu Search and Simulated Annealing
o We also looked at population based methods like Genetic Algorithms
and Ant Colony Optimization

o But these algorithms did not guarantee optimality.

o Are there space saving versions of A* that are admissible?


• Perhaps at the expense of time complexity?
• Remember Depth First Iterative Deepening?

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Depth Bounded DFS (DBDFS) (recap)

Goal

Start
d

Do DFS with a depth bound d.


Linear space
Not complete
Does not guarantee shortest path
Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Depth First Iterative Deepening (DFID) (recap)
DepthFirstIterativeDeepening(start)
1 depthBound ß 1
2 while TRUE
3 do DepthBoundedDFS(start, depthBound)
4 depthBound ß depthBound + 1

DFID does a series of DBDFSs with increasing depth bounds

A series of depth bounded depth first searches


(thus requiring linear space)
of increasing depth bound.

When a path to goal is found in


some iteration it is the shortest path
(otherwise it would have been found in the previous iteration)

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
DFID: cost of extra work
For every new layer in the search tree that DFID
explores, it searches the entire tree up to that layer
all over again. I internal nodes

L leaves
DFID inspects (L+I) nodes whereas
Breadth First would have inspected L nodes
NDFID = NBFS (L+I)
L
But L = (b−1)*I + 1 for a full tree with branching factor b

\ NDFID ≈ NbFS b for large L where b


(b−1) is the branching factor

The extra cost is not significant!


Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Iterative Deepening A*
• The algorithm Iterative Deepening A* (IDA*) was
presented by Richard Korf in 1985
• It is designed to save on space by doing a series of Depth
First Searches of increasing depth
– Like Depth First Iterative Deepening

• Unlike DFID which uses depth as a parameter, IDA* uses


f-values to determine how far should Depth First Search go
• IDA* initially sets the bound to f(S)
– which is equal to h(S) and is an underestimate on optimal cost

• In subsequent cycles it extends the bound to the next


unexplored f-value

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Iterative Deepening A* (IDA*)
IterativeDeepeingA*(start)
1 depthBound ß f(S)
2 while TRUE
3 do DepthBoundedDFS(start, depthBound)
4 depthBound ß f(N) of cheapest unexpanded node on OPEN

IDA* does a series of DBDFSs with increasing depth bounds

A series of depth bounded depth first searches


(thus requiring linear space)
of increasing depth bound.

When a path to goal is found in


some iteration it is the shortest path
(otherwise it would have been found in the previous iteration)

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
IDA*: nodes explored by DFS In the next cycle cutoff to lowest
unexplored f-value node

Start
Goal

Nodes within Cutoff f-value


are explored by DFS

IDA* iteratively extends search frontier for Depth First Search


Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Problems with IDA*
o Even when the state space grows quadratically the the number of
paths to each node grows exponentially.

o The DFS algorithm can spend a lot of time exploring these


different paths if a CLOSED list is not maintained
• We observed that this has to be done to guarantee shortest
path in the case of DFID

o The other problem is that in each cycle it extends the bound only
to the next f-value
• This means that the number of cycles may become too large
specially if the nodes have all different f-values
• One option is to extend the bound by a certain constant
amount each time, with a limited compromise on the cost

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
IDA*: Incrementing bound by d DFS may terminate before
looking at a cheaper solution.

IDA* may find a


costlier solution.
The extra cost is
bounded by d
Start
Goal

Loss in optimality is bounded by d


Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Recursive Best First Search
• IDA* has linear space requirements (being Depth First in nature)
• But it has no sense of direction (again being Depth First in nature)
• Recursive Best First Search (RBFS) is an algorithm also
presented by Richard Korf (1991)
• “RBFS is a linear-space best-first search algorithm that always
explores new nodes in best-first order, and expands fewer nodes
than iterative deepening with a nondecreasing cost function.”
• RBFS is like Hill Climbing with backtracking
• Backtracking if no child is best node on OPEN
• Except that instead of backtracking, RBFS rolls back search
to a node it has marked as second best
• While rolling back it backs up the lowest value for each node
on from its children to update the value of the parent

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
50
RBFS: Roll back
50

59 55 60 62
59 60 62

61 61 56 60
Backed up f-value

60 56 62 New Search
60

57 60 67 60

61
Lowest f-values are
64 60 58
backed up at each level
when RBFS rolls back

63 61 62

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
An exercise problem Yet to be
generated
CLOSED
645 500
OPEN
Draw the graph after 690
2 expansions, and 670 640 630
mark the 3rd node 660 700
550
RBFS will pick 720 660
690
720 700 720
570
740 770
730 710
710
720 590 690
650
700 680
710
720 620
650 660 700 730 700
710
740 710

750 720 670 690


720
690 680 700
740
750 770 Numbers in the nodes denote f-values
Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Next
The monotone or consistency condition
when, like Dijkstra’s algorithm,
A* too finds the optimal path
to every node it picks from OPEN

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
The Monotone Condition
The monotone property or the consistency property for a heuristic function
says that for a node n that is a successor to a node m on a path to the goal
being constructed by the algorithm A* using the heuristic function h(x),

h(m) – h(n) ≤ k(m,n)

This means that this is true for any two nodes M and N
connected by an edge.

The heuristic function


underestimates the cost of each edge

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
h(M) − h(N) ≤ k(M,N) The nodes are placed on a grid
110
where each edge is 10km, and
140
A B h(n) is the Manhattan distance
23
33
120 37
22
E
29
Even on an optimal path to the
120 100 90
h(H) − h(O) = 20 ≤ 23 = k(H,O) goal the difference in h-values
22 start
H I 12
J is less than the edge cost
22 h(E) − h(B) = 10 ≤ 33 = k(E,P)
23 100 21
L h(L) − h(P) = 20 ≤ 21 = k(L,P)
23
21
100 80 70 h(S) − h(U) = 20 ≤ 21 = k(S,U)
O 22 P 12
Q

h(U) − h(S) = −20 ≤ 21 = k(U,S)


21 80
S
21

60 0
U W
10 goal

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Monontone condition à non-decreasing f-values
Given that we started from start S, we can add the term g(m)
to both sides to get,

h(m) + g(m) ≤ k(m,n) + h(n) + g(m)

Since n is the successor of m we have


g(m) + k(m,n) = g(n)

Therefore
h(m) + g(m) ≤ h(n) + g(n),
or f(m) ≤ f(n)

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
L7: Monotone condition à when A* picks a node n, g(n) = g*(n)
Proof: Let A* expand node n with cost g(n).
Let nL be the last node on the optimal path from S to n that has been
expanded. Let nL+I be the successor of nL that must be on OPEN.

nL nL+1 n Goal G

OPEN List
Optimal path to N

Start
Path to node N

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Proof (continued): g(n) = g*(n)
Given that f(nL) ≤ f(nL+1) the following property holds,

h(nL) + g(nL) ≤ h(nL+I) + g(nL+I)


or h(nL) + g*(nL) ≤ h(nL+I) + g*(nL+I) because both are on the optimal path

By transitivity of ≤ the above property holds true for any two nodes on the optimal
path. In particular it holds for nL+I and node n. That is,

h(nL+I) + g*(nL+I) ≤ h(n) + g*(n)

That is,
f(nL+I) ≤ h(n) + g*(n)
because nL+1 is on the optimal path to n

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Proof (continued): g(n) = g*(n)
But since A* is about to pick node n instead,
f(n) ≤ f(nL+I)

That is,
h(n) + g(n) ≤ f(nL+I)
or h(n) + g(n) ≤ h(nL+I) + g*(nL+I)

Recall that h(nL+I) + g*(nL+I) ≤ h(n) + g*(n)

Therefore
h(n) + g(n) ≤ h(n) + g*(n)
or g(n) ≤ g*(n)
or g(n) = g*(n)
because g(n) cannot be less than g*(n) the optimal cost.

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
The consequences: CLOSED is closed

For A* the interesting consequence of searching with a


heuristic function satisfying the monotone property is that
every time it picks a node for expansion, it has found an
optimal path to that node.

As a result, there is no necessity of improved cost


propagation through nodes in CLOSED (Case 3 in A*),

...because A* would have already found the best path to


them in the first place when it picked them from OPEN
and put them in CLOSED.

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Next
We look at a problem in which pruning the
CLOSED would be desirable:
Sequence Alignment in Biology

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
DNA sequences
A nucleic acid sequence is a succession of letters that indicate the
order of nucleotides forming alleles within a DNA (using GACT) or
RNA (GACU) molecule.

Because nucleic acids are normally linear (unbranched) polymers,


specifying the sequence is equivalent to defining the covalent
structure of the entire molecule.

The sequence has capacity to represent information. Biological


deoxyribonucleic acid represents the information which directs the
functions of a living thing.

The possible letters are A, C, G, and T, representing the four


nucleotide bases of a DNA strand — adenine, cytosine, guanine,
thymine
Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
The Sequence Alignment problem
The Sequence Alignment problem is one of the fundamental
problems of biological sciences, aimed at finding the similarity of
two amino-acid sequences.

Comparing amino-acids is of prime importance to humans, since


it gives vital information on evolution and development.

Saul B. Needleman and Christian D. Wunsch devised a dynamic


programming algorithm to the problem and got it published in
1970.

Since then, numerous improvements have been made to


improve the time complexity and space complexity,

https://www.geeksforgeeks.org/sequence-alignment-problem/

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Cost of alignment
Given two sequences composed of the characters C, A, G and T the
task of sequence alignment is to list the two alongside with the option
of inserting a gap in either sequence.

The objective is to maximize the similarity between the resulting two


sequences with gaps possibility inserted.

The similarity can be quantified by associating a cost with


misalignment. Typically two kinds of penalties/costs are involved –

o Mismatch : if character X is aligned to a different character Y


• The cost/penalty could be combination specific

o Indel : associated with inserting a gap

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Best alignment depends on penalties
https://www.geeksforgeeks.org/
indel = 3, mismatch = 7 total penalty = 6 sequence-alignment-problem/

X=CG X=CG_
Y=CA Y=C_ A

indel = 3, mismatch = 5 total penalty = 5


X=CG X=CG
Y=CA Y=CA

indel = 3, mismatch = 2 total penalty = 5


X = AGGGCT X = AGGGCT
Y = AGGCA Y = A _GGCA

The two strings need not be of equal length

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Similarity functions
A similarity function is a kind of inverse of the distance function.

Using a similarity function transforms the sequence alignment


into a maximization problem

Some simple similarity functions

Match: +1
Mismatch or Indel: −1

Match = 0
Indel = −1
Mismatch = −10

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
A fine grained similarity function
A G C T
Highest weight to aligning A with A A 10 -1 -3 -4
No penalty for aligning T with C
Other mismatches have negative weight G -1 7 -5 -3
C -3 -5 9 0
The alignment: T -4 -3 0 8

AGACTAGTTAC
CGA - - - GACGT

with a indel penalty of -5, would have the following score:


S(A,C)+S(G,G)+S(A,A)+(3×d)+S(G,G)+S(T,A)+S(T,C)+S(A,G)+S(C,T)
= -3 + 7 + 10 - (3 × 5) + 7 + (-4) + 0 + (-1) + 0
=1

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Sequence alignment as graph search
Let X and Y be the first characters of the two strings.
There are three possibilities Insert gap before Y
1. Align X with Y
2. Insert blank before X
3. Insert blank before Y X
Before considering X and Y Align X and Y

Insert gap before X

Y
There are three moves from each node
Each move has an associated penalty or cost (mismatch or indel)
Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
The graph for the sequence alignment problem
G C A T G C U
start If we had a longer
horizontal sequence the
G last column would come
into play
A

A goal
Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Four alignments
GCATGCU GCATG -CU
G C A T G C U
GATTACA G - ATTACA

G GCA - TGCU GCAT-GCU


G - ATTACA G -ATTACA
A
The Needleman-Wunsch algorithm
T explores the entire space

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Complexity for A*
The state space grows quadratically with depth

But the number of distinct paths grows combinatorically

Consider two strings of length N and M being aligned.


The grid size is (N+1)×(M+1)

The number of ways that gaps can be inserted (moving only


horizontally or vertically) is (N+M)! / (N!×M!)

Taking diagonal moves also into account the number of paths is


∑ (M+N-R)! / (M-R)!×(N-R)!×R!
where R varies from 0 to min(M,N)
and stands for the number of diagonal moves in the path

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
OPEN grows Linearly, CLOSED quadratically
G C A T G C U In biology the sequences to be
start aligned may have hundreds of
G
thousands of characters.

A Quadratic is then a formidable


growth rate.
T
Motivation to prune CLOSED
T

A goal
Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras
Next
Pruning CLOSED

Artificial Intelligence: Search Methods for Problem Solving Deepak Khemani, IIT Madras

You might also like