Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Programming Solving Optimization Problems.

Similar presentations


Presentation on theme: "Dynamic Programming Solving Optimization Problems."— Presentation transcript:

1 Dynamic Programming Solving Optimization Problems

2 Last Class “Those who cannot …”

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

4 Dynamic Programming Applications –Finding potential partnersFinding potential partners (Optimal Stopping Problems) –Matching protein sequences (BioInformatics) –Scheduling jobs on a processor (Parallel Computing) –Shortest Path computations (MapQuest)

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

6 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

7 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

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

9 Fibonacci-DP A sub-problem depends on only two predecessor sub-problems. Time complexity : O(n) Space complexity : O(1)

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

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

12 Matrix Multiplication Order Problem. What is the best way to multiply M 1, M 2, M 3, …, M n (when n > 2) –Where M i has dimensions d i-1 x d i –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?

13 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? i matricesn-i matrices

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

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

16 Naïve Top-Down Approach

17 Running Time? Exponential or Polynomial? Any easy lower bounds?

18 Top-Down Dynamic Program

19 The cost function If L + 1 < h If L+1 = h then m[l,h] = 0.

20 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?)

21 The Bottom Up code int 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]; }

22 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)?

23 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;

24 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

25 Edit Distance Applications –Spell Checking –Genomics –Morphing/Vision

26 Edit Distance Example –algorithm to logarithm? –algorithm –lgorithm (delete) –logorithm (Insert) –logarithm (Replace) Edit Distance = 3

27 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?

28 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]

29 Edit Distance Recursion Replace Delete s[i] Insert t[j]

30 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?

31 Loop Assignment for i = 1 to n for 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) );

32


Download ppt "Dynamic Programming Solving Optimization Problems."

Similar presentations


Ads by Google