Dynamic Programming 
• Steps. 
View the problem solution as the result of a sequence 
of decisions. 
Obtain a formulation for the problem state. 
Verify that the principle of optimality holds. 
Set up the dynamic programming recurrence 
equations. 
Solve these equations for the value of the optimal 
solution. 
 Perform a traceback to determine the optimal 
solution.
Dynamic Programming 
• When solving the dynamic programming 
recurrence recursively, be sure to avoid the 
recomputation of the optimal value for the 
same problem state. 
• To minimize run time overheads, and hence 
to reduce actual run time, dynamic 
programming recurrences are almost always 
solved iteratively (no recursion).
0/1 Knapsack Recurrence 
• If wn <= y, f(n,y) = pn. 
• If wn > y, f(n,y) = 0. 
• When i < n 
 f(i,y) = f(i+1,y) whenever y < wi. 
 f(i,y) = max{f(i+1,y), f(i+1,y-wi) + pi}, y >= wi. 
• Assume the weights and capacity are integers. 
• Only f(i,y)s with 1 <= i <= n and 0 <= y <= c 
are of interest.
Iterative Solution Example 
• n = 5, c = 8, w = [4,3,5,6,2], p = [9,7,10,9,3] 
f[i][y] 0 1 2 3 4 5 6 7 8 
y 
5 
4 
3 
2 
1 
i
Compute f[5][*] 
• n = 5, c = 8, w = [4,3,5,6,2], p = [9,7,10,9,3] 
f[i][y] 0 1 2 3 4 5 6 7 8 
y 
5 
4 
3 
2 
1 
i 
0 0 3 3 3 3 3 3 3
Compute f[4][*] 
• n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] 
f[i][y] 0 1 2 3 4 5 6 7 8 
y 
5 
4 
3 
2 
1 
i 
0 0 3 3 3 3 3 3 3 
0 0 3 3 3 3 9 9 12 
f(i,y) = max{f(i+1,y), f(i+1,y-wi) + pi}, y >= wi
Compute f[3][*] 
• n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] 
f[i][y] 0 1 2 3 4 5 6 7 8 
y 
5 
4 
3 
2 
1 
i 
0 0 3 3 3 3 3 3 3 
0 0 3 3 3 3 9 9 12 
0 0 3 3 3 10 10 13 13 
f(i,y) = max{f(i+1,y), f(i+1,y-wi) + pi}, y >= wi
Compute f[2][*] 
• n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] 
f[i][y] 0 1 2 3 4 5 6 7 8 
y 
5 
4 
3 
2 
1 
i 
0 0 3 3 3 3 3 3 3 
0 0 3 3 3 3 9 9 12 
0 0 3 3 3 10 10 13 13 
0 0 3 8 8 11 11 13 18 
f(i,y) = max{f(i+1,y), f(i+1,y-wi) + pi}, y >= wi
Compute f[1][c] 
• n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] 
f[i][y] 0 1 2 3 4 5 6 7 8 
y 
5 
4 
3 
2 
1 
i 
0 0 3 3 3 3 3 3 3 
0 0 3 3 3 3 9 9 12 
0 0 3 3 3 10 10 13 13 
0 0 3 8 8 11 11 13 18 
18 
f(i,y) = max{f(i+1,y), f(i+1,y-wi) + pi}, y >= wi
Traceback 
• n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] 
f[i][y] 0 1 2 3 4 5 6 7 8 
y 
5 
4 
3 
2 
1 
i 
0 0 3 3 3 3 3 3 3 
0 0 3 3 3 3 9 9 12 
0 0 3 3 3 10 10 13 13 
0 0 3 8 8 11 11 13 18 
18 
f[1][8] = f[2][8] => x1 = 0
Traceback 
• n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] 
f[i][y] 0 1 2 3 4 5 6 7 8 
y 
5 
4 
3 
2 
1 
i 
0 0 3 3 3 3 3 3 3 
0 0 3 3 3 3 9 9 12 
0 0 3 3 3 10 
10 13 13 
0 0 3 8 8 11 11 13 18 
18 
f[2][8] != f[3][8] => x2 = 1
Traceback 
• n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] 
f[i][y] 0 1 2 3 4 5 6 7 8 
y 
5 
4 
3 
2 
1 
i 
0 0 3 3 3 3 3 3 3 
0 
0 3 3 3 3 9 9 12 
0 0 3 3 3 10 10 13 13 
0 0 3 8 8 11 11 13 18 
18 
f[3][5] != f[4][5] => x3 = 1
Traceback 
• n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] 
f[i][y] 0 1 2 3 4 5 6 7 8 
y 
5 
4 
3 
2 
1 
i 
0 3 3 3 3 3 3 3 
0 
0 0 3 3 3 3 9 9 12 
0 0 3 3 3 10 10 13 13 
0 0 3 8 8 11 11 13 18 
18 
f[4][0] = f[5][0] => x4 = 0
Traceback 
• n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] 
f[i][y] 0 1 2 3 4 5 6 7 8 
y 
5 
4 
3 
2 
1 
i 
0 0 3 3 3 3 3 3 3 
0 0 3 3 3 3 9 9 12 
0 0 3 3 3 10 10 13 13 
0 0 3 8 8 11 11 13 18 
18 
f[5][0] = 0 => x5 = 0
Complexity Of Traceback 
• O(n)
Matrix Multiplication Chains 
• Multiply an m x n matrix A and an n x p matrix 
B to get an m x p matrix C. 
n 
C(i,j) = A(i,k) * B(k,j) 
k = 1 
• We shall use the number of multiplications as 
our complexity measure. 
• n multiplications are needed to compute one 
C(i,j). 
• mnp multiplicatons are needed to compute all 
mp terms of C.
Matrix Multiplication Chains 
• Suppose that we are to compute the product X*Y*Z of 
three matrices X, Y and Z. 
• The matrix dimensions are: 
 X:(100 x 1), Y:(1 x 100), Z:(100 x 1) 
