Post on 20-Dec-2015
transcript
Dynamic Programming
Solving Optimization Problems
Last Class
• “Those who cannot …”
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.
Dynamic Programming
• Applications– Finding potential partners
• (Optimal Stopping Problems)
– Matching protein sequences (BioInformatics)
– Scheduling jobs on a processor (Parallel Computing)
– Shortest Path computations (MapQuest)
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)
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
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
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).
Fibonacci-DP
• A sub-problem depends on only two predecessor sub-problems.
• Time complexity : O(n)• Space complexity : O(1)
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).
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)
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?
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
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.
The sub-problems
• What are the sub-problems when we want to optimize how we want to multiply?
Naïve Top-Down Approach
Running Time?
• Exponential or Polynomial? • Any easy lower bounds?
Top-Down Dynamic Program
The cost function
)],[],[(min],[ hilhil dddhimilmhlm
If L + 1 < h
If L+1 = h then m[l,h] = 0.
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?)
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];}
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)?
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;
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
Edit Distance
• Applications– Spell Checking– Genomics– Morphing/Vision
Edit Distance
• Example– algorithm to logarithm?– algorithm– lgorithm (delete)– logorithm (Insert)– logarithm (Replace)
•Edit Distance = 3
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?
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]
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]
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?
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));