Longest Common Subsequence: Difference between revisions

From Rice Wiki
No edit summary
No edit summary
Line 9: Line 9:


Consider <math>X = \langle x_1, x_2, \ldots, x_m \rangle</math> and
Consider <math>X = \langle x_1, x_2, \ldots, x_m \rangle</math> and
<math>Y = \langle y_1, y_2, \ldots, y_n \rangle</math>, and  
<math> Y = \langle y_1, y_2, \ldots, y_n \rangle </math>, and  
let <math>Z = \langle z_1, z_2, \ldots, z_k \rangle</math> be an LCS of
let <math>Z = \langle z_1, z_2, \ldots, z_k \rangle</math> be an LCS of
<math>X</math> and <math>Y</math>.
<math>X</math> and <math>Y</math>.

Revision as of 03:24, 4 March 2024

Problem description

A sequence Z is a subsequence of another sequence X if all the elements in Z also appear in order in X. Notably, the elements do not need to be consecutive.

In the longest-common-subsequence problem (LCS), we wish to find a maximum-length common subsequence of X and Y.

Approach: dynamic programming

Optimal Substructure

Consider and , and let be an LCS of and .

If , then and is an LCS of and .

If and , then is an LCS of and .

If and , then is an LCS of and .