DAA Unit04
DAA Unit04
General Method:
Dynamic Programming is an algorithm design method that can be
used when the solution to a problem can be viewed as the result of a
sequence of decisions.
And we wish to get the optimal solution.
EX:
Knapsack problem
Shortest path problem
Consider the shortest path and assume that i,i1,i2, … ik,j is the shortest
path from i to j.starting with the initial vertex i, a decision has been
made to go to vertex i1. Now we need to find the path from i1 to j. let
r1, r2, r3, … j be a shortest i1 to j path. Then I,i1,r1,r2,… rq, j be a
shortest path.
Principle of Optimality: It states that an optimal sequence of decisions
has the property that whatever the initial state and decision are, the
remaining decisions must constitute an optimal decision sequence
with regards to the state resulting from the first decision.
1.Matrix Chain Multiplication
Matrix-chain multiplication problem
Given a chain A1, A2, …, An of n matrices,
where for i=1, 2, …, n, matrix Ai has
dimension pi-1×pi
Parenthesize the product A1A2…An such
that the total number of scalar
multiplications is minimized.
Brute force method of exhaustive
search takes time exponential in n
( 2n ).
Matrix Multiplication
p q
× ×
q r
p
×
r
Cost: Number of scalar multiplications = pqr
Example
Matrix Dimensions
A1 13 x 5
A2 5 X 89
A3 89 X 3
A4 3 X 34
Parenthesization Scalar multiplications
1 ((A1 A2 ) A3 ) A4 10,582
2 ( A1 A2 ) (A3 A4 ) 54,201
3 (A1 (A2 A3 )) A4 2, 856
4 A1 ((A2 A3 ) A4 ) 4, 055
5 A1 (A2 (A3 A4 )) 26,418
1. 13 x 5 x 89 scalar multiplications to get (A B) 13 x 89
result
13 x 89 x 3 scalar multiplications to get ((AB)C) 13 x 3
result
13 x 3 x 34 scalar multiplications to get (((AB)C)D) 13 x 34
Dynamic Programming
Approach
The structure of an optimal solution
Let us use the notation Ai..j for the matrix that
results from the product Ai Ai+1 … Aj
An optimal parenthesization of the product
A1A2…An splits the product between Ak and
Ak+1 for some integer k where1 ≤ k < n
First compute matrices A1..k and Ak+1..n ; then
multiply them to get the final matrix A1..n
Dynamic Programming
Approach …contd
Key observation: parenthesizations of the
subchains A1A2…Ak and Ak+1Ak+2…An must also
be optimal if the parenthesization of the chain
A1A2…An is optimal.
That is, the optimal solution to the problem
contains within it the optimal solution to
subproblems.
Dynamic Programming
Approach …contd
Recursive definition of the value of
an optimal solution.
Let m[i, j] be the minimum number of
scalar multiplications necessary to
compute Ai..j
Minimum cost to compute A1..n is m[1,
n]
Suppose the optimal parenthesization
of Ai..j splits the product between Ak and
Ak+1 for some integer k where i ≤ k < j
A1….4
[ A1 ] [ A2 ] [ A3]
[ A4 ]
K=
2
[ A1 ] p0xp1 [ A2 ] p1xp2 [ A3] p2xp3 [ A4 ]
p3xp4
Ai….j
[ Ai] ………… [ Aj ]
K
[ Mi k ] pi-1 x pk [Mk+1 j ] pk x
pj
pi-1 x pk x pj
Dynamic Programming
Approach …contd
Ai..j = (Ai Ai+1…Ak)·(Ak+1Ak+2…Aj)= Ai..k · Ak+1..j
Cost of computing Ai..j = cost of computing Ai..k
+ cost of computing Ak+1..j + cost of
multiplying Ai..k and Ak+1..j
Cost of multiplying Ai..k and Ak+1..j is pi-1pk pj
m[i, j ] = m[i, k] + m[k+1, j ] + pi-1pk pj
for i ≤ k < j
m[i, i ] = 0 for i=1,2,…,n
Dynamic Programming
Approach …contd
But… optimal parenthesization occurs
at one value of k among all possible i ≤
k<j
Check all these and select the best one
0 if i=j
m[i, j ]
= min {m[i, k] + m[k+1, j ] + pi-1pk pj }
i≤
if i<j
k< j
Dynamic Programming
Approach …contd
To keep track of how to construct an
optimal solution, we use a table s
s[i, j ] = value of k at which Ai Ai+1 …
Aj is split for optimal
parenthesization.
Ex:-
[A1]5×4 [A2]4×6 [A3]6×2
[A4]2×7
P0=5, p1=4, p2=6, p3=2,
p4=7 1 2 3 4
Seque
2
nce
Size
3
4
Matrix Chain Multiplication Algorithm
st
op
i
f
d
o
Algorithm search(x)
{
found:=false;
t:=tree;
while( (t≠0) and not found ) do
{
if( x=t->data ) then found:=true;
else if( x<t->data ) then t:=t->lchild;
else t:=t->rchild;
}
if( not found ) then return 0;
else return 1;
}
Optimal Binary Search Trees
Problem
Given sequence of identifiers (a1,a2 …., an) with
a1<a2 <··· < an.
Let p(i) be the probability with which we search
for ai
Let q(i) be the probability with which we search
for an identifier x such that ai< x <ai+1 .
Want to build a binary search tree (BST) with
minimum expected search cost.
Identifiers : stop, if, do
st Internal node :
op
successful search, p(i)
i E
f 3 External node :
d E
unsuccessful search,
o 2 q(i)
E E
0 1
∑ P ∗ level(a ) + ∑ Q ∗ (level(E ) − 1)
n =1
i i
n =0
i i
The dynamic programming
Make a decision approach
as which of the a ’s should i be
assigned to the root node of the tree.
If we choose ak, then it is clear that the internal
nodes for a1,a2,……,ak-1 as well as the external
nodes for the classes E0, E1,….,Ek-1 will lie in the
left subtree l of the root. The remaining nodes will
be in the right subtree r.
ak
P1...Pk-1 Pk+1...Pn
Q0...Qk-1 Qk...Qn
a1...ak-1 ak+1...an
C(1,k-1) C(k+1,n)
cost( l)= ∑ p(i)*level(ai) + ∑ q(i)*(level(Ei)-1)
1≤i 0≤i
<k
cost(r)=∑ <k
p(i)*level(ai) + ∑ q(i)*(level(Ei)-1)
K<i K≤i
≤j ≤j
i<
c(i, k≤
j)= min cost(i,k-1) + cost(k,j) + w (i, j)
j
Therefore, c(0,n) can be solved by first computing all c(i, j)
such that j - i=1, next we compute all c(i,j) such that j - i=2,
then all c(i, j) wiht j – i=3, and so on.
w(0,1) = q(0)+p(1)+q(1) = 8
c(0,1) = min { c(0,0) + c(1,1)} +
w(0,1) = 8
r(0,1)=1
a
2
E E
1 2
w(1,2) = q(1)+p(2)+q(2) =
p(2)+q(2)+w(1,1) = 7
c(1,2) = min { c(1,1) + c(2,2)} + w(1,2) =
7
r(0,1)=2
a
3
E E
2 3
w(2,3) = q(2)+p(3)+q(3) = a
p(3)+q(3)+w(2,2) = 3 4
c(2,3) = min { c(2,2) + c(3,3)} + w(2,3)
=3 E E
r(0,1)=3 3 4
w(3,4) = q(3)+p(4)+q(4) =
p(4)+q(4)+w(3,3) = 3
c(3,4) = min { c(3,3) + c(4,4)} + w(0,1) =
3
r(0,1)=4
a a
2 1
a E E a
1 2
0 2
E E E E
1 2
0 1
w(0,2) = q(0)+p(1)+q(1)+p(2)+q(2) = 1
p(2)+q(2)+w(0,1) = 21
c(0,2) = min { c(0,0) + c(1,2) , c(0,1)+c(2,2) } 9
0 + 7 8 + 0
+ w(0,2) =
12
r(0,2)=1
4
1 a
2
5 2
2 a a
1 3
1 a
4
2 a a
2 6
3 a a a
a 7
1 3 5
p = ( 3, 3, 1, 1) and q = ( 2, 3,
1,1,1 ).
1
j-i
2
P: 3 3 1
11
Q: 2 3 3 1
3 11
d i
o n
t w
h
il
e
Ex 2: Let n=4, and ( a1,a2,a3,a4 ) = (count, float,
int,while).
Let p(1 : 4 ) =( 1/20, 1/5, 1/10, 1/20) and
q(0: 4) = ( 1/5,1/10, 1/5,1/20,1/20 ).
Observation:
The optimal k can be found by limiting the
search to the range r( i, j – 1) ≤ k ≤ r( i+1,
j)
for m:= 2 to n do
{
for i := 0 to n – m do
{
j:= i + m ;
w[ i, j ]:= p[ j ] + q[ j ] + w[ i, j -1 ];
// Solve using Knuth’s result
x := Find( c, r, i, j );
c[ i, j ] := w[ i, j ] + c[ i, x -1 ] + c[ x,
j ];
r[ i, j ] :=x;
}
}
Algorithm Find( c, r, i, j )
{
for k := r[ i, j -1 ] to r[ i+1, j ] do // k = 1 to n
{ min :=∞;
if ( c[ i, k -1 ] +c[ k, j ] < min ) then
{
min := c[ i, k-1 ] + c[ k, j ]; y:= k;
}
}
return y;
}
Travelling Salesperson
Problem (TSP)
Problem:-
You are given a set of n cities.
You are given the distances between the cities.
You start and terminate your tour at your
home city.
You must visit each other city exactly once.
Your mission is to determine the shortest tour.
OR
minimize the total distance traveled.
e.g. a directed graph :
2
1 2
2
10
4
6 5 9 3
8
7
4 3
Cost matrix: 4
1 2 3 4
1 ∞ 2 10 5
0
2 2 ∞ 9 ∞
0
3 4 3 ∞ 4
0
4 6 8 7 ∞
0
The dynamic programming approach
2
g(i, S) = min{c ij + g(j, S - {j})}
j∈S
Equation 1 can be solved for g( 1, V- {1} ) if
we know g( k, V- {1,k} ) for all choices of k.
Clearly,
g( i, Ø ) = Ci1 , 1≤ i ≤ n.
5 3 ∞ ∞ ∞ 0
Idea of Floyd-Warshall Algorithm
Assume vertices are {1,2,……n}
General formula
Ak [ i, j ]= min {A k-1
[ i, j ], Ak-1[ i, k ]+ Ak-1[ k, j ] }
Shortest path using intermediate vertices
{ V1 , . . . V k }
d k-1[i, Vk d k-1[k,
k] j]
V
Vi
j
d k-1[i,
j]
for k := 1 to n do
for i := 1 to n do
for j := 1 to n do
d [ i, j ] := min ( d [ i, j ] , d [ i, k ] + d [ k, j ] );
}
Time Complexity is O ( n 3
)
Sequence Of Decisions
General formula
f(1,c)
f(2,c) f(2,c-w1)
Note That s0 =( 0, 0 )
si ={ ( P, W ) ( P- pi+1, W- wi+1)€ s i
}
1
OR
Si = Si-1 + (pi, wi)
1
Step1: if ( p, w ) € s n
and ( p, w ) €
s n -1
xn=1
otherwise x n = 0
This leaves us to determine how either ( p, w
) or
( p – pn, w- wn ) was obtained in Sn-1 .
This can be done recursively ( Repeat
Step1 ).
Knapsack Algorithm
Algorithm DKP(p,w,n,m)
{
S0 ={(0,0)};
for i=1 to n-1 do
{
Si-1 ={(P,W)|(P-pi ,W-wi ) (- Si-1 and
W<=m);
Si =MergePurge(Si-1 ,S1i-1 );
}
(PX,WX)=last pair in Sn-1 ;
(PY,WY)=(P‘+pn ,W‘+wn ) where W‘ is
the largest W in any pair in Sn-1 such
that
W+wn <=m;
if(PX>PY) then xn =0;
else xn=1;
TraceBackFor(xn-1,…..,x1);
}
Example: solve the following knapsack problem by using
dynamic programming:
Knap sack with n=4 and m=8.
Sol: create the sequence of decision as Sa = {(w, p)}
S0 = { (0,0) } // initially no item got selected in the
knapsack
S01 = { (2,1) }
So S1 = { (0,0) , (2,1) } // Merge the S0 and S01
S12 = { (3,2) , (5,3) }
So S2 = {(0,0) , (2,1) , (3,2) , (5,3) }
S23= {(4,5) , (6,6) , (7,7) , (9,8) }
So S3 = {(0,0) , (2,1) , (3,2) , (4,5) , (5,3), (6,6) , (7,7) , (9,8) }
S3 = {(0,0) , (2,1) , (3,2) , (4,5) , (6,6) , (7,7) }
S34= {(5,6), (7,7), (8,8), (9,11), (11,12), (12,13) }
D1 D2 D3
… Dn
D1 D2 D3 Dn
D1 D2 D3
D1 D3 … Dn
Dn
D3
ui= ( c + c i - ∑ cj )
1 C
i
≤
The upper bound u i follows from the observation
that j
m i ≥ 1. ≤
The optimal solution m 1,m 2,……,m n is the result
n
of a sequence of decisions, one decision for each
mi
Let fn(c) be the reliability of an
optimal solution, then
fn(c)= max { Ф ( m ) f ( c- c m ) }
n n n-1 n n
1≤ m n
≤un
General formula
Clearly,
1≤ m f0(x)=1, for all x, 0 ≤ x ≤ c
i
≤ ui
Let s i
consist of tuples of the form ( f, x )
Where f = f i( x )
S1 = {(30,0.9)}
1
D1= I unit
S = {(60,0.99)} D(30,0.9)
1
2
1= 2 unit (60,0.99) (30+30 , 1-(1-
0.9)2 )
S1= { (30,0.9), (60,0.99) }
___________________________
S2 = { (45,0.72) , (75,0.792) }
1
S2 = { (60,0.864) , (90,0.95) }
2
D2= I unit
S = { (75,0.893) , (105,0.982) }
2
3
(15,0.8)
D2= 2 unit(75,0.893), (90,0.95), (105,0.982)
S = {(45,0.72) , (60,0.864) ,(75,0.792),
2
} (30,0.96)
D2= 3 unit
(45,0.992)
S2= {(45,0.72) , (60,0.864) , (75,0.893)}
____________________________________
S31= { (65,0.36) , (80,0.432),(95,0.446) }
S32= { (85,0.54) , (100,0.648),(115,0.669)}
S33= { (105,0.63) , (120,0.756),(135,0.783D3=}I unit
D3= 2 unit
(20,0.5)
S 3= {(65,0.36) , (80,0.432), (85,0.54) , (95,0.446),
3
(40,0.75) (100,0.648),
D3= 3 unit
(105,0.63) , (60,0.875)
(115,0.669), (120,0.756),(135,0.783 }
S 3= {(65,0.36) , (80,0.432), (85,0.54), (100,0.648)}
3
_____________________________________________