DYNAMIC PROGRAMMING 2ND DAY
Lecture:168 DEBOLINA PAN
Date: 07.10.13 I.D-110811026
ANALYSIS AND DESIGN OF
ALGORITHM
Content
 Example of Finding Optimal Number of Scaler Multiplication In
Matrix Multiplication
 FindThe Optimal Number Of Parenthesization
 Compare with Bruteforce and Divide and concure method
 Where we can use dynamic programming
 Elements of Dynamic Programming
 Optimal Structure
 Proof:
 Some problems where we should not use dynamic programming
 Proof :optimal substructure property holds for unweighted
shortest path problem
 Proof :optimal substructure property holds for unweighted
longest path problem
Example of Finding Optimal Number of
Scaler Multiplication In Matrix Multiplication
 Find the optimal number of scaler products of the given
matrices:
 Matrix dimension
 A1 30*35
 A2 35*15
 A3 15*5
 A4 5*10
 A5 10*20
 A6 20*25
contd
 According to algorithm to
find the optimal number of
scaler product if
i=j,m[i,i]<-0
contd
 If we rotate this by
45degree
contd
 According to algorithm
m[i,j]=m[i,k]+m[k+1,j]
 m[1,2]=m[1,1]+m[1,2]
=0+30*35*15=15750
 m[2,3]=m[2,2]+m[2,3]
=0+35*15*5=2625
 m[3,4]=m[3,3]+m[3,4]
=0+15*5*10=750
 m[4,5]=m[4,4]+m[4,5]
=0+5*10*20=1000
 m[5,6]=m[5,5]+m[5,6]
=0+10*20*25=5000
contd
 m[1,3]=min m[1,1]+m[2,3]+
P0.P1.P3=7875 =
m[1,2]+m[3,3]+P0.P2.P3=
18000
=7875
 m[2,4]=min m[2,2]+m[3,4]+
P1.P2.P4=6000
m[2,3]+m[4,4]+
P1.P3.P4=4375
=4375
 m[3,5]=min m[3,3]+m[4,5]+
P2.P3.P5=2500
m[3,4]+m[5,5]+
P2.P4.P5=3750
= 2500
 m[4,6]= min m[4,4]+m[5,6]+
P3.P4.P6=6250
m[4,5]+m[6,6]+
P3.P5.P6=3500
= 3500
contd
contd
 m[1,4]=min m[1,1]+m[2,4] +
P0.P1.P4 =14875
m[1,2]+m[3,4]+P0.
P2.P4=21000
m[1,3]+m[4,4]+P
0.P3.P4=9375=9
357
 m[2,5]=min m[2,2]+m[3,5] +
P1.P2.P5 =13000
m[2,3]+m[4,5]+P1.
P3.P5=7125
m[2,4]+m[5,5]+P
1.P4.P5=9375
=11375
 m[3,6]=min m[3,3]+m[4,6] +
P2.P3.P6 =5375
m[3,4]+m[5,6]+P2.
P4.P6=9500
m[3,5]+m[6,6]+P
2.P5.P6=10000
=5375
contd
contd
 m[1,5]=min m[1,1]+m[2,5]+
P0.P1.P5=28125
m[1,2]+m[3,5]+
P0.P2.P5=27250
m[1,3]+m[4,5]+
P0.P2.P5=11875
m[1,4]+m[5,5]+
P0.P4.P5=15375
= 11875
 m[2,6]=min m[2,2]+m[3,6]+
P1P2.P6=18500
m[2,3]+m[4,6]+
P2.P3.P6=10500
m[2,4]+m[5,6]+
P2.P4.P6=18125
m[2,5]+m[6,6]+
P1P5.P6=24625
= 10500
contd
 m[1,6]=min m[1,1]+m[2,6]+
P0.P1.P6=36750
m[1,2]+m[3,6]+
P0.P2.P6=37000
m[1,3]+m[4,6]+
P0.P3.P6=15125
m[1,4]+m[5,6]+
P0.P4.P6=21875
m[1,5]+m[6,6]+
P0.P5.P6=26875
= 15125
Find The Optimal Number Of
Parenthesization
Complexity Of This Algorithm
 Upper bound of this number O(n^3)
 Lower bound of this problem Ω(n^3)
 Tight bound of this problem theta(n^3)
Compare with Bruteforce and Divide
and concure method
 We can solve this matrix chain optimization problem by
bruteforce method or divide n concure method.but
complexity of this problem by these methods would be
exoponential,using dynamic programming the complexity of
the algorithm becomes order of n^3.
Where we can use dynamic
programming
 We can’t use Dynamic programming to solve any
problem,problem must have some basic property to use
dynamic programming to solve them.
 For example we cant use dynamic programming for merge
