Skip to content

Longest common subsequence

planning ahead

Level 2 of 3

Also called LCS and Longest common subsequence, by dynamic programming.

test yourself

Six questions, four to pick from and two for paper.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

How many cells does the method fill to compare stone and notes?

the answer, and why

the answer is C

5 rows of 5 cells, a row for each letter of stone. The first row and column are written at the start.

if you chose another

  • That counts the first row and column too, which are written at the start, not filled.
  • That is the longest common subsequence, such as t and e.
  • That adds the letters, but each row fills a cell for each letter of notes.

question 2

How many letters do lemon and melon share in the same order, at most?

the answer, and why

the answer is A

Such as l, o and n, or e, o and n. Could they share 4? Leave one letter out of each word: lemon gives emon, lmon, leon, lemn and lemo, and melon gives elon, mlon, meon, meln and melo. None match.

if you chose another

  • They have the same 5 letters, but not in the same order.
  • That is o and n, the longest run side by side. The letters may have gaps, such as e, o and n.
  • 0 is only for words with no letter in common.

question 3

Which pair makes the method fill the most cells?

the answer, and why

the answer is D

5 times 5, which is 25.

if you chose another

  • 3 times 4, which is 12.
  • Equal words still fill every cell, but 4 times 4 is 16.
  • 1 times 6, which is 6.

question 4

The last letters of a cell's two starts differ. What does the method write in the cell?

the answer, and why

the answer is A

Shared letters cannot use both last letters, so the cell leaves one out and keeps the larger. Question 4 of the learn page's section 4 shows that this is the most.

this tests how it works, on the learn page

if you chose another

  • That is the rule for two letters that match.
  • The letters before them may still be shared.
  • That leaves out both letters, and may miss a shared one.

pen and paper

question 5about 8 minutes

Run the method by hand on stop and tops.

  1. Fill the table.
  2. How many letters do they share in order, and how many cells did you fill?
  3. Read back the shared letters.
a hint

The first row and column hold 0.

a worked answer

The table is drawn below. The last cell says 3 letters, and you fill 16 cells, 4 rows of 4. Reading back: left from the last cell, then up and to the left three times, taking p, o and t. Read in order, that is t, o and p.

start-ttotoptops
-00000
s00001
st01111
sto01222
stop01233
Stop and tops. The last cell holds 3.

this tests how it works, on the learn page

what a full answer includes
  • The table as drawn.
  • 3 letters and 16 cells.
  • The letters t, o and p.

question 6about 6 minutes

A diff tool compares two files of 20,000 lines each with this method, each line taken as one letter.

  1. How many cells does it fill?
  2. How long does that take at a billion cells a second?
  3. How many numbers does the table hold, and how many do two rows hold?
a hint

Questions 1 to 3 of the learn page's section 4 count all three.

a worked answer

20,000 times 20,000, which is 400,000,000 cells, about 0.4 seconds. The table holds 20,001 times 20,001, which is 400,040,001 numbers, but two rows hold only 40,002.

Question 1: how many cells, CC, does the method fill on two words of nn letters, from 1 up?
  1. the move: a row for each letter

    the line:
    m letters and n letters:m×n cells\begin{gathered} m \text{ letters and } n \text{ letters:} \\ m \times n \text{ cells} \end{gathered}
    order-rraram
    -givengivengivengiven
    agiven123
    argiven456
    armgiven789
    Arm and ram, each cell numbered in the order it is filled, row by row. Given cells are written first, not counted. 3 rows of 3, 9 cells.

    why it is allowed: Each letter of the first word has a row, and each row fills one cell for each letter of the second. Crate and cart fill 5 times 4, 20 cells.

  2. the move: both of n letters

    the line:
    C=n×nC = n \times n

    why it is allowed: Here the first word has nn letters too, so mm is nn. For 3, 3 times 3, which is 9.

  3. the move: write it shorter

    the line:
    C=n2C = n^2

    why it is allowed: nn times nn is written n2n^2, read nn squared. For 3, 9.

  4. the move: a ceiling

    the line:
    C≤n2C \le n^2

    why it is allowed: By line 3, as a number is at most itself.

  5. the move: read the bound

    the line:
    C∈O(n2)C \in O(n^2)

    why it is allowed: O(n2)O(n^2) means at most a fixed multiple of n2n^2, from some nn on, and ∈\in reads is in. By line 4, CC is at most 1 times n2n^2, from 1 up.

  6. the move: a floor

    the line:
    C≥n2C \ge n^2

    why it is allowed: By line 3, as a number is at least itself.

  7. the move: read the bound

    the line:
    C∈Ω(n2)C \in \Omega(n^2)

    why it is allowed: Ω(n2)\Omega(n^2) means at least a fixed multiple of n2n^2, above 0, from some nn on. By line 6, CC is at least 1 times n2n^2, from 1 up.

  8. the move: both together

    the line:
    C∈Θ(n2)C \in \Theta(n^2)

    why it is allowed: Lines 5 and 7 bound CC by the same growth. Θ(n2)\Theta(n^2) means both.

try it with a small nn: at n=3n = 3, line 2 gives C=9C = 9, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • 400,000,000 cells.
  • About 0.4 seconds.
  • 400,040,001 numbers, against 40,002.