32
Dynamic Programming Solving Optimization Problems

Dynamic Programming Solving Optimization Problems

  • View
    240

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Dynamic Programming Solving Optimization Problems

Dynamic Programming

Solving Optimization Problems

Page 2: Dynamic Programming Solving Optimization Problems

Last Class

• “Those who cannot …”

Page 3: Dynamic Programming Solving Optimization Problems

Dynamic Programming

• Optimization problems have many solutions (usually we want the ‘best’ solution of some sort). Like the shortest path, the easiest way, the minimal cost, the maximum gain etc.

Page 4: Dynamic Programming Solving Optimization Problems

Dynamic Programming

• Applications– Finding potential partners

• (Optimal Stopping Problems)

– Matching protein sequences (BioInformatics)

– Scheduling jobs on a processor (Parallel Computing)

– Shortest Path computations (MapQuest)

Page 5: Dynamic Programming Solving Optimization Problems

Main Features

• Replaces Exp-Time with Poly-Time algs

• Solves Optimization Problems• Similar to D & C but sub-

problems are overlapping ( Hence, Doesn’t resolve sub-problems)

Page 6: Dynamic Programming Solving Optimization Problems

Basic Elements

• Dictionary or Table• Polynomial number of Sub-

problems• DP-Principle

– For global problems to be solved optimally, each sub-problem should be solved optimally

• Top down (a.k.a Memoization) – Vs Bottom Up

Page 7: Dynamic Programming Solving Optimization Problems

Optimal Substructure

• A problem exhibits optimal substructure if an optimal solution contains within it optimal solutions to subproblems– Build an optimal solution from

optimal solutions to subproblems

Page 8: Dynamic Programming Solving Optimization Problems

Dynamic Programming

• The sub-problem graph (DAG). • Doing DFS on sub-problem

graph tells us in which order to solve sub-problems. (a.k.a. Reverse Topological Sorting).

Page 9: Dynamic Programming Solving Optimization Problems

Fibonacci-DP

• A sub-problem depends on only two predecessor sub-problems.

• Time complexity : O(n)• Space complexity : O(1)

Page 10: Dynamic Programming Solving Optimization Problems

Dynamic Programming-Recipe

1. Write the naïve top down algorithm (Recursive D&C)

2. Use Dictionary on (1)3. Analyze sub-problem graph and simplify the

dynamic program if possible. (e.g. Fibonacci)4. Optional : Decide how to get the solution of the

problem using the data in the dictionary (Only applicable to some problems).

Page 11: Dynamic Programming Solving Optimization Problems

Matrix Multiplication

• Recall that multiplying p x q matrix with q x r matrix takes p.q.r element wise multiplications.

• Matrix multiplication is associative i.e. – (A x B) x C = A x (B x C)

Page 12: Dynamic Programming Solving Optimization Problems

Matrix Multiplication Order Problem.

• What is the best way to multiply M1, M2 , M3 , … , Mn (when n > 2)

– Where Mi has dimensions di-1 x di

– We saw an example in the last class.

• What is the minimum number of multiplications required to compute the product? What is the running time?

Page 13: Dynamic Programming Solving Optimization Problems

How many parenthesizations?

• For n matrices? • O(n) ? O(n^3)? O(2^n) ? even

worse?• What is the recursive relation that

