1 / 36

Instructor: Shengyu Zhang

CSC3160: Design and Analysis of Algorithms. Week 5: Dynamic Programming. Instructor: Shengyu Zhang. About midterm. Time: 2:50pm – 4:50pm. Feb 25: Mar 4: Mar 11: Place: This lecture room. Open book, open lecture notes. But no Internet is allowed . Scope: First 5/6/7 lectures.

mikko
Download Presentation

Instructor: Shengyu Zhang

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. CSC3160: Design and Analysis of Algorithms Week 5: Dynamic Programming Instructor: Shengyu Zhang

  2. About midterm • Time: 2:50pm – 4:50pm. • Feb 25: • Mar 4: • Mar 11: • Place: This lecture room. • Open book, open lecture notes. • But no Internet is allowed. • Scope: First 5/6/7 lectures

  3. Dynamic Programming • A simple but non-trivial method for designing algorithms • Achieve much better efficiency than naïve ones. • A couple of examples will be exhibited and analyzed.

  4. Problem 1: Chain matrix multiplication

  5. Suppose we want to multiply four matrices • Suppose we want to multiply four matrices: . • Dimensions: , , , • Assume: cost . • : • : • Question: In what order should we multiply them? The order matters!

  6. Key property • General question: We have matrices ,we want to find the best order for • Dimension of • One way to find the optimum: Consider the last step. • Suppose: for some .

  7. Algorithm • But what is a best ? • We don’t know… Try all and take the min. • : the min cost of computing • How to solve and ? • Attempt: Same way, i.e. a recursion • Complexity: • Exponential!

  8. Observation: small subproblems are calculated many times! min

  9. What did we observe? • Why not just do it once and store the result for later reference? • When needed later: simply look up the stored result. • That’s dynamic programming. • First compute the small problems and store the answers • Then compute the large problems using the stored results of smaller subproblems.

  10. Now solve the problem this way. min

  11. Algorithm For the first example: : {bestcost(), bestcost(), bestcost()} : {bestcost(), bestcost()} : {bestcost()}. • for to • for to // : step length • for to • return Best cost of Best cost of Cost of , where ,

  12. Complexity • for to • for to // : step length • for to • return • Total: • Much better than the exponential!

  13. Optimal value vs. optimal solution • We’ve seen how to compute the optimal value using dynamic programming. • What if we want an optimal solution? • The order of matrix multiplication.

  14. Problem 2: longest increasing subsequence

  15. Problem 2: longest increasing subsequence • A sequence of numbers • Eg: 5, 2, 8, 6, 3, 6, 9, 7 • A subsequence: a subset of these numbers taken in order • , , …, , where • An increasing subsequence: a subsequence in which the numbers are strictly increasing • Eg: 5, 2, 8, 6, 3, 6, 9, 7 • Problem: Find a longest increasing subsequence.

  16. A good algorithm • Consider the following graph where longest increasing subsequence ↔ longest path

  17. Attempt • Consider the solution. • Suppose it ends at . • The path must come from some edge as the last step. • If we do this recursively • = length of the longest path ending at • Length: # of nodes on the path. • Simple recursion: exponential.

  18. Again… • We observe that subproblems are calculated over and over again. • So we record the answers to them. • And use them for later computation.

  19. Algorithm • for • return • Run this algorithm on the example 5, 2, 8, 6, 3, 6, 9, 7 • What’s ?

  20. Correctness • = length of the longest path ending at • Length here: number of nodes on the path • Any path ending at must go through an edge from some • Where is the best ? • It’s taken care of by the max operation. • By induction, property proved.

  21. Complexity • Obtaining the graph - • for • - • return • Total:

  22. What’s the strategy used? • We break the problem into smaller ones. • We find an order of the problems s.t.easyproblems appear ahead of hard ones. • We solve the problems in the order of their difficulty, and write down answers along the way. • When we need to compute a hard problem, we use the previously stored answers (to the easy problems) to help.

  23. Optimal value vs. optimal solution • We’ve seen how to compute the optimal value using dynamic programming. • The length of the longest increasing subsequence. • What if we want an optimal solution? • A longest increasing subsequence.

  24. More questions to think about • We’ve learned two problems using dynamic programming. • Chain matrix multiplication: solve problem from to • Longest increasing subsequence: solve problem from to . • Questions: Why different? • What happens if we compute chain matrix multiplication by solving problem from to ? • What happens if we compute longest increasing subsequence by solving problem from to ?

  25. In general • Think about whether you can use algorithm methods on problems … • That’ll help you to understand both the algorithms and the problems.

  26. Problem 3: Edut dstamnce

  27. Definition and applications • Edutdstamnce  Edit distance • : the minimal number of single-character edits needed to transform to . • edit: deletion, insertion, substitution • and don’t need to have the same length • Applications: • Misspelling correction • Similarity search (for information retrieval, plagiarism catching, DNA variation) • …

  28. What are subproblems now? • It turns out that the edit distance between prefixes is a good one. • We want to know . Suppose we already know • Express as a function of and comparison of .

  29. Answer • Two cases:

  30. If: • To finally match the last character, we need to do at least one of the following three: • Delete • Delete • Substitute for • Convince yourself that inserting letters after or yj doesn’t help. • It reduces to three subproblems: • Delete : • Delete : • Substitute for : • We pick whichever is the best, so • in case of Each costs 1.

  31. If • delete . Reduces to . • delete . Reduces to . • Don’t delete or . Reduces to . • So in case of • “1”: the cost for the deletion. • Btw, do you think the minimum is always achieved by d1 in this case of ?

  32. Now the algorithm The initialization part corresponds to . (The best way is simply insert one by one.) And similarly . • for • for : • for : for : • return

  33. Running it on (polynomial, exponential)

  34. Complexity • for • for : • for : for : • return • time for each square, so clearly in total.

  35. Optimal value vs. optimal solution • We’ve seen how to compute the optimal value using dynamic programming. • The edit distance. • What if we want an optimal solution? • A short sequence of insert/delete/substitution operations to change to .

  36. Summary of dynamic programming • Break the problem into smaller subproblems. • Subproblems overlap • Some subproblems appear many times in different branches. • Compute subproblems and store the answers. • When later needed to solve these subproblems, just look up the stored answers.

More Related