Docsity

Dynamic Programming: Solving Common Algorithmic Problems, Study notes of Computer Science

Toggle sidebar
bg1
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:
1) The whole list
2) The size of the list
3) The index of the current value we are considering
4) The height of the last missile taken previous to the current.
One thing to note is that sometimes, the current missile can not be intercepted. This is
precisely when its height is greater than the height of the previous missle intercepted.
Initially, the last parameter is set to a value greater than the height of any missile.
In the code, if it's possible to intercept the current missile, then take the maximum of:
1) 1 + maximum number of missiles that can be intercepted from the rest of the list such
that the maximum height is that of the current missile.
2) the maximum number of missiles that can be intercepted from the rest of the list such
that the maximum height is that of the previous missile taken.
pf3
pf4
pf5

Partial preview of the text

Download Dynamic Programming: Solving Common Algorithmic Problems and more Study notes Computer Science in PDF only on Docsity!

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:

  1. The whole list
  2. The size of the list
  3. The index of the current value we are considering
  4. The height of the last missile taken previous to the current. One thing to note is that sometimes, the current missile can not be intercepted. This is precisely when its height is greater than the height of the previous missle intercepted. Initially, the last parameter is set to a value greater than the height of any missile. In the code, if it's possible to intercept the current missile, then take the maximum of:
  5. 1 + maximum number of missiles that can be intercepted from the rest of the list such that the maximum height is that of the current missile.
  6. the maximum number of missiles that can be intercepted from the rest of the list such that the maximum height is that of the previous missile taken.

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:

  1. Store the last missile height the corresponds to each of the longest sequences
  2. Stipulate that the entry in the array corresponds to the longest sequence of missiles that can be intercepted with the LAST missile being intercepted. It turns out that the characterization for choice 2 works quite well. Here is the adjusted auxiliary array if we choose to store this information: 0 1 2 3 4 5 6 7 1 2 3 4 3 5 4 3 To finish up the problem, we simply find the maximum value stored in this auxiliary array. Now, how do we construct this auxiliary array? We will fill in each value one by one, in order. When we fill in the kth^ element, we must ask ourselves the following question: Assuming that the last missile intercepted was any of the previous, which of these previous interceptions leads to the maximum number of missile interceptions that end with this current missile?

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:

  1. If either string is empty, return the length of the other string.
  2. If the last characters of both strings match, recursively find the edit distance between each of the strings without that last character.
  3. If they don't match then return 1 + minimum value of the following three choices: a) Recursive call with the string s w/o its last character and the string t b) Recursive call with the string s and the string t w/o its last character c) Recursive call with the string s w/o its last character and the string t w/o its last character. Now, how do we use this to create a DP solution? We simply need to store the answers to all the possible recursive calls. In particular, all the possible recursive calls we are interested in are determining the edit distance between prefixes of s and t. Consider the following example with s="hello" and t="keep". To deal with empty strings, an extra row and column have been added to the chart below: 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 An entry in this table simply holds the edit distance between two prefixes of the two strings. For example, the highlighted square indicates that the edit distance between the strings "he" and "keep" is 3. In order to fill in all the values in this table we will do the following:
  4. Initialize values corresponding to the base case in the recursive solution. These are all the values dealing with edit distances with the empty string. (They are the first row and first column inside the table.)
  5. Loop through the table from the top left to the bottom right. In doing so, simply follow the recursive solution. If the characters you are looking at match, store the number in the square diagonally up and left from the square you are filling in. This square holds the edit distance between the two strings w/o their last character.

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-