3.7.12 · D4Algorithm Paradigms

Exercises — DP problems — edit distance (Levenshtein)

2,295 words10 min readBack to topic

Throughout, remember the single object we are filling:

And the one rule that fills it:

Figure — DP problems — edit distance (Levenshtein)

The picture above is the mental compass for the whole page: ↖ diagonal compares two letters, ↑ up means a delete from , ← left means an insert into . Keep glancing at it.


Level 1 — Recognition

(Can you spot the right piece of machinery?)

L1.1

State for any two strings, and say in words why.

Recall Solution

. The row means "string contributes zero letters" — it is empty. To build the first 4 letters of out of nothing, the only move that adds letters is insert, and you need exactly 4 of them. No cheaper route exists, so it is a base case, not a computed cell.

L1.2

Given 'k' and 'k', which of the three neighbours does copy, and is there a ?

Recall Solution

The letters match, so : the pure diagonal, with no . Matching a letter is free — you neither insert, delete, nor substitute at this position.

L1.3

Name the operation each neighbour encodes:

  • (the cell directly above)
  • (the cell directly to the left)
  • (the diagonal, when letters differ)
Recall Solution
  • delete a letter from (we shortened 's prefix by one, kept 's).
  • insert a letter into (we shortened 's prefix, meaning needed that extra letter).
  • (differing letters) → substitute (we shortened both prefixes and paid to swap the letter).

Level 2 — Application

(Turn the crank correctly.)

L2.1

Compute by reasoning about the last letters (no full table needed).

Recall Solution

Align right-to-left. 't'='t' → free. 'a' vs 'u' → differ, one substitute (+1). 'c'='c' → free. Total .

L2.2

Fill the full table for and give the answer.

Recall Solution

abc (rows), yabd (cols).

"" y a b d
"" 0 1 2 3 4
a 1 1 1 2 3
b 2 2 2 1 2
c 3 3 3 2 2

Bottom-right . Path: insert y at the front (+1), then a,b match for free, then substitute c→d (+1). One insert + one substitute = 2.

L2.3

Compute .

Recall Solution

Every letter is 'a', so no substitutions are ever needed — matches carry the cost straight down the diagonal. The only difference is length: has two extra 'a's. Delete both. (This equals the length difference here precisely because no letter ever needs changing.)


Level 3 — Analysis

(Explain why a cell holds its value.)

L3.1

In the table for (from the parent note), came from . Which operation won, and what does choosing it mean physically?

Recall Solution

The winning neighbour is (the cell directly above). "Above" encodes a delete from : we delete the trailing 'e' of horse. So the last move on the optimal path is delete e, and the remaining cost 2 fixes horsros. The other two options (substitute via , insert via ) were strictly more expensive.

L3.2

Prove that for any strings (edit distance is symmetric).

Recall Solution

Every operation has an exact inverse of equal cost: an insert of a letter into is undone by a delete of that letter (and vice-versa); a substitute x→y is undone by the substitute y→x. So any editing script that turns into in moves can be reversed move-by-move to turn into in the same moves. Hence any cost achievable one way is achievable the other, and the minimums are equal: .

L3.3

Give the tightest lower bound on using only the lengths , , and explain why it holds.

Recall Solution

. Reason: only insert and delete change a string's length, and each changes it by exactly 1. Substitute leaves length untouched. To go from length to length you therefore need at least length-changing moves. So the difference in lengths is a floor, never exceeded downward — but it is only a lower bound, because substitutions add cost without changing length. For horse(5)→ros(3): bound is , yet true answer is 3.


Level 4 — Synthesis

(Combine and adapt the machinery.)

L4.1

Suppose substitution is forbidden (only insert and delete allowed, each cost 1). Write the new recurrence and compute the new distance for .

Recall Solution

Removing substitution deletes the "substitute" candidate from the min. The recurrence becomes:

This is exactly the delete-both-then-reinsert cost, and it is tied to the Longest Common Subsequence: distance . For catcut: LCS is ct of length 2, so distance . (One delete of a, one insert of u — versus a single substitute if it were allowed.)

L4.2

Now let substitution cost 2 while insert and delete each cost 1. Recompute and decide which strategy wins.

Recall Solution

Replace the substitute term's with :

For the single mismatch a vs u: substitute costs 2, but delete-a + insert-u costs as well — a tie at . The general rule: when substitution costs , delete+insert is always at least as good, so substitution never strictly wins.

L4.3

Using Space Optimization in DP, compute keeping only two rows (previous and current). Show both rows at each step and give the answer.

Recall Solution

ab (rows), ba (cols). Row 0 (base) is .

Compute row 1 (A[0]='a'), reading only row 0:

  • (base column).
  • , 'a' vs 'b' differ → .
  • , 'a' vs 'a' match → diagonal .

Row 1 . Discard row 0, keep row 1 as "previous".

Compute row 2 (A[1]='b'), reading only row 1:

  • (base column).
  • , 'b' vs 'b' match → diagonal prev.
  • , 'b' vs 'a' differ → .

Row 2 . Answer (swap abba costs two edits: substitute both, or delete+insert). We never needed the full grid — two rows sufficed because the recurrence only ever reads row .


Level 5 — Mastery

(Invent, generalise, and defend.)

L5.1

A colleague claims: "If and share no letters at all, then ." Prove or disprove.

Recall Solution

True. With no shared letters, no diagonal match is ever free. The cheapest way to reconcile the overlapping positions is to substitute them (cost 1 each) for the first positions, then insert or delete the remaining leftover letters (cost 1 each). Total . No route beats this: you must account for all positions, and each costs at least 1 since nothing matches. So .

L5.2

Design a verification test: for strings of the form "x" (the letter x repeated times) and "y", predict with a closed formula and justify.

Recall Solution

Every letter differs (x vs y), lengths are equal, so no length change is needed and each of the aligned positions needs one substitute. Closed form: . Check : "xxx""yyy" needs 3 substitutions . This makes a clean regression test because the answer scales exactly linearly with .

L5.3

Reconstruct the actual edit script (the list of moves) for from L2.2 by walking backwards from the bottom-right cell. Explain the traceback rule.

Recall Solution

Traceback rule: at each cell, look at which neighbour produced it.

  • Start (c vs d, differ). It equals via the diagonal → substitute c→d. Move to .
  • (b vs b, match) → came from diagonal , no move. Move to .
  • (a vs a, match) → diagonal , no move. Move to .
  • is on row 0 → we still have a y in to account for → insert y. Reach . Done.

Reading forwards: insert y, keep a, keep b, substitute c→d → exactly 2 edits, matching the table. This traceback is why storing the whole grid (rather than one row) is worth it when you need the script, not just the number.


Recall

Recall Quick self-check

with all costs 1 ::: 1 ::: 2 ::: 2 Insert-and-delete-only distance of cat,cut ::: 2 (equals ) Lower bound on from lengths ::: ::: 3 Distance when strings share no letters :::


Connections

  • Parent topic — the full derivation these exercises drill.
  • Longest Common Subsequence — the insert/delete-only variant (L4.1) is LCS in disguise.
  • Space Optimization in DP — the two-row trick of L4.3.
  • Dynamic Programming, Optimal Substructure, Overlapping Subproblems, Recursion and Memoization, Sequence Alignment — the paradigm and its neighbours.