| 65 | | Now, to understand the potential cost of this it is clear that we go through Y at least n times (where n is the number of items in X). So that is O(n*m) (m is the number of items in Y). However, at each point we do not match, we could potentially rewind multiple times. Doing a lot of testing with no with sequences like X={A,B,C,D} and Y={D} I saw some extra looping which I stopped with some if statements but these cost time. In any case, what you see is the final one and it still will cost more comparisons than the next one.
|
| | 65 | Now, to understand the potential cost of this it is clear that we go through Y at least n times (where n is the number of items in X). So that is O(n*m) (m is the number of items in Y). However, at each point we do not match, we could potentially rewind multiple times. Doing a lot of testing with no with sequences like X={A,B,C,D} and Y={D} I saw some extra looping which I stopped with some if statements but these cost time. In any case, what you see is the final version and it still will cost more comparisons than the ideal LCS algorithm.
|
| 67 | | What if we take every subsequence of X and look for it in Y? Then we have the longest subsequence, X itself, then start knocking off one character at a time and check again. This truly brute force algorithm is provided in ``:snippet:`380```
|
| | 67 | What if we take every subsequence of X and look for it in Y? Then we have the longest subsequence, X itself, then start knocking off one character at a time and check again. This truly brute force algorithm is provided in ``:snippet:`380```.
|
| | 68 |
|
| | 69 | A critical insight into this problem is to realize that it can be broken down into sub-problems. Let us define a suffix as the first part of a sequence. For example, above suffixes would be A, AB, ABC, ABCD & etc.
|
| | 70 |
|
| | 71 | Now, suppose two sequences start with the same element. Then to find their LCS we need to only compare the remainder. In other words, if the first item matches then a LCS is the item that matched plus a LCS of X = {:math:`x_2, x_3, .., x_i, .., x_n`} with Y = {:math:`y_2, y_3, .., y_j, .., y_m`}.
|
| | 72 |
|
| | 73 | If the first element does not match, then obviously it is not part of an LCS and can be ignored. But then how do we proceed? We must check X = {:math:`x_2, x_3, .., x_i, .., x_n`} against the Y = {:math:`y_3, y_4, .., y_j, .., y_m`} and vice-versa to see which comparison yields a longer LCS.
|
| | 74 |
|
| | 75 | This solution is analogous to the description in the `Wikipedia`_ reference except that there it is more formally defined and he is removing the last element instead of the first.
|
| | 76 |
|
| | 77 | My version leads to a recursive solution like ``:snippet:`381```.
|