• Multiply X and Y to get a 100 x 100 matrix T. 
 100 * 1 * 100 = 10,000 multiplications. 
• Multiply T and Z to get the 100 x 1 answer. 
 100 * 100 * 1 = 10,000 multiplications. 
• Total cost is 20,000 multiplications. 
• 10,000 units of space are needed for T.
Matrix Multiplication Chains 
• The matrix dimensions are: 
 X:(100 x 1) 
 Y:(1 x 100) 
 Z:(100 x 1) 
• Multiply Y and Z to get a 1 x 1 matrix T. 
 1 * 100 * 1 = 100 multiplications. 
• Multiply X and T to get the 100 x 1 answer. 
 100 * 1 * 1 = 100 multiplications. 
• Total cost is 200 multiplications. 
• 1 unit of space is needed for T.
Product Of 5 Matrices 
• Some of the ways in which the product of 5 matrices 
may be computed. 
 A*(B*(C*(D*E))) right to left 
 (((A*B)*C)*D)*E left to right 
 (A*B)*((C*D)*E) 
 (A*B)*(C*(D*E)) 
 (A*(B*C))*(D*E) 
 ((A*B)*C)*(D*E)
Find Best Multiplication Order 
• Number of ways to compute the product of q 
matrices is O(4q/q1.5). 
• Evaluating all ways to compute the product 
takes O(4q/q0.5) time.
An Application 
• Registration of pre- and post-operative 3D brain 
MRI images to determine volume of removed 
tumor.
3D Registration
3D Registration 
• Each image has 256 x 256 x 256 voxels. 
• In each iteration of the registration algorithm, the 
product of three matrices is computed at each 
voxel … (12 x 3) * (3 x 3) * (3 x 1) 
• Left to right computation => 12 * 3 * 3 + 12 * 
3*1 = 144 multiplications per voxel per iteration. 
• 100 iterations to converge.
3D Registration 
• Total number of multiplications is about 2.4 * 
1011. 
• Right to left computation => 3 * 3*1 + 12 * 3 * 1 
= 45 multiplications per voxel per iteration. 
• Total number of multiplications is about 7.5 * 
1010. 
• With 108 multiplications per second, time is 40 
min vs 12.5 min.

