Daa Unit-3
Daa Unit-3
p×q q×r
p×r
0 if i=j
m[i, j ] =
min {m[i, k] + m[k+1, j ] + pi-1pk pj } if i<j
i ≤ k< j
Dynamic Programming Approach …
contd
1 2 3 4 J
Algorithm Matrix_Chain_Mul(p[], n)
{
for i:= 1 to n do
m[i, i]:= 0 ;
11-18
Example
• Copy the table of previous example and then
construct optimal parenthesization.
Optimal Binary Search Tree(OBST)
stop
if
do
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
stop
Internal node : successful
if
search, p(i)
E3
• External node :
do E2 unsuccessful search, q(i)
E0 E1
P level(a ) Q (level(E ) 1)
n 1
i i
n 0
i i
8 -24
The dynamic programming approach
• Make a decision as which of the ai’s should 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
P 1 ...P k-1 P k+1 ...P n
Q 0 ...Q k-1 Q k ...Q n
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<k 0≤i<k
We can generalize the above formula for any c(i,j) as shown below
do int
while
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 ).
• Therefore, the total time to evaluate all the c(i, j)’s and
r(i, j)’s is
∑ ( mn – m2 ) = O(n3)
1≤m≤n
• We can reduce the time complexity by using the
observation of D.E. Knuth
• 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 ];
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
{ min :=∞;
if ( c[ i, k -1 ] +c[ k, j ] < min ) then
{
min := c[ i, k-1 ] + c[ k, j ]; y:= k;
}
}
return y;
}
Traveling Salesperson Problem (TSP)
Problem:-
1 2 3 4
• Cost matrix: 1
0 2 10 5
2 2
0 9
3 4 3
0 4
4 6 8 7
0
The dynamic programming approach
g(1,
• The general V - {1}) min {c1k g(k, V - {1, k})}
form:
2k n
1
Clearly,
g( i, Ø ) = Ci1 , 1≤ i ≤ n.
g(4, Ø)=C41=6
We can obtain
g(2, {3})=C23 + g(3, Ø)=9+4=13
g(2, {4})=C24 + g(4, Ø)=∞
Floyd’s Algorithm
All-Pairs Shortest Path Problem
• General formula
d k-1[i, k] Vk d k-1[k, j]
Vj
Vi
d k-1[i, j]
d1 =
d2 =
d3 =
d4 =
d5 =
Algorithm
Algorithm AllPaths( c, d, n )
// c[1:n,1:n] cost matrix
// d[i,j] is the length of a shortest path from i to j
{
for i := 1 to n do
for j := 1 to n do
d [ i, j ] := c [ i, j ] ; // copy c into d
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 )
0/1 Knapsack Problem
Let xi = 1 when item i is selected and let xi = 0 when item i is not selected.
n
maximize pi xi
i=1
n
subject to wi xi <= c
i=1
and xi = 0 or 1 for all i
All profits and weights are positive.
Sequence Of Decisions
General formula
• Note That s0 =( 0, 0 )
1 si ={ ( P, W ) ( P- pi+1, W- wi+1)€ s i }
OR
Si =
1 S i-1
+ (pi, wi)
D1 D3
D2 Dn
D3
D1
D2 … Dn
D1 D3
D3 Dn
maximize π Фi ( m i )
1≤i≤n
subjected to ci mi ≤ c
1≤i≤n
1≤i≤n
m i ≥1 and integer,
Dynamic programming approach
ui= ( c + c i - ∑ cj )
1≤j≤n Ci
General formula
Where f = f i( x )