Program Explanation
The minimum edit distance between two strings is defined as the minimum number of operations required
to transform one string into another. The allowable operations are insertion, deletion, and substitution
of a single character. This program uses dynamic programming to compute the edit distance efficiently by
constructing a table that stores the edit distances for substrings of the two input strings.
Program Structure
- Function Definition: The main function is
minEditDistance(s1, s2 string) int
,
which takes two strings as input and returns the minimum edit distance between them. - Dynamic Programming Table: A 2D slice
dp
is created to store the edit distances
for different prefixes of the two strings. - Logic: Nested loops iterate through the characters of both strings:
- The first row and column of the DP table are initialized to represent the distance from the
empty string to the prefixes of the other string. - If characters match, the value is taken from the diagonal cell. If they don’t match, the program
calculates the minimum cost from insertion, deletion, and substitution.
- The first row and column of the DP table are initialized to represent the distance from the
- Result: The minimum edit distance is found in the bottom-right cell of the DP table.
Go Code
package main
import (
"fmt"
)
// minEditDistance calculates the minimum edit distance between two strings.
func minEditDistance(s1, s2 string) int {
m := len(s1)
n := len(s2)
// Create a DP table to store edit distances
dp := make([][]int, m+1)
for i := range dp {
dp[i] = make([]int, n+1)
}
// Initialize the DP table
for i := 0; i <= m; i++ {
dp[i][0] = i // Deleting all characters from s1
}
for j := 0; j <= n; j++ {
dp[0][j] = j // Inserting all characters of s2
}
// Fill the DP table
for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ {
if s1[i-1] == s2[j-1] {
dp[i][j] = dp[i-1][j-1] // No operation needed
} else {
dp[i][j] = min(
dp[i-1][j]+1, // Deletion
dp[i][j-1]+1, // Insertion
dp[i-1][j-1]+1, // Substitution
)
}
}
}
// The minimum edit distance is in dp[m][n]
return dp[m][n]
}
// min returns the minimum of three integers.
func min(a, b, c int) int {
if a < b {
if a < c {
return a
}
return c
}
if b < c {
return b
}
return c
}
func main() {
// Example strings
s1 := "kitten"
s2 := "sitting"
distance := minEditDistance(s1, s2)
fmt.Printf("Minimum Edit Distance: %d\n", distance)
}
Explanation of the Code:
- Function
minEditDistance(s1, s2 string) int
:- This function computes the minimum edit distance between two strings using dynamic programming.
- It initializes a 2D slice
dp
, wheredp[i][j]
represents the minimum edit distance between the firsti
characters ofs1
and the firstj
characters ofs2
.
- Dynamic Programming Table (
dp
):- The dimensions of the
dp
table are(m+1) x (n+1)
, wherem
is the length ofs1
andn
is the length ofs2
. - The first row and column are initialized to represent the costs of converting to/from an empty string.
- The dimensions of the
- Logic for Filling the DP Table:
- If the characters at
s1[i-1]
ands2[j-1]
match, the value is taken from the diagonal cell (dp[i-1][j-1]
). - If they do not match, the program computes the minimum edit distance considering the three operations: deletion, insertion, and substitution.
- If the characters at
- Function
min(a, b, c int) int
:- A utility function that returns the minimum of three integers.
How to Run the Program:
- Save the code as
main.go
. - Open your terminal and navigate to the directory containing the file.
- Run the command
go run main.go
.
This program effectively demonstrates the calculation of minimum edit distance using dynamic programming and can be a useful reference for various applications in string processing and comparison tasks.
Conclusion
This Go program efficiently calculates the minimum edit distance between two strings using dynamic
programming. The algorithm has a time complexity of O(m * n), where m and n are the lengths of the
two strings. This method provides a solid foundation for string comparison tasks and can be applied
in various domains, including natural language processing and spell checking.