LearnViz CompSci
Home / Learn / Algorithms / Edit Distance

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

How to study this topic

  1. Read the explanation above and restate the definition in your own words.
  2. Open the interactive lesson and answer its mini quiz.
  3. Read the common pitfall, then write a short example that avoids it.
  4. Check your understanding in the Algorithms quiz, and revisit the topic if you miss a question.

Frequently asked questions

What is Edit Distance?
Minimum insertions, deletions and substitutions turning one string into another. It belongs to Algorithms and is an intermediate-level topic.
Where do people use Edit Distance?
Spell checkers, autocorrect, fuzzy search, DNA sequence comparison, plagiarism detection and record linkage use edit distance.
What is the most common mistake with Edit Distance?
Initializing the first row and column incorrectly gives wrong answers.
What should I learn before this topic?
Start with Longest Common Subsequence, the topic just before it in Algorithms.
How can I practice it?
Answer the mini quiz in the interactive lesson, then take the Algorithms quiz. You can also explore DP Table (LCS) in the Visual Lab.
Is Edit Distance hard to learn?
It is an Intermediate topic. Be comfortable with the Beginner topics in this area first, then read the common pitfall carefully, since it is where most mistakes happen.
How long does it take to learn Edit Distance?
Most learners can read the explanation and finish the interactive lesson in under an hour. Fluency comes from applying the idea, so plan a few short practice sessions with the quiz.
Which topics are related to Edit Distance?
Longest Common Subsequence, Backtracking, Knapsack Problem are closely connected. The full list is in the related topics section of this page.
What should I study after Edit Distance?
Continue with Backtracking, the next topic in Algorithms.

Related topics