Levenshtein Matrix using only a diagonal strip
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The Levenshtein Distance, also known as the edit distance, is a measure of similarity between two strings. It is defined as the minimum number of single-character edits required to change one string into another. These edits can be insertions, deletions, or substitutions. A comprehensive technique to compute this distance involves using a matrix, commonly referred to as the Levenshtein Matrix. Interestingly, for many applications, leveraging a diagonal strip within the matrix can lead to significant computational improvements.
Levenshtein Matrix Basics
Matrix Representation
The Levenshtein Matrix is constructed such that one string is represented along the rows, and the other along the columns. The element at cell represents the edit distance between the first characters of one string and the first characters of another string.
Traditional Matrix Construction
For strings `A` and `B` with lengths `m` and `n`, the matrix `D` is of size `(m+1) x (n+1)`. The initial row and column are initialized as follows: • The first row: `D[i][0] = i` (converting `i` characters of `A` to an empty string requires `i` deletions). • The first column: `D[0][j] = j` (converting an empty string to `j` characters of `B` requires `j` insertions).
The recursive definition to compute each cell is: where is `0` if the characters are the same, and `1` if they are different.
Diagonal Strip Optimization
Concept
In many practical scenarios, especially when strings `A` and `B` are similar, the edit distance is relatively small compared to their lengths. This similarity means changes cluster around the diagonal of the matrix (i.e., where the number of deletions is approximately equal to the number of insertions). Thus, instead of calculating the entire matrix, we can focus computations within a diagonal strip.
Implementation Details
• Let `k` be the maximum number of allowed differences between the two strings. • Instead of computing all cells, maintain a diagonal strip of width `2*k + 1` around the main diagonal. • Only values within this strip are calculated and updated. This drastically reduces computation for large `m` and `n` with small `k`.
Example
Consider strings `A = "kitten"` and `B = "sitting"`. The traditional approach uses a full `7x8` matrix. However, if we anticipate their difference to be no more than 3 edits, a diagonal strip width of `7` suffices.
| i \ j | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 0 | 0 | 1 | 2 | 3 | - | - | - | - |
| 1 | - | 0 | 1 | 2 | 3 | - | - | - |
| 2 | - | 1 | 0 | 1 | 2 | 3 | - | - |
| 3 | - | - | 1 | 0 | 1 | 2 | 3 | - |
| 4 | - | - | - | 1 | 1 | 1 | 2 | 3 |
• Cells indicated by `-` denote uncomputed values. • Computations focus solely within the indicated range.
Applications and Efficiency
Use Cases
• Spell Checkers: By restricting computations, spell checkers can quickly determine similar suggestions for a misspelled word. • DNA Sequencing: Efficiently calculate similarity between sequences where mutations are minimal. • Plagiarism Detection: Quick identification of partial matching sections in large texts.
Efficiency Gains
• Reduced Complexity: From potentially to , thereby enhancing the run-time efficiency. • Memory Savings: Less memory overhead due to reduced matrix computation, especially important for constrained environments or high-dimensional data.
Conclusion
Using only a diagonal strip within the Levenshtein Matrix, especially when both strings have minimal differences, enables efficient computation of the edit distance. This results in substantial performance enhancements both in terms of execution time and resource consumption, making it invaluable in various domains like text processing, computational biology, and beyond.
Summary Table
Here’s a quick overview of the diagonal strip method:
Related reading
- Lexicographic minimum permutation such that all adjacent letters are distinct
- Lexicographically minimal grid path algorithm
- Library for working with potentially infinite graphs defined by neighbor-list functions
- libsvm Shrinking Heuristics
- Lights Out - finding worst initial state
- Limit number of cores used in Keras
- Lightweight decompression algorithm for embedded use
- Line clipping to arbitary 2D polygon

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 courseTrack 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.