Dynamic Programming ACM Workshop 24 August 2011. Dynamic Programming Dynamic Programming is a...

Preview:

Citation preview

Dynamic Programming

ACM Workshop

24 August 2011

Dynamic Programming

Dynamic Programming is a programming technique that dramatically reduces the runtime of some algorithms from exponential to polynomial.

Not all problems have DP characteristics

Richard Bellman was one of the principal founders of this approach.

Recursion

.for

21

11

00

i>1i

Fi

FiF

F

F

The Fibonacci numbers are defined by the following recurrence:

Recursive code for Fibonacci

Int fib(int n){ if(n==0 || n==1 ) return 1; else return fib(n-1) + fib(n-2);}

DP solution

The above algo is of exponential order.You can have O(n) solution using DP

Tabular computation

The tabular computation can avoid recompuation.

553421138532110

F10F9F8F7F6F5F4F3F2F1F0

Two key ingredients

Two key ingredients for an optimization problem to be suitable for a dynamic-programming solution:

Each substructure is optimal.

(Principle of optimality)

1. optimal substructures

2. overlapping subproblems

Subproblems are dependent.

(otherwise, a divide-and-conquer approach is the choice.)

Three basic components

The development of a dynamic-programming algorithm has three basic components: The recurrence relation (for defining

the value of an optimal solution); The tabular computation (for

computing the value of an optimal solution);

The traceback (for delivering an optimal solution).

Maximum sum

You have a sequence of integers.You need the maximum sum from the continuous sub-sequences.

Eg- 3, -4 , 5 , -7 , 8 , -6 , 2 1, -14 , -9 , 19

Max sum from 8,-6,21 which gives 23Brute-force is O(n^3).We can check sums of all sequences of

different sizes from 1 to n.

Solving Problem is no problem . Actual problem is understanding the problem!!!

Formulation of Linear Recurrence

Let S[i] be the max sum of a continuous sequence that starts with any index and ends at i.

S[i]=max(Arr[i],Arr[i]+S[i-1])

DP Solution

Longest increasing subsequence(LIS)

The longest increasing subsequence is to find a longest increasing subsequence of a given sequence of distinct integers a1a2…an .

e.g. 9 2 5 3 7 11 8 10 13 6

2 3 7

• 7 10 13

• 8 11

5 3 11 13

are increasing subsequences.

are not increasing subsequences.

We want to find a longest one.

A naive approach for LIS

Let L[i] be the length of a longest increasing subsequence ending at position i.

L[i] = 1 + max j = 0..to..i-1{L[j] where aj < ai}

Prev[j]=k9 2 5 3 7 11 8 10 13 6

DP Solution

9 52Arr 3 117 8 1310 6

-1 1-1Prev 1 42 4 76 2

1 21L 2 43 4 65 3

Binomial Coefficients (x + y)2 = x2 + 2xy + y2, coefficients are 1,2,1 (x + y)3 = x3 + 3x2y + 3xy2 + y3, coefficients are 1,3,3,1 (x + y)4 = x4 + 4x3y + 6x2y2 + 4xy3 + y4,

coefficients are 1,4,6,4,1 (x + y)5 = x5 + 5x4y + 10x3y2 + 10x2y3 + 5xy4 + y5,

coefficients are 1,5,10,10,5,1 The n+1 coefficients can be computed for (x + y)n according to

the formula c(n, i) = n! / (i! * (n – i)!)for each of i = 0..n

The repeated computation of all the factorials gets to be expensive We can use dynamic programming to save the factorials as we go

Solution by dynamic programming

n c(n,0) c(n,1) c(n,2) c(n,3) c(n,4) c(n,5) c(n,6) 0 1 1 1 1 2 1 2 1 3 1 3 3 1 4 1 4 6 4 1 5 1 5 10 10 5 1 6 1 6 15 20 15 6 1 Each row depends only on the preceding row Only linear space and quadratic time are needed This algorithm is known as Pascal’s Triangle

Matrix-chain multiplication

If the chain of matrices is A1, A2, A3, A4, the product A1 A2 A3 A4 can

be fully parenthesized in five distinct ways:(A1 (A2 (A3 A4))) ,(A1 ((A2 A3) A4)) ,((A1 A2) (A3 A4)) ,((A1 (A2 A3)) A4) ,(((A1 A2) A3) A4).

Matrix-chain multiplication

We can multiply two matrices A and B only if they are compatible: the number of columns of A must equal the number of rows of B. If A is a p × q matrix and B is a q × r matrix, the resulting matrix C is a p × r matrix.

Matrix-chain multiplication

Three matrices: A1: 10 × 100, A2: 100 × 5 , A3: 5 × 50

((A1 A2) A3) -> we perform 10 · 100 · 5 = 5000 scalar multiplications to compute the 10 × 5 matrix product A1 A2, plus another 10 · 5 · 50 = 2500 scalar multiplications to multiply this matrix by A3, for a total of 7500 scalar multiplications.

(A1 (A2 A3))-> we perform 100 · 5 · 50 = 25,000 scalar multiplications to compute the 100 × 50 matrix product A2 A3, plus another 10 ·100 · 50 = 50,000 scalar multiplications to multiply A1 by this matrix, for a total of 75,000scalar multiplications.

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, fully parenthesize the product A1 A2 An in a way that minimizes the number of scalar multiplications.

Step 2: A recursive solution

Now we use our optimal substructure to show that we can construct an optimal solution to the

problem from optimal solutions to subproblems.

The three expressions here represent the combinations - A2*(A3…A5) -(A2..A3)*(A4..A5) -(A.2...A4)*A5 Respectively.

List of problems(LIS)

1 History Grading 231 Testing the catcher1 What goes up10131 Is bigger smarter

Problem statement:Given a bag of Capacity W and objects

with some weights and profit associated. Find the best combination to maximize the profit.

0-1 Knapsack problem

0-1 knapsack Problem

Make all possible combination of objects and select the lot with maximum profit.

For n objects total possible combinations are

2^n

For n=100 total possibilities will be 2^100

Naïve approach

Around 10^30 secs……

Recursively the 0-1-knapsack problem can be formulated as:

A(0, Y) = 0 A(j, 0) = 0 A(j, Y) = A(j − 1, Y)  if wj > Y A(j, Y) = max { A(j − 1, Y),  pj + A(j − 1, Y

− wj) }  if wj ≤ Y.

Recursive relation

Suppose we have gold bars of following weights 2 ,3,4 and we have a bag of capacity 5 kgs. Now using the dynamic programming approach we come up with following table.

Dynamic programming approach

Table 0 1 2 3 4 5

0 0 0 0 0 0

2 0 2 2

2 2

3 0 2 3 3 5

4 0 2 3 4 5

Filling the table finally arr[m][n] gives the value of best possible combination.

0 1 2 3 4 5

0 0 0 0 0 0

2 u c l l l

3 u u c l c

4 u u u c u

Printing the solution

Moving from the arr[m][n] we get following path u->c->c->0So the answer will be 2,3.

1. 562 Dividing coins2. 624 CD3. 990 Diving for gold4. 10130 Super sale

List of problems

Recommended