1 / 39

Lecture 9 Dynamic programming

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

tomita
Download Presentation

Lecture 9 Dynamic programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 9 Dynamic programming • What is Dynamic Programming • How to write Dynamic Programming paradigm • When to apply Dynamic Programming

  2. Roadmap • What is Dynamic Programming • How to write DP paradigm • Knapsack problem • Longest common subsequence • Matrix chain multiplication • When to apply Dynamic Programming • Practice

  3. Techniques Based on Recursion • Induction or Tail recursion • Non-overlapping subproblems • Overlapping subproblems

  4. An Introductory Example 19th century statue of Fibonacci in Camposanto, Pisa. (Source from Wikipedia) Problem: Fibonacci Input: A positive integer n Output:Fib(n)

  5. Quiz Problem: Fibonacci Input: A positive integer n Output:Fib(n) Give your solution of Fibonacci and explain the time/space complexity.

  6. Recursion implementation int fib_rec (int n){ if (n==1 || n==2) { return1; } else { return fib_rec(n-1) + fib_rec(n-2); } } O

  7. 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]; }

  8. 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; }

  9. O(log n) time, O(1) space Use Divide-and-conquer

  10. Recursion? No, thanks. Fibonacci Overlapping subproblems 19th century statue of Fibonacci in Camposanto, Pisa. (Source from Wikipedia)

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

  12. All-pairs shortest path

  13. Θ(n3) Floyd-Warshall algorithm

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

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

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

  17. Time: Θ(nC) • Space: Θ(nC) Knapsack problem Psuedo-polynomial algorithm

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

  19. Knapsack problem What if the thief robs a super-market? Knapsack with repetition.

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

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

  22. Time: Θ(mn) • Space: Θ(max{m,n}) LCS

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

  24. Matrix chain multiplication What is the most efficient way of computing the following multiplication? The order of multiplications matters.

  25. Quiz How many different ways to compute M1 * M2 * … * Mn

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

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

  28. MCM

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

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

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

  32. Optimal Substructure Given a graph G=<V,E>, which problem has optimal substructure, longest simple path problem or shortest simple path problem?

  33. Practice • Subset sum • Longest palindrome sequence • Editing distance • Longest increasing subsequence • Planning a company party

  34. 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]

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

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

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

  38. Single-source longest path in DAG D[s] = 0 D[v] = max(u,v)\in E{D[u]+w(u,v)}

  39. Conclusion • What is Dynamic Programming • How to write DP paradigm • Knapsack problem • Longest common subsequence • Matrix chain multiplication • When to apply Dynamic Programming • Practice

More Related