1. Gyanastra
  2. Algorithms
  3. Longest Common Subsequence
  4. Longest Common Subsequence: DNA Similarity Application
Free lesson, no account needed 6 min read 2 practice questions after it

Algorithms · Longest Common Subsequence

Longest Common Subsequence: DNA Similarity Application

Overview

DNA strands of two species can be compared as strings over the alphabet {A, C, G, T}, and their similarity is measured by the length of their Longest Common Subsequence (LCS).
The longer the common subsequence, the more similar the two species; the same measure identifies the closest pair among several DNAs.

Key concepts

  • DNA as a string: A DNA strand is a string made of four molecules (bases): A, C, G, T. The names are not important — only that there are four symbols.
    A real DNA repeats over and over, so in practice only a significant part is taken; in problems the full DNA string is given.
  • Longest Common Subsequence (LCS): If a string of length nn has 2n2^n subsequences and another string of length mm has 2m2^m subsequences, the LCS is a subsequence present in both strings that is the longest such subsequence.
  • Similarity measure: The similarity of two DNAs depends on how long their common subsequence is. The longer the LCS, the more similar the two DNAs (species) are.
  • Subsequence vs substring: A subsequence keeps the order of characters but need not be contiguous; a substring is a contiguous part of the string.
  • Empty string (Phi): The empty string is a common subsequence of every pair of strings.

How it works

Deciding similarity between two DNAs, in order of cases:

  1. Both DNAs are equal → they are 100% similar (assume they come from the same parent).
  2. One DNA is a substring of the other (one is a part of the other, in either direction) → they are very much similar.
  3. Neither is a substring of the other → measure similarity by the length of the longest common subsequence.

General procedure to measure similarity of two strings/DNAs:

  1. Take one DNA and the other DNA.
  2. Find all common subsequences of the two.
  3. Among all common subsequences, identify the longest one.
  4. The length of that longest common subsequence is the similarity number — the longer the common subsequence, the more similar the strings.

A common subsequence already preserves the relative order of characters in both strings; so after listing the common subsequences, take the one of maximum length and use its length as the similarity.

Finding which two of several DNAs (e.g., D1, D2, D3) are closest:

  1. Take every pair of DNAs.
  2. For each pair, find all their common subsequences.
  3. For each pair, take the maximum length among those common subsequences (the LCS length).
  4. The pair with the largest LCS length is the most similar pair.

Formulas

  • Number of subsequences of a string of length nn: 2n2^n
  • Number of subsequences of a string of length mm: 2m2^m
  • Similarity of two strings = length of their LCS

Worked examples

Example 1 — Three DNAs, which pair is closest

PairLongest common subsequence foundLCS length
D1, D2ACG3
D2, D3ACGT4
D1, D3AAGT5

D1 and D3 have the longest common subsequence (length 5), so D1 and D3 are the most similar pair compared to any other pair.

Example 2 — Two ordinary strings

Two strings are given (not DNA). Checking all common subsequences:

Common subsequencePresent in string 1?Present in string 2?
A, Ayesyes
Ayesyes
Phi (empty string)yesyes (empty subsequence is common to everything)

The strings do not contain J and do not contain Y, so no common subsequence can use J or Y. The only common subsequences are AA, A, and Phi. Among {AA, A, Phi} the longest is AA, so the LCS length = 2, i.e., the similarity of these two strings is 2.

Comparison

Situation between two DNAsSimilarity conclusion
Both DNAs equal100% similar (same parent)
One DNA is a substring of the otherVery much similar
Neither is a substring of the otherSimilarity = length of the longest common subsequence (larger length → more similar)

Revision summary

  • A DNA strand is a string over the alphabet {A, C, G, T}; the base names themselves are irrelevant.
  • Equality → 100% similarity; substring relation → very high similarity; otherwise use LCS length.
  • LCS = a subsequence common to both strings with maximum length;
    a string of length nn has 2n2^n subsequences, so brute-force enumeration is exponential.
  • The length of the LCS is the numerical measure of similarity — longer LCS means more similar species.
  • The empty string (Phi) is a common subsequence of every pair of strings.
  • To find the closest pair among several DNAs, compute the LCS length for every pair and pick the pair with the maximum LCS length (e.g., lengths 3, 4, 5 → the pair with 5 is closest).
  • A subsequence keeps order but need not be contiguous; a substring is contiguous.

Previous-year exam insights

  • LCS satisfies the optimal-substructure recurrence: if the last characters match, LCS(X[1..m],Y[1..n]) = 1 + LCS(X[1..m-1],Y[1..n-1]);
    otherwise it is the maximum of LCS(X[1..m-1],Y[1..n]) and LCS(X[1..m],Y[1..n-1]);
    the base case is 0 when either string is empty.
  • The LCS length of strings of lengths m and n is computed by dynamic programming in O(mn) time and O(mn) space (reducible to O(min⁡(m,n))O(\min(m,n)) space), whereas brute-force enumeration of all subsequences is exponential.
  • The LCS length is at most min(m,n) and at least 0; it equals min(m,n) exactly when the shorter string is a subsequence of the longer one (in particular when one is a substring of the other).
  • A string of length n has 2n2^{n} subsequences by index choice (including the empty one) but at most n(n+1)/2 + 1 distinct substrings counting the empty one (n(n+1)/2 non-empty, with equality when all characters are distinct); the number of distinct subsequences, and of distinct substrings, can be smaller when characters repeat.
  • LCS is not the same as the Longest Common Substring: the substring must be contiguous, and its length can be strictly smaller than the LCS length; the longest common substring is also found by DP in O(mn).
  • The LCS of two strings is not unique: several distinct common subsequences can all have the maximum length, so the LCS length (not a particular subsequence) is the well-defined similarity measure.
Pages 1–2 of 2