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

  1. 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.
  2. Dynamic Programming Table: A 2D slice dp is created to store the edit distances
    for different prefixes of the two strings.
  3. 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.
  4. 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, where dp[i][j] represents the minimum edit distance between the first i characters of s1 and the first j characters of s2.
  • Dynamic Programming Table (dp):
    • The dimensions of the dp table are (m+1) x (n+1), where m is the length of s1 and n is the length of s2.
    • The first row and column are initialized to represent the costs of converting to/from an empty string.
  • Logic for Filling the DP Table:
    • If the characters at s1[i-1] and s2[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.
  • Function min(a, b, c int) int:
    • A utility function that returns the minimum of three integers.

How to Run the Program:

  1. Save the code as main.go.
  2. Open your terminal and navigate to the directory containing the file.
  3. 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.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)