More Related Content

PPT
PPTX
Definite Integrals 8/ Integration by Parts
PPT
Matdis 3.4
PPTX
Comuter graphics ellipse drawing algorithm
PPTX
Comuter graphics bresenhams circle drawing algorithm
PDF
Hog processing
PPTX
J3 j3bu5t3r5
PDF
Worksheet 3 addition & subtraction
Definite Integrals 8/ Integration by Parts
Matdis 3.4
Comuter graphics ellipse drawing algorithm
Comuter graphics bresenhams circle drawing algorithm
Hog processing
J3 j3bu5t3r5
Worksheet 3 addition & subtraction

What's hot (19)

PDF
Ch 02
PPTX
Modular arithmetic
PPT
Metric k center
PPTX
Lec05 circle ellipse
DOC
Sbma 4603 numerical methods Assignment
PDF
Factoring by the trial and-error method
PPTX
Bresenham's line drawing algorithm
PDF
Econometric Analysis 8th Edition Greene Solutions Manual
DOC
00000070 1 20130107-130231
PPTX
Computer Graphic - Lines, Circles and Ellipse
PPTX
Alg 2 : multiply ing & factoring
PDF
Finite elements : basis functions
DOCX
Class notes precalc
PDF
Ch 03
PPSX
3f. Pedagogy of Mathematics (Part II) - Algebra (Ex 3.6)
PDF
Ch 06
PDF
Bresenham derivation
DOC
00000070 1 20130107-130231
PPTX
Comuter graphics bresenhams line drawing algorithm
Ch 02
Modular arithmetic
Metric k center
Lec05 circle ellipse
Sbma 4603 numerical methods Assignment
Factoring by the trial and-error method
Bresenham's line drawing algorithm
Econometric Analysis 8th Edition Greene Solutions Manual
00000070 1 20130107-130231
Computer Graphic - Lines, Circles and Ellipse
Alg 2 : multiply ing & factoring
Finite elements : basis functions
Class notes precalc
Ch 03
3f. Pedagogy of Mathematics (Part II) - Algebra (Ex 3.6)
Ch 06
Bresenham derivation
00000070 1 20130107-130231
Comuter graphics bresenhams line drawing algorithm
Ad

Similar to Lec38 (20)

PDF
Pythonbrasil - 2018 - Acelerando Soluções com GPU
ODP
Wu Mamber (String Algorithms 2007)
PDF
Matrix chain multiplication
PDF
Introduction to machine learning algorithms
PDF
Palm ch1
PDF
PPTX
Matrix chain multiplication in design analysis of algorithm
PPT
algorthm analysis from computer scince.ppt
PPTX
5.1 sequences and summation notation t
PDF
طراحی الگوریتم فصل 1
PPT
Learn about dynamic programming and how to design algorith
PDF
PRE: Datamining 2nd R
PDF
Datamining R 1st
PDF
PPTX
Unit-1 Basic Concept of Algorithm.pptx
PDF
Datamining r 1st
PPTX
Definite Integral 1.pptx
PPT
Introduction to MatLab programming
DOC
Set 1 mawar
PDF
Numeros reales, inecuaciones y desigualdades
Pythonbrasil - 2018 - Acelerando Soluções com GPU
Wu Mamber (String Algorithms 2007)
Matrix chain multiplication
Introduction to machine learning algorithms
Palm ch1
Matrix chain multiplication in design analysis of algorithm
algorthm analysis from computer scince.ppt
5.1 sequences and summation notation t
طراحی الگوریتم فصل 1
Learn about dynamic programming and how to design algorith
PRE: Datamining 2nd R
Datamining R 1st
Unit-1 Basic Concept of Algorithm.pptx
Datamining r 1st
Definite Integral 1.pptx
Introduction to MatLab programming
Set 1 mawar
Numeros reales, inecuaciones y desigualdades
Ad

More from Nikhil Chilwant (20)