gives us the count?

)...()...( 121 nii xMxMxxMxxMM

i matrices n-i matrices

Page 14: Dynamic Programming Solving Optimization Problems

Recursive relation

3

1

1

2 4

1

1)()()()(

nC

nnCinPiPnP

nn

i

nn

P(n) = 1 for n = 1. This shows that there are exponential number of paranthesizations.

Page 15: Dynamic Programming Solving Optimization Problems

The sub-problems

• What are the sub-problems when we want to optimize how we want to multiply?

Page 16: Dynamic Programming Solving Optimization Problems

Naïve Top-Down Approach

Page 17: Dynamic Programming Solving Optimization Problems

Running Time?

• Exponential or Polynomial? • Any easy lower bounds?

Page 18: Dynamic Programming Solving Optimization Problems

Top-Down Dynamic Program

Page 19: Dynamic Programming Solving Optimization Problems

The cost function

)],[],[(min],[ hilhil dddhimilmhlm

If L + 1 < h

If L+1 = h then m[l,h] = 0.

Page 20: Dynamic Programming Solving Optimization Problems

Bottom-Up Dynamic Programming

• Is Depth First Search Unique?• How should we get rid of the

recursion in this case (Remember how we did it for Fibonacci?)

Page 21: Dynamic Programming Solving Optimization Problems

The Bottom Up codeint bestCost(int left, int right, int *darray){

for (i = right; i >= left; --i){ // rows for(j = left; j <= right; ++j){ // columns if((i >= j) || (j – i == 1)) continue;

bc = INF; for(k = i+1; k < j; ++k){

int cost = bcMatrix[i][k] + bcMatrix[k][j] darray[i] * darray[k] * darray[j]; if(cost < bc) bc = cost;

} bcMatrix[i][j] = bc; } } return bcMatrix[left][right];}

Page 22: Dynamic Programming Solving Optimization Problems

Knapsack

• Subproblems :– B[n,k] = There exists a subset of

(s[1],s[2],…,s[n]) that exactly fills up a knapsack of size k. [It’s a boolean variable]

– B[n,k] = B[n-1,k] or B[n-1, k-s[n]]– How many total such subproblems?

O(nk)?

Page 23: Dynamic Programming Solving Optimization Problems

Knapsack: Recursive

bool B(int n, int k)if( n == 0 && k == 0) return true;

if( n == 0 && k > 0) return false; if ( B(n-1,k) || ((k-s[n] >= 0) && B(n-1,k-

s[n]))) return true;

return false;

Page 24: Dynamic Programming Solving Optimization Problems

Edit Distance

• Given : Strings s[1..n] and t[1..m]• Find fewest number of edits

needed to convert s to t where ‘edit’ is :– Deleting a character– Inserting a character– Changing a character

• a.k.a Levenshtein distance

Page 25: Dynamic Programming Solving Optimization Problems

Edit Distance

• Applications– Spell Checking– Genomics– Morphing/Vision

Page 26: Dynamic Programming Solving Optimization Problems

Edit Distance

• Example– algorithm to logarithm?– algorithm– lgorithm (delete)– logorithm (Insert)– logarithm (Replace)

•Edit Distance = 3

Page 27: Dynamic Programming Solving Optimization Problems

Edit Distance

• Subproblems?– What do we want?

•Small number of Subproblems (Polynomial?)

•Should be able to recover the solution easily from other smaller subproblems.

• Edit distance d(i,j) = Edit distance between s[1..i] and t[1..j] ? Does it satisfy what we want?

Page 28: Dynamic Programming Solving Optimization Problems

Edit Distance

• How can we solve the subproblem (i,j) by looking at smaller subproblems?– Solve s[1..i] to t[1..j]– given the edit distances between

•s[1..i-1] and t[1..j-1] and …

– What if we look at s[i] and t[j]

Page 29: Dynamic Programming Solving Optimization Problems

Edit Distance Recursion

1, 1

, 1,

, 1

0 if [ ] [ ]

1 else

min 1

1

i j

i j i j

i j

s i t jd

d d

d

Replace

Delete s[i]

Insert t[j]

Page 30: Dynamic Programming Solving Optimization Problems

Base Cases

– To turn empty string to t[1..j], do j inserts– To turn s[1..i] to empty string, do i deletes

d(i,0) = i; for I = 1 .. m (deletes)d(0,j) = j; for j = 1 to n (inserts)

Can we fill up d(i,j) using this table and the recursion given? In what order?

What is the space requirement? Running time?

Page 31: Dynamic Programming Solving Optimization Problems

Loop Assignment

for i = 1 to nfor j = 1 to m

d(i,j) = min(d(i-1,j) + 1,d(i,j-1) + 1,d(i-1,j-1)+((s[i] == t[j])?0:1));

Page 32: Dynamic Programming Solving Optimization Problems