sort.
for merge sort we have to sort all keys,and the keys for
merge sort are unique,and subproblems are independent.
To use dynamic programming all keys should be dependent on
each other.
We cant replace divide n concure method to dynamic
programming.
Elements of Dynamic Programming
1. Optimal structure
2. Overlapping subproblem
3. memoization
Optimal Structure
 This is the necessary property,if this property is not present
we cant use dynamic programming.
 Say,a problem p,with subproblems p1,p2.
Solution of problem p is s,and s1 is the optimum solution of
subproblem p1,and s2 is the optimum solution of
subproblem p2.
 Claim:s is the optimal solution ,if both solution of the
subproblems are optimal only then the final solution is
optimal.
Proof:
 the final solution is optimal. only when both solution of
the subproblems are optimal
 We will proof this by contradiction.
Let us take there is a another solution of subproblem p1 s1’which is
not optimal.then combining the this solutions s1’ and s2 we get
the final solution s’ which takes less number parenthesization than
s.
But we know s has the optimal number of solution.so s’cant have
number of parenthesization less than the number parenthesization
than s.
So by contradicyion we have prooved that both solution of
subproblems are optimal only when the solution of the
subproblems are optimal.
Some problems where we should not
use dynamic programming
 Consider the following two problems in which we are given a
directed graph G =(V,E)and vertices u,v €V.
 Unweighted shortest path:Find a path from u to
consisting of the fewest edges. Such a path must be simple,
since removing a cycle from a path produces a path with
fewer edges.
 Unweighted longest simple path: Find a simple path
from u to consisting of the most edges.We need to include
the requirement of simplicity because otherwise we can
traverse a cycle as many times as we like to create paths with
an arbitrarily large number of edges.
Proof :optimal substructure property holds
for unweighted shortest path problem
 There are 2 vertices u and v.
 P path is optimal if it contains
minimum number of vertices.2
subpaths
p1[u w] and p2[w->v] if the path
is optimal then the subpaths must
be optimal.
We will proof this by contradiction.
Let we have a path p1’between u and
w which is not optimal,by
combining 2 subpaths we get the
final path p’which take less number
of vertices than p.but p is
optimal.so byy contradiction we
have prooved that unweighted
shortest path problem follows
optimal substructure problem.
Proof :optimal substructure property holds
for unweighted longest path problem
 Longest path betweenA
and C: A->B->C
 Subpath A->B and B->C
should be longest.
 Longest path betweenA-
>B is: A->C->B
 Longest path between B-
>C is: B->A->C
 It does not follow optimal
structure.solution to this
problem is not possible yet.

