edit distance
python programming
string comparison
algorithm implementation
python tutorial

Edit Distance in Python

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

Introduction

Edit Distance is a fundamental concept in computer science when it comes to measuring how different two strings are from each other. It has wide applications in areas like natural language processing, bioinformatics, and spell correction. In this article, we discuss the concept of edit distance, particularly focusing on the Levenshtein distance, and demonstrate how to compute it using Python.

What is Edit Distance?

Edit distance is a way of quantifying how dissimilar two strings (e.g., words) are. The edit distance between two strings is the minimum number of operations required to transform one string into the other. The operations typically allowed are:

  1. Insertion: Adding a character.
  2. Deletion: Removing a character.
  3. Substitution: Replacing one character with another.

For example, the edit distance between the strings "kitten" and "sitting" is 3, due to the following transformations:

  • kitten → sitten (substitution of 'k' with 's')
  • sitten → sittin (substitution of 'e' with 'i')
  • sittin → sitting (insertion of 'g')

Levenshtein Distance

The Levenshtein distance is perhaps the most commonly used edit distance, where each operation (insertion, deletion, substitution) has a cost of 1. It can be effectively computed using dynamic programming.

Dynamic Programming Approach

The dynamic programming approach to calculate the Levenshtein distance involves filling up a 2D table (matrix) to store the results of subproblems. The table helps in reducing the time complexity from an exponential one (like with recursion) to a polynomial one.

Python Implementation

  • Time Complexity: The time complexity of calculating the Levenshtein distance using dynamic programming is O(m×n)O(m \times n), where m and n are the lengths of the strings.
  • Space Complexity: It requires O(m×n)O(m \times n) space for the DP table. However, this can be optimized to O(min(m,n))O(\min(m, n)) using a single row/column array.
  • Limitations: The method is mainly suited for short string comparisons due to quadratic space requirements. Other specialized algorithms exist for longer texts and specific use cases.

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.