



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!




We have looked at several algorithms that involve recursion. In some situations, these algorithms solve fairly difficult problems efficiently, but in other cases they are inefficient because they recalculate certain function values many times. The example given in the text is the fibonacci example. Recursively we have:
public static int fibrec(int n) {
if (n < 2) return n; else return fibrec(n-1)+fibrec(n-2); }
The problem here is that lots and lots of calls to Fib(1) and Fib(0) are made. It would be nice if we only made those method calls once, then simply used those values as necessary. In fact, if I asked you to compute the 10th Fibonacci number, you would never do it using the recursive steps above. Instead, you'd start making a chart:
First you calculate F 3 by adding F 1 and F 2 , then F 4 , by adding F 3 and F 4 , etc.
The idea of dynamic programming is to avoid making redundant method calls. Instead, one should store the answers to all necessary method calls in memory and simply look these up as necessary.
Using this idea, we can code up a dynamic programming solution to the Fibonacci number question that is far more efficient than the recursive version:
public static int fib(int n) {
int[] fibnumbers = new int[n+1]; fibnumbers[0] = 0; fibnumbers[1] = 1;
for (int i=2; i<n+1;i++) fibnumbers[i] = fibnumbers[i-1]+fibnumbers[i-2];
return fibnumbers[n]; }
The only requirement this program has that the recursive one doesn't is the space requirement of an entire array of values. (But, if you think about it carefully, at a particular moment in time while the recursive program is running, it has at least n recursive calls in the middle of execution all at once. The amount of memory necessary to simultaneously keep track of each of these is in fact at least as much as the memory the array we are using above needs.)
Usually however, a dynamic programming algorithm presents a time-space trade off. More space is used to store values, but less time is spent because these values can be looked up.
Can we do even better (with respect to memory) with our Fibonacci method above? What numbers do we really have to keep track of all the time?
The problem is to find the longest common subsequence in two given strings. A subsequence of a string is simply some subset of the letters in the whole string in the order they appear in the string. In order to denote a subsequence, you could simply denote each array index of the string you wanted to include in the subsequence. For example, given the string "GOODMORNING", the subsequence that corresponds to array indexes 1, 3, 5, and 6 is "ODOR."
Here is the basic idea behind solving the problem:
If the initial characters of both strings s1 and s2 match, then the LCS will be one plus the LCS of both of the rest of the strings.
If the initial characters of both strings do NOT match, then the LCS will be one of two options:
Thus, in this case we will simply take the maximum of these two values. Let's examine the code for both the recursive solution to LCS and the dynamic programming solution:
// Arup Guha // 9/26/
// The method below solves the longest common subsequence // problem recursively. import java.io.*;
public class LCS {
// Precondition: Both x and y are non-empty strings. public static int lcsrec(String x, String y) {
// If one of the strings has one character, search for that // character in the other string and return the appropriate // answer. if (x.length() == 1) return find(x.charAt(0), y); if (y.length() == 1) return find(y.charAt(0), x);
// Solve the problem recursively.
// Corresponding characters match. if (x.charAt(0) == y.charAt(0)) return 1+lcsrec(x.substring(1), y.substring(1));
// Corresponding characters do not match. else return max(lcsrec(x,y.substring(1)), lcsrec(x.substring(1),y));