I'm going over notes that discuss dynamic programming in the context of finding the longest common subsequence of two equal-length strings. The algorithm in question outputs the length (not the substring).
So I have two strings, say:
S = ABAZDC, T = BACBAD
Longest common subsequence is ABAD (substrings don't have to be adjacent letters)
Algorithm is as follows, where LCS[i, j] denotes longest common subsequence of S[1..i] and T[1..j]:
if S[i] != T[j] then
LCS[i, j] = max(LCS[i - 1, j], LCS[i, j - 1])
else
LCS[i, j] = 1 + LCS[i - 1, j - 1]
My notes claim you can fill out a table where each string is written along an axis. Something like:
B A C B A D
A 0 1 1 1 1 1
B 1 1 1 2 2 2
A ...
Z
D
C
Two questions:
1) How do we actually start filling this table out. Algorithm is recursive but doesn't seem to provide a base case (otherwise I'd just call LCS[5, 5])? Notes claim you can do two simple loops with i and j and fill out each spot in constant time...
2) How could we modify the algorithm so the longest common subsequence would be of adjacent letters? My thought is that I'd have to reset the length of the current subsequence to 0 once I find that the next letter in S doesn't match the next letter in T. But it's tricky because I want to keep track of the longest seen thus far (It's possible the first subsequence I see is the longest one). So maybe I'd have an extra argument, longestThusFar, that is 0 when we call our algorithm initially and changes in subsequent calls.
Can someone make this a bit more rigorous?
Thanks!