Abstract
Given two words (strings), how can we define a notion of “closeness”?
Definition We can keep track of how many "changes" we need to change one word into another. The changes can be:
- insertion
- deletion
- substitution
For example, lining up the words PELICAN and OSTRICH:
this alignment uses 7 changes, but it is not the cheapest.
- Category: Dynamic Programming / String Processing
- Input: Two strings and
- Output: The minimum number of edits (insert/delete/substitute) to transform into
- Paradigm: Dynamic Programming — equivalently, shortest path in a DAG (see below)
- Typical use cases: spell checking/correction, diff tools, DNA sequence alignment, fuzzy string matching
Problem Specification
- Instance: Two strings , (WLOG ).
- Solution Format: A sequence of edit operations (insert, delete, substitute) that transforms into .
- Constraints: The sequence must actually transform into exactly.
- Objective: The number of edit operations used.
- Goal: Minimize.
Candidate Strategies / Approaches
Brute Force ✘
Try all possible alignments/combinations and find the minimum cost among them. A lower bound on how many combinations exist: each of (at least) the first columns of an alignment table could independently be one of three things (delete, insert, or substitute/match) — so there are at least different combinations. Exponential.
Dynamic Programming ✔
Define = the edit distance to transform into . Solve smallest prefixes first, reusing each answer.
Dynamic Programming Solution
1. Define Subproblems
Let be the edit distance to transform into (the minimum number of changes).
2. Base Cases
When the first word is empty, the edit distance is the length of the second word; when the second word is empty, it’s the length of the first word:
3. Express Recursively
What does the last column of the alignment table look like? Three cases:
Case 1 — Delete :
| ― |
Case 2 — Insert :
| ― | |
|---|---|
Case 3 — Substitute (or match, if equal):
Since we don’t know which case is cheapest, take the minimum of all three.
4. Ordering
To calculate , we need , , and — all already computed if we visit cells left to right through rows, top to bottom.

5. Iterative Algorithm
Algorithm 17 Edit Distance
procedure EditDist()
for from to do
for from to do
for from to do
for from to do
if then
if then
return
6. Final Output

Variables & Data Structures
| Name | Type | Purpose |
|---|---|---|
E | 2D array, | E[i][j] = edit distance between and |
Helper Functions / Operations Used
- Character comparison
x[i] == y[j]— .
Edit Distance as a DAG
This table can be viewed as a DAG:

This graph has vertices and edges (each cell has up to 3 incoming edges — from the delete, insert, and substitute/match cases above). The goal becomes: find the length of the shortest path from the top-left corner to the bottom-right corner .
We could use Dijkstra’s Algorithm for a runtime of:
But there’s a faster way, using the fact that this graph is specifically a DAG — see Shortest Path in a DAG Example.
Why This Connection Matters
The DP recurrence above is a shortest-path-in-a-DAG algorithm, just described in array terms instead of graph terms: filling
Erow by row, left to right is exactly a topological order of this DAG, and each cell’sminover three incoming edges is exactly the DAG shortest-path relaxation step. That’s why the DP solution’s own runtime already beats Dijkstra’s — it’s implicitly using the DAG structure (no comparisons/priority queue needed) rather than Dijkstra’s general-graph machinery.
Proof of Correctness / Optimality
Claim: equals the true minimum edit distance between and .
- Base cases: (transform empty string to by insertions) and (transform to empty by deletions) are both correct by inspection — there’s no cheaper way to create or destroy characters than single-character operations.
- Inductive Hypothesis: every cell visited before in the row-by-row, top-to-bottom order — in particular , , — is correct.
- Inductive Step: the last operation in any optimal transformation of into must be one of exactly three things: delete , insert , or substitute/match with . Each case’s cost is (or for a free match) plus the cost of optimally solving the remaining smaller prefix problem — which is correct by the Inductive Hypothesis. Since takes the minimum over exactly these three cases, and every valid transformation’s last step falls into one of them, is the true minimum.
Time & Space Complexity Analysis
General Case
| Complexity | Notes | |
|---|---|---|
| Time | One computation per cell, cells | |
| Space | The full table; reducible to if only the distance value is needed (keep just the current and previous row), at the cost of losing the ability to reconstruct the actual edit sequence |
Best / Worst / Average Case
- Best / Worst / Average case: all — every cell is filled regardless of how similar or different the two strings are.
Drawbacks / Constraints
- Doesn’t directly output the edit sequence, only its length — recovering the actual operations (like String Reconstruction’s
prevpointers) requires tracing back through the table from to , following whichever case achieved the minimum at each step. - space can be heavy for very long strings if the full table is kept; see the space-reduction note above when only the distance value is needed.
- All operations cost the same (1 each) here. A weighted variant (e.g. substitutions costing more than insertions, or cost depending on which characters are involved) is a natural extension — same recurrence shape, different constants per case.
