Intermediate Topic 28 of 51 Algorithms
Edit Distance
Minimum insertions, deletions and substitutions turning one string into another.
How it works
Edit distance, also known as Levenshtein distance, is the minimum number of insertions, deletions and substitutions needed to turn one string into another. A dynamic programming table D[i][j] holds the distance between prefixes, taking the minimum over the three operations plus their costs, and runs in O(mn) time. Variants add transpositions or different costs for each operation.
Where it is used
Spell checkers, autocorrect, fuzzy search, DNA sequence comparison, plagiarism detection and record linkage use edit distance. Search engines suggest corrections with it.
Common pitfall
Initializing the first row and column incorrectly gives wrong answers. It is quadratic, so comparing every pair among many strings is expensive; use indexes or BK-trees. It ignores meaning, so words with different spelling but the same sense have large distance. Normalize case and whitespace first.
Where it fits in the course
Edit Distance is topic 28 of 51 in Algorithms, rated Intermediate. It follows Longest Common Subsequence. The next topic is Backtracking.
Practice and explore
- Open the interactive lesson, with a mini quiz.
- Take the Algorithms quiz.
- Explore the DP Table (LCS) visualizer: Watch the longest common subsequence table fill.
How to study this topic
- Read the explanation above and restate the definition in your own words.
- Open the interactive lesson and answer its mini quiz.
- Read the common pitfall, then write a short example that avoids it.
- Check your understanding in the Algorithms quiz, and revisit the topic if you miss a question.
Frequently asked questions
What is Edit Distance?
Where do people use Edit Distance?
What is the most common mistake with Edit Distance?
What should I learn before this topic?
How can I practice it?
Is Edit Distance hard to learn?
How long does it take to learn Edit Distance?
Which topics are related to Edit Distance?
What should I study after Edit Distance?
Related topics
- Longest Common Subsequence (Algorithms)
- Backtracking (Algorithms)
- Knapsack Problem (Algorithms)
- N-Queens Problem (Algorithms)