Levenshtein Distance
Edit Operations
String Matching
Dynamic Programming
Computational Linguistics

Levenshtein Distance Inferring the edit operations from the matrix

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Levenshtein Distance is a popular metric used in the field of computer science and information theory for measuring the difference between two sequences. Named after Vladimir Levenshtein, who introduced this concept in 1965, it is often used in applications like spell checking, DNA sequencing, and natural language processing to quantify how dissimilar two strings are by counting the minimum number of operations required to transform one string into another.

Understanding Levenshtein Distance

Levenshtein Distance is defined as the smallest number of single-character edits (insertions, deletions, or substitutions) needed to change one word into the other. For example, transforming "kitten" into "sitting" can be achieved with three operations:

  1. Substitute 'k' with 's': "kitten" -> "sitten"
  2. Substitute 'e' with 'i': "sitten" -> "sittin"
  3. Insert 'g': "sittin" -> "sitting"

Therefore, the Levenshtein distance between the words "kitten" and "sitting" is 3.

Constructing the Levenshtein Matrix

To compute the Levenshtein distance, a two-dimensional matrix is used. Suppose we want to transform string A into string B . We initialize a matrix D , where D[i][j] represents the minimum number of operations required to transform the first i characters of A into the first j characters of B .

Steps to Fill the Matrix

  1. Initialization:
    • D[i][0] = i for all i , since transforming any prefix of A into an empty string requires i deletions.
    • D[0][j] = j for all j , since transforming an empty string into any prefix of B requires j insertions.
  2. Filling the Matrix:
    • For each cell D[i][j] , set:
  • The matrix shows the operations converted to "yabd" from "abc".
  • D[3][4] is 2, indicating the shortest path consists of two operations.
    • Move to D[i-1][j] for deletion.
    • Move to D[i][j-1] for insertion.
    • Move to D[i-1][j-1] for substitution (or no operation if the characters matched).
    • Comparing a potentially misspelled word against a dictionary finds the closest match using Levenshtein Distance.
    • By comparing DNA sequences, researchers can infer evolutionary distances and genetic similarities.
    • Analyzing the difference between phrases or sentences in language processing and translation tasks.
    • Validating string inputs where typos might occur, considering corrections with minimal changes.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.