TXT
The joyless economy
PPTX
IIT Delhi training pioneer ct (acemicromatic )
PPT
PPT
PPT
PPT
PPT
PPT
PPT
PPT
PPT
PPT
PPT
PPT
PPT
PPT
PPT
PPT
PPT

Lec38

  • 1. Dynamic Programming • Steps. View the problem solution as the result of a sequence of decisions. Obtain a formulation for the problem state. Verify that the principle of optimality holds. Set up the dynamic programming recurrence equations. Solve these equations for the value of the optimal solution.  Perform a traceback to determine the optimal solution.
  • 2. Dynamic Programming • When solving the dynamic programming recurrence recursively, be sure to avoid the recomputation of the optimal value for the same problem state. • To minimize run time overheads, and hence to reduce actual run time, dynamic programming recurrences are almost always solved iteratively (no recursion).
  • 3. 0/1 Knapsack Recurrence • If wn <= y, f(n,y) = pn. • If wn > y, f(n,y) = 0. • When i < n  f(i,y) = f(i+1,y) whenever y < wi.  f(i,y) = max{f(i+1,y), f(i+1,y-wi) + pi}, y >= wi. • Assume the weights and capacity are integers. • Only f(i,y)s with 1 <= i <= n and 0 <= y <= c are of interest.
  • 4. Iterative Solution Example • n = 5, c = 8, w = [4,3,5,6,2], p = [9,7,10,9,3] f[i][y] 0 1 2 3 4 5 6 7 8 y 5 4 3 2 1 i
  • 5. Compute f[5][*] • n = 5, c = 8, w = [4,3,5,6,2], p = [9,7,10,9,3] f[i][y] 0 1 2 3 4 5 6 7 8 y 5 4 3 2 1 i 0 0 3 3 3 3 3 3 3
  • 6. Compute f[4][*] • n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] f[i][y] 0 1 2 3 4 5 6 7 8 y 5 4 3 2 1 i 0 0 3 3 3 3 3 3 3 0 0 3 3 3 3 9 9 12 f(i,y) = max{f(i+1,y), f(i+1,y-wi) + pi}, y >= wi
  • 7. Compute f[3][*] • n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] f[i][y] 0 1 2 3 4 5 6 7 8 y 5 4 3 2 1 i 0 0 3 3 3 3 3 3 3 0 0 3 3 3 3 9 9 12 0 0 3 3 3 10 10 13 13 f(i,y) = max{f(i+1,y), f(i+1,y-wi) + pi}, y >= wi
  • 8. Compute f[2][*] • n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] f[i][y] 0 1 2 3 4 5 6 7 8 y 5 4 3 2 1 i 0 0 3 3 3 3 3 3 3 0 0 3 3 3 3 9 9 12 0 0 3 3 3 10 10 13 13 0 0 3 8 8 11 11 13 18 f(i,y) = max{f(i+1,y), f(i+1,y-wi) + pi}, y >= wi
  • 9. Compute f[1][c] • n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] f[i][y] 0 1 2 3 4 5 6 7 8 y 5 4 3 2 1 i 0 0 3 3 3 3 3 3 3 0 0 3 3 3 3 9 9 12 0 0 3 3 3 10 10 13 13 0 0 3 8 8 11 11 13 18 18 f(i,y) = max{f(i+1,y), f(i+1,y-wi) + pi}, y >= wi
  • 10. Traceback • n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] f[i][y] 0 1 2 3 4 5 6 7 8 y 5 4 3 2 1 i 0 0 3 3 3 3 3 3 3 0 0 3 3 3 3 9 9 12 0 0 3 3 3 10 10 13 13 0 0 3 8 8 11 11 13 18 18 f[1][8] = f[2][8] => x1 = 0
  • 11. Traceback • n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] f[i][y] 0 1 2 3 4 5 6 7 8 y 5 4 3 2 1 i 0 0 3 3 3 3 3 3 3 0 0 3 3 3 3 9 9 12 0 0 3 3 3 10 10 13 13 0 0 3 8 8 11 11 13 18 18 f[2][8] != f[3][8] => x2 = 1
  • 12. Traceback • n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] f[i][y] 0 1 2 3 4 5 6 7 8 y 5 4 3 2 1 i 0 0 3 3 3 3 3 3 3 0 0 3 3 3 3 9 9 12 0 0 3 3 3 10 10 13 13 0 0 3 8 8 11 11 13 18 18 f[3][5] != f[4][5] => x3 = 1
  • 13. Traceback • n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] f[i][y] 0 1 2 3 4 5 6 7 8 y 5 4 3 2 1 i 0 3 3 3 3 3 3 3 0 0 0 3 3 3 3 9 9 12 0 0 3 3 3 10 10 13 13 0 0 3 8 8 11 11 13 18 18 f[4][0] = f[5][0] => x4 = 0
  • 14. Traceback • n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3] f[i][y] 0 1 2 3 4 5 6 7 8 y 5 4 3 2 1 i 0 0 3 3 3 3 3 3 3 0 0 3 3 3 3 9 9 12 0 0 3 3 3 10 10 13 13 0 0 3 8 8 11 11 13 18 18 f[5][0] = 0 => x5 = 0
  • 16. Matrix Multiplication Chains • Multiply an m x n matrix A and an n x p matrix B to get an m x p matrix C. n C(i,j) = A(i,k) * B(k,j) k = 1 • We shall use the number of multiplications as our complexity measure. • n multiplications are needed to compute one C(i,j). • mnp multiplicatons are needed to compute all mp terms of C.
  • 17. Matrix Multiplication Chains • Suppose that we are to compute the product X*Y*Z of three matrices X, Y and Z. • The matrix dimensions are:  X:(100 x 1), Y:(1 x 100), Z:(100 x 1) • Multiply X and Y to get a 100 x 100 matrix T.  100 * 1 * 100 = 10,000 multiplications. • Multiply T and Z to get the 100 x 1 answer.  100 * 100 * 1 = 10,000 multiplications. • Total cost is 20,000 multiplications. • 10,000 units of space are needed for T.
  • 18. Matrix Multiplication Chains • The matrix dimensions are:  X:(100 x 1)  Y:(1 x 100)  Z:(100 x 1) • Multiply Y and Z to get a 1 x 1 matrix T.  1 * 100 * 1 = 100 multiplications. • Multiply X and T to get the 100 x 1 answer.  100 * 1 * 1 = 100 multiplications. • Total cost is 200 multiplications. • 1 unit of space is needed for T.
  • 19. Product Of 5 Matrices • Some of the ways in which the product of 5 matrices may be computed.  A*(B*(C*(D*E))) right to left  (((A*B)*C)*D)*E left to right  (A*B)*((C*D)*E)  (A*B)*(C*(D*E))  (A*(B*C))*(D*E)  ((A*B)*C)*(D*E)
  • 20. Find Best Multiplication Order • Number of ways to compute the product of q matrices is O(4q/q1.5). • Evaluating all ways to compute the product takes O(4q/q0.5) time.
  • 21. An Application • Registration of pre- and post-operative 3D brain MRI images to determine volume of removed tumor.
  • 23. 3D Registration • Each image has 256 x 256 x 256 voxels. • In each iteration of the registration algorithm, the product of three matrices is computed at each voxel … (12 x 3) * (3 x 3) * (3 x 1) • Left to right computation => 12 * 3 * 3 + 12 * 3*1 = 144 multiplications per voxel per iteration. • 100 iterations to converge.
  • 24. 3D Registration • Total number of multiplications is about 2.4 * 1011. • Right to left computation => 3 * 3*1 + 12 * 3 * 1 = 45 multiplications per voxel per iteration. • Total number of multiplications is about 7.5 * 1010. • With 108 multiplications per second, time is 40 min vs 12.5 min.

Editor's Notes

  • #5: Note: no recomputation recursive version computes f(1,c) and some (maybe even all) f(i,y), i &amp;gt; 1, 0 &amp;lt;= y &amp;lt;= c. Iterative version computes f(1,c) and all f(i,y), i &amp;gt; 1, 0 &amp;lt;= y &amp;lt;= c.