Docsity

Steps in Dynamic Programming - Algorithms and Applications in Java - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Steps in Dynamic Programming - Algorithms and Applications in Java - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

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).

Iterative Solution Example

  • n = 5, c = 8, w = [4,3,5,6,2], p = [9,7,10,9,3]

y

f[i][y]

i

Compute f[5][*]

  • n = 5, c = 8, w = [4,3,5,6,2], p = [9,7,10,9,3]

y

f[i][y]

i

Compute f[3][*]

  • n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3]

y

f[i][y]

i

f(i,y) = max{f(i+1,y), f(i+1,y-w i ) + p i }, y >= w i

Compute f[2][*]

  • n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3]

y

f[i][y]

i

f(i,y) = max{f(i+1,y), f(i+1,y-w i ) + p i }, y >= w i

Traceback

  • n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3]

y

f[i][y]

i

f[1][8] = f[2][8] => x 1 = 0 Docsity.com

Traceback

  • n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3]

y

f[i][y]

i

f[2][8] != f[3][8] => x 2 = 1

Traceback

  • n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3]

y

f[i][y]

i

f[4][0] = f[5][0] => x 4 = 0

Traceback

  • n = 5, c = 8, w = [4,3,5,6,2], p = [9,8,10,9,3]

y

f[i][y]

i

f[5][0] = 0 => x 5 = 0 Docsity.com

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.

k = 1

n

C(i,j) = A(i,k) * B(k,j)

  • 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 XYZ 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.

Product Of 5 Matrices

  • Some of the ways in which the product of 5 matrices may be computed.  A(B(C(DE))) right to left  (((AB)C)D)E left to right  (AB)((CD)E)  (AB)(C(DE))  (A(BC))(DE)  ((AB)C)(DE)

Find Best Multiplication Order

  • Number of ways to compute the product of q

matrices is O(4 q/q 1.5).

  • Evaluating all ways to compute the product

takes O(4 q/q 0.5) time.