Dynamic programmng2

  • 1.
    DYNAMIC PROGRAMMING 2NDDAY Lecture:168 DEBOLINA PAN Date: 07.10.13 I.D-110811026 ANALYSIS AND DESIGN OF ALGORITHM
  • 2.
    Content  Example ofFinding Optimal Number of Scaler Multiplication In Matrix Multiplication  FindThe Optimal Number Of Parenthesization  Compare with Bruteforce and Divide and concure method  Where we can use dynamic programming  Elements of Dynamic Programming  Optimal Structure  Proof:  Some problems where we should not use dynamic programming  Proof :optimal substructure property holds for unweighted shortest path problem  Proof :optimal substructure property holds for unweighted longest path problem
  • 3.
    Example of FindingOptimal Number of Scaler Multiplication In Matrix Multiplication  Find the optimal number of scaler products of the given matrices:  Matrix dimension  A1 30*35  A2 35*15  A3 15*5  A4 5*10  A5 10*20  A6 20*25
  • 4.
    contd  According toalgorithm to find the optimal number of scaler product if i=j,m[i,i]<-0
  • 5.
    contd  If werotate this by 45degree
  • 6.
    contd  According toalgorithm m[i,j]=m[i,k]+m[k+1,j]  m[1,2]=m[1,1]+m[1,2] =0+30*35*15=15750  m[2,3]=m[2,2]+m[2,3] =0+35*15*5=2625  m[3,4]=m[3,3]+m[3,4] =0+15*5*10=750  m[4,5]=m[4,4]+m[4,5] =0+5*10*20=1000  m[5,6]=m[5,5]+m[5,6] =0+10*20*25=5000
  • 7.
    contd  m[1,3]=min m[1,1]+m[2,3]+ P0.P1.P3=7875= m[1,2]+m[3,3]+P0.P2.P3= 18000 =7875  m[2,4]=min m[2,2]+m[3,4]+ P1.P2.P4=6000 m[2,3]+m[4,4]+ P1.P3.P4=4375 =4375  m[3,5]=min m[3,3]+m[4,5]+ P2.P3.P5=2500 m[3,4]+m[5,5]+ P2.P4.P5=3750 = 2500  m[4,6]= min m[4,4]+m[5,6]+ P3.P4.P6=6250 m[4,5]+m[6,6]+ P3.P5.P6=3500 = 3500
  • 8.
  • 9.
    contd  m[1,4]=min m[1,1]+m[2,4]+ P0.P1.P4 =14875 m[1,2]+m[3,4]+P0. P2.P4=21000 m[1,3]+m[4,4]+P 0.P3.P4=9375=9 357  m[2,5]=min m[2,2]+m[3,5] + P1.P2.P5 =13000 m[2,3]+m[4,5]+P1. P3.P5=7125 m[2,4]+m[5,5]+P 1.P4.P5=9375 =11375  m[3,6]=min m[3,3]+m[4,6] + P2.P3.P6 =5375 m[3,4]+m[5,6]+P2. P4.P6=9500 m[3,5]+m[6,6]+P 2.P5.P6=10000 =5375
  • 10.
  • 11.
    contd  m[1,5]=min m[1,1]+m[2,5]+ P0.P1.P5=28125 m[1,2]+m[3,5]+ P0.P2.P5=27250 m[1,3]+m[4,5]+ P0.P2.P5=11875 m[1,4]+m[5,5]+ P0.P4.P5=15375 =11875  m[2,6]=min m[2,2]+m[3,6]+ P1P2.P6=18500 m[2,3]+m[4,6]+ P2.P3.P6=10500 m[2,4]+m[5,6]+ P2.P4.P6=18125 m[2,5]+m[6,6]+ P1P5.P6=24625 = 10500
  • 12.
  • 13.
    Find The OptimalNumber Of Parenthesization
  • 14.
    Complexity Of ThisAlgorithm  Upper bound of this number O(n^3)  Lower bound of this problem Ω(n^3)  Tight bound of this problem theta(n^3)
  • 15.
    Compare with Bruteforceand Divide and concure method  We can solve this matrix chain optimization problem by bruteforce method or divide n concure method.but complexity of this problem by these methods would be exoponential,using dynamic programming the complexity of the algorithm becomes order of n^3.
  • 16.
    Where we canuse dynamic programming  We can’t use Dynamic programming to solve any problem,problem must have some basic property to use dynamic programming to solve them.  For example we cant use dynamic programming for merge sort. for merge sort we have to sort all keys,and the keys for merge sort are unique,and subproblems are independent. To use dynamic programming all keys should be dependent on each other. We cant replace divide n concure method to dynamic programming.
  • 17.
    Elements of DynamicProgramming 1. Optimal structure 2. Overlapping subproblem 3. memoization
  • 18.
    Optimal Structure  Thisis the necessary property,if this property is not present we cant use dynamic programming.  Say,a problem p,with subproblems p1,p2. Solution of problem p is s,and s1 is the optimum solution of subproblem p1,and s2 is the optimum solution of subproblem p2.  Claim:s is the optimal solution ,if both solution of the subproblems are optimal only then the final solution is optimal.
  • 19.
    Proof:  the finalsolution is optimal. only when both solution of the subproblems are optimal  We will proof this by contradiction. Let us take there is a another solution of subproblem p1 s1’which is not optimal.then combining the this solutions s1’ and s2 we get the final solution s’ which takes less number parenthesization than s. But we know s has the optimal number of solution.so s’cant have number of parenthesization less than the number parenthesization than s. So by contradicyion we have prooved that both solution of subproblems are optimal only when the solution of the subproblems are optimal.
  • 20.
    Some problems wherewe should not use dynamic programming  Consider the following two problems in which we are given a directed graph G =(V,E)and vertices u,v €V.  Unweighted shortest path:Find a path from u to consisting of the fewest edges. Such a path must be simple, since removing a cycle from a path produces a path with fewer edges.  Unweighted longest simple path: Find a simple path from u to consisting of the most edges.We need to include the requirement of simplicity because otherwise we can traverse a cycle as many times as we like to create paths with an arbitrarily large number of edges.
  • 21.
    Proof :optimal substructureproperty holds for unweighted shortest path problem  There are 2 vertices u and v.  P path is optimal if it contains minimum number of vertices.2 subpaths p1[u w] and p2[w->v] if the path is optimal then the subpaths must be optimal. We will proof this by contradiction. Let we have a path p1’between u and w which is not optimal,by combining 2 subpaths we get the final path p’which take less number of vertices than p.but p is optimal.so byy contradiction we have prooved that unweighted shortest path problem follows optimal substructure problem.
  • 22.
    Proof :optimal substructureproperty holds for unweighted longest path problem  Longest path betweenA and C: A->B->C  Subpath A->B and B->C should be longest.  Longest path betweenA- >B is: A->C->B  Longest path between B- >C is: B->A->C  It does not follow optimal structure.solution to this problem is not possible yet.