Lecture 9 Dynamic programming • What is Dynamic Programming • How to write Dynamic Programming paradigm • When to apply Dynamic Programming
Roadmap • What is Dynamic Programming • How to write DP paradigm • Knapsack problem • Longest common subsequence • Matrix chain multiplication • When to apply Dynamic Programming • Practice
Techniques Based on Recursion • Induction or Tail recursion • Non-overlapping subproblems • Overlapping subproblems
An Introductory Example 19th century statue of Fibonacci in Camposanto, Pisa. (Source from Wikipedia) Problem: Fibonacci Input: A positive integer n Output:Fib(n)
Quiz Problem: Fibonacci Input: A positive integer n Output:Fib(n) Give your solution of Fibonacci and explain the time/space complexity.
Recursion implementation int fib_rec (int n){ if (n==1 || n==2) { return1; } else { return fib_rec(n-1) + fib_rec(n-2); } } O
O(n) time, O(n) space int fib (int n){ int* array = newint[n]; array[0] = 0; array[1] = 1; for (int i = 2; i <= n; i++) array[i] = array[i-1] + array[i-2]; return array[n]; }
We can do even better: O(n) time, O(1) space int fib (int n){ if (n < 2) returnn; int f0 = 0; int f1 = 1; int i = 2; while (i <= n) { f1 = f1 + f0; f0 = f1 - f0; i++; } return f1; }
O(log n) time, O(1) space Use Divide-and-conquer
Recursion? No, thanks. Fibonacci Overlapping subproblems 19th century statue of Fibonacci in Camposanto, Pisa. (Source from Wikipedia)
Dynamic programming Non-recursively handle recursive problems • Find smaller subproblems • Write the relation expression • Use an array to save the intermediate results • Update the array iteratively. Time-Memorytradeoff
All-pairs shortest path
Θ(n3) Floyd-Warshall algorithm
Where are we? • What is Dynamic Programming • How to write DP paradigm • Knapsack problem • Longest common subsequence • Matrix chain multiplication • When to apply Dynamic Programming • Practice
Knapsack problem Problem: Knapsack Input: A set of items U = {u1,...,un} with sizes s1,s2,...,sn and values v1,v2,...,vn and a knapsack capacity C Output:The maximum value that can be put into the knapsack
Knapsack problem V[i,j]: the maximum value obtained by filling a knapsack of size j with items taken from the first i items {u1,...,ui}. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 0 2 3 4 10 12 13 14 15 15 15 15 15 15 15 15 0 2 3 4 10 12 13 14 15 15 15 15 15 15 15 15
Time: Θ(nC) • Space: Θ(nC) Knapsack problem Psuedo-polynomial algorithm
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 0 2 3 4 10 12 13 14 15 15 15 15 15 15 15 15 0 2 3 4 10 12 13 14 15 15 15 15 15 15 15 15 How to construct the solution? V
Knapsack problem What if the thief robs a super-market? Knapsack with repetition.
Longest common subsequence Problem: LCS Input: Two strings A=a1a2...an and B=b1b2...bm Output: The longest common subsequence of A and B L[i,j]: length of the longest common subsequence of A[1..i] and B[1..j] ababe ababe Solution: L[n,m] abcabc abcabc
LCS 0 0 0 0 0 0 0 1 0 1 1 1 1 1 2 2 0 1 2 2 2 1 3 3 3 2 0 2 4 3 1 2 4 0 2 0 2 2 4 1 3 4
Time: Θ(mn) • Space: Θ(max{m,n}) LCS
Longest common substring? L[i,j]: length of longest common sub-postfix of a[1..i] and b[1..j]. Solution: the maximum L[i,j] for all i>0 and j>0
Matrix chain multiplication What is the most efficient way of computing the following multiplication? The order of multiplications matters.
Quiz How many different ways to compute M1 * M2 * … * Mn
Matrix chain multiplication Problem: MCM Input: A matrix chain M1M2...Mn with rank r1r2..rnrn+1 Output: Minimal cost of the multiplication ABCD 50,20,1,10,100
Dynamic programming C[i,j]: the minimal cost of the multiplication MiMi +1 ... Mj Memoization 1000 1500 7000 ABCD 50,20,1,10,100 200 3000 1000
MCM
1000 2 1500 3 7000 3 200 3 3000 4 1000 4 Dynamic programming C[i,j]: the minimal cost of the multiplication MiMi +1 ... Mj ABCD 50,20,1,10,100
Where are we? • What is Dynamic Programming • How to write DP paradigm • Knapsack problem • Longest common subsequence • Matrix chain multiplication • When to apply Dynamic Programming • Practice
Elements of DP • Optimal substructure • A problem exhibits optimal substructure if an optimal solution to the problem contains within it optimal solutions to subproblems. • Overlapping subproblems
Optimal Substructure Given a graph G=<V,E>, which problem has optimal substructure, longest simple path problem or shortest simple path problem?
Practice • Subset sum • Longest palindrome sequence • Editing distance • Longest increasing subsequence • Planning a company party
Subset sum problem Problem: SubsetSum Input: A set of numbers A={a1,a2,...,an}, and a sum s Output:Yes, if there exists a subset B⊆A such that the sum of B equals to s; No, otherwise. L[i,j]: Does there exists a subset B of {a1,...,ai} such that the sum of B equals to j Solution: L[n,s]
Longest palindrome subsequence A palindrome is a nonempty string over some alphabet that reads the same for- ward and backward. civic, racecar Problem: PalindromeSubsequence Input: A string A=a1a2...an Output: Length of the longest subsequence of A that is a palindrome • We can also reduce this problem to the Longest Common Subsequence problem of a1a2..an and an...a2a1. (by Qiu Zhe.) L[i,j]: length of the longest palindrome subsequence of ai...aj L[i,j] = 1 if i = j L[i,j] = L[i+1, j-1] if ai = aj L[i,j] = max{L[i+1,j], L[i,j-1]} o.w
Editing distance snowy --> sunny The edit distance between two strings is the cost of their best possible alignment. E[i,j]: edit distance of a1...aiand b1...bj
Planning a company party 2 We like convivial friends. And we will not invite both the employee and his/her direct supervisor. How to maximize the conviviality? 4 9
Single-source longest path in DAG D[s] = 0 D[v] = max(u,v)\in E{D[u]+w(u,v)}
Conclusion • What is Dynamic Programming • How to write DP paradigm • Knapsack problem • Longest common subsequence • Matrix chain multiplication • When to apply Dynamic Programming • Practice