



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Programming Team Lecture: Dynamic Programming Standard Algorithms to Know Computing Binomial Coefficients (Brassard 8.1) World Series Problem (Brassard 8.1) Making Change (Brassard 8.2) Knapsack (Brassard 8.4 Goodrich 5.3) Subset Sum (special instance of knapsack where weights=values) Floyd-Warshall's (Brassard 8.5 Cormen 26.2) Chained Matrix Multiplication (Brassard 8.6, Cormen 16.1 Goodrich 5.3) Longest Common Subsequence (Cormen 16.3) Edit Distance (Skiena 11.2) Polygon Triangulation (Cormen 16.4) Example #1: Testing the Catcher If you read this carefully, this problem is really a longest non-increasing sequence problem. The recursive solution is as follows: For the first value in the list, we have two options: (a) take it, (b) don't take it Work out which of these two options is better and return the maximum of the two strategies. Our recursive function takes in four parameters:
Now, the question is, how can we turn this into DP? For each sublist starting from the beginning, perhaps we could store the maximum number of missiles that can be intercepted from that sublist. Thus, for the list: 0 1 2 3 4 5 6 7 80 70 60 50 65 45 60 61 We could store (in an auxiliary array) the values: 0 1 2 3 4 5 6 7 1 2 3 4 4 5 5 5 The key to DP is being able to construct this table just using previously filled in values to the table. The problem here is just because we know that the longest list using the first 7 elements (in indexes 0 through 5) is 5, doesn't mean we can decide whether or not we can do better using the last element, 35. In fact, the information we need is what was the height of the last missile in the list of four intercepted missiles. If this is greater than or equal to 61, then we can add 61 to the list, otherwise we can't. So, we have two options:
Also, in our recursive solution, we must note that the edit distance between the empty string and another string is the length of the second string. (This corresponds to having to insert each letter for the transformation.) So, an outline of our recursive solution is as follows:
If the characters don't match, look that the square to your left, above you, and the square diagonally up and left of the one you are filling in. Take the minimum of these and add 1. This corresponds exactly to the recursive call, except that instead of making it, you just look it up in the table. Finally we can reconstruct the path as follows: h e l l o 0 1 2 3 4 5 k 1 1 2 3 4 5 e 2 2 1 2 3 4 e 3 3 2 2 3 4 p 4 4 3 3 3 4 Start at the bottom right corner of the auxiliary array. Compare the corresponding characters. If they match, automatically go up the diagonal and do not edit anything. If they don't match, as in the case of 'o' and 'p', the look at the three squares we mentioned before: up, left, and up&left. If any of these is one less than the current square value, go to that square. (I chose one of the two possible choices.) Then make the edit that corresponds to this choice. For the choice above, that means deleting the o: hello -> hell From here on, the edit path dictated is hello->hell->help->heep->keep. Homework Problems from acm.uva.es site 10131, 10069, 10154, 116, 10003, 10261, 10271, 10201 The team should attempt to complete these collectively. I will email one more problem to the three team members that all of them should do. References Brassard, Gilles & Bratley, Paul. Fundamentals of Algorithmics (text for COT5405) Prentice Hall, New Jersey 1996 ISBN 0-13-335068- Cormen, Tom, Leiserson, Charles, & Rivest, Ronald. Introduction to Algorithms The MIT Press,Cambridge, MA 1992 ISBN 0-262-03141-8 (a newer version exists) Goodrich, Michael & Tamassia, Roberto. Algorithm Design (text for COP3530) John Wiley & Sons, New York 2002 ISBN 0-471-38365- Skiena, Steven & Revilla, Miguel. Programming Challenges Springer-Verlag, New York 2003 ISBN 0-387-00163-