How to resolve the algorithm Levenshtein distance step by step in the Go programming language
How to resolve the algorithm Levenshtein distance step by step in the Go programming language
Table of Contents
Problem Statement
In information theory and computer science, the Levenshtein distance is a metric for measuring the amount of difference between two sequences (i.e. an edit distance). The Levenshtein distance between two strings is defined as the minimum number of edits needed to transform one string into the other, with the allowable edit operations being insertion, deletion, or substitution of a single character.
The Levenshtein distance between "kitten" and "sitting" is 3, since the following three edits change one into the other, and there isn't a way to do it with fewer than three edits:
The Levenshtein distance between "rosettacode", "raisethysword" is 8. The distance between two strings is same as that when both strings are reversed.
Implements a Levenshtein distance function, or uses a library function, to show the Levenshtein distance between "kitten" and "sitting".
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Levenshtein distance step by step in the Go programming language
Levenshtein Distance
The provided source code calculates the Levenshtein distance between two strings using two different implementations.
First Implementation (ld function)
- Creates a 2D matrix
d
whered[i][j]
represents the Levenshtein distance between the prefixes of lengthi
in strings
and prefixes of lengthj
in stringt
. - Initializes the first row and column to be the length of the corresponding string, representing the case when one string is empty and the other is not.
- Iterates over the strings and matrix, comparing characters and updating the distance.
- If characters match, distance remains the same as the diagonal element (
d[i-1][j-1]
). - If characters don't match, it calculates the minimum of the following options:
- Inserting a character (
d[i-1][j] + 1
) - Deleting a character (
d[i][j-1] + 1
) - Substituting a character (
d[i-1][j-1] + 1
)
- Inserting a character (
- Returns the Levenshtein distance stored in
d[len(s)][len(t)]
.
Second Implementation (levenshtein function)
- This implementation is recursive and operates on the suffix of the strings.
- If either string is empty, the distance is the length of the other string.
- If the first characters match, it recursively calls itself on the suffixes of the strings.
- If characters don't match, it considers three options:
- Inserting a character (
levenshtein(s[1:], t[1:]) + 1
) - Deleting a character (
levenshtein(s, t[1:]) + 1
) - Substituting a character (
levenshtein(s[1:], t) + 1
)
- Inserting a character (
- Returns the minimum of the three options.
Main Function
- The
main
function calls theld
orlevenshtein
function to calculate the Levenshtein distance between two sample strings. - It prints the calculated distance to the console.
Source code in the go programming language
package main
import "fmt"
func main() {
fmt.Println(ld("kitten", "sitting"))
}
func ld(s, t string) int {
d := make([][]int, len(s)+1)
for i := range d {
d[i] = make([]int, len(t)+1)
}
for i := range d {
d[i][0] = i
}
for j := range d[0] {
d[0][j] = j
}
for j := 1; j <= len(t); j++ {
for i := 1; i <= len(s); i++ {
if s[i-1] == t[j-1] {
d[i][j] = d[i-1][j-1]
} else {
min := d[i-1][j]
if d[i][j-1] < min {
min = d[i][j-1]
}
if d[i-1][j-1] < min {
min = d[i-1][j-1]
}
d[i][j] = min + 1
}
}
}
return d[len(s)][len(t)]
}
package main
import "fmt"
func levenshtein(s, t string) int {
if s == "" { return len(t) }
if t == "" { return len(s) }
if s[0] == t[0] {
return levenshtein(s[1:], t[1:])
}
a := levenshtein(s[1:], t[1:])
b := levenshtein(s, t[1:])
c := levenshtein(s[1:], t)
if a > b { a = b }
if a > c { a = c }
return a + 1
}
func main() {
s1 := "rosettacode"
s2 := "raisethysword"
fmt.Printf("distance between %s and %s: %d\n", s1, s2,
levenshtein(s1, s2))
}
You may also check:How to resolve the algorithm Reverse a string step by step in the Wart programming language
You may also check:How to resolve the algorithm Reverse words in a string step by step in the Scheme programming language
You may also check:How to resolve the algorithm Guess the number step by step in the Erlang programming language
You may also check:How to resolve the algorithm Function definition step by step in the Io programming language
You may also check:How to resolve the algorithm String concatenation step by step in the Bracmat programming language