I was going through the longest common sub sequence problem in the INTRODUCTION TO ALGORITHMS by CLRS.But i was unable to understand the thought process behind it. Most other problems explained were pretty intuitive and am confident that i can solve similar problems in the future but i am unable to visualize the LCS problem as i do not understand the logic behind how the optimal substructure is determined.
the book gives the following explanation for the optimal sub structure
**Theorem 15.1 (Optimal substructure of an LCS)
Let X == and Y == be sequences, and let Z =
be any LCS of X and Y.
1. If xm = yn,then zk = xm = yn and Zk−1 is an LCS of Xm−1 and Yn−1.
2. If xm != yn ,then zk = xm implies that Z is an LCS of Xm−1 and Y.
3. If xm != yn,then zk = yn implies that Z is an LCS of X and Yn−1.
Proof (1) If zk != xm, then we could append xm = yn to Z to obtain a common
subsequence of X and Y of length k + 1, contradicting the supposition that Z is
a longest common subsequence of X and Y. Thus, we must have zk = xm = yn.
Now, the prefix Zk−1 is a length-(k − 1) common subsequence of Xm−1 and Yn−1.
We wish to show that it is an LCS. Suppose for the purpose of contradiction that
there is a common subsequence W of Xm−1 and Yn−1 with length greater than k−1.
Then, appending xm = yn to W produces a common subsequence of X and Y
whose length is greater than k, which is a contradiction.
(2) If zk != xm,then Z is a common subsequence of Xm−1 and Y. If there were a
common subsequence W of Xm−1 and Y with length greater than k,then W would
also be a common subsequence of Xm and Y, contradicting the assumption that Z
is an LCS of X and Y.
(3) The proof is symmetric to (2).**
The proof is clear but i can’t see how they came up with the optimal substructure.
Can someone help?
Please go through the recurrence part of the the following link:
These recurrence relations are nothing but optimal substructure and overlapping sub-problem equalities.