Question

In: Computer Science

Find the edit distance between HORSE and NORTH Please give a dynamic-programming table for computing this...

Find the edit distance between HORSE and NORTH Please give a dynamic-programming table for computing this distance

Solutions

Expert Solution

Edit distance:

Edit distance is the minimum number of edit operations required to convert from string 1 to string 2

Code:

def editDist(str1, str2):
    m=len(str1)
    n=len(str2)
    # this table will stores the results
    dp = [[0 for x in range(n + 1)] for x in range(m + 1)] 
  
    for i in range(m + 1): 
        for j in range(n + 1):      #filling the table with bottom up approach
            if i == 0:           # i==0 then insert the dp[0][j] as j
                dp[i][j] = j    # minimum operations = j
            elif j == 0: # j==0 then insert the dp[i][0] as i
                dp[i][j] = i    # minimum operations = i 
            elif str1[i-1] == str2[j-1]:   #if characters are equal then ignore last 
                dp[i][j] = dp[i-1][j-1] 
            else:                    #else consider the possibilities
                dp[i][j] = 1 + min(dp[i][j-1],dp[i-1][j],dp[i-1][j-1])       
                                    #Insert    #Remove    # Replace 
    return dp 
  
# Driver program 
str1 = "HORSE"
str2 = "NORTH"
m=len(str1)
n=len(str2)
dp=editDist(str1, str2) 
for i in range(m+1):
    for j in range(n+1):
        print(dp[i][j],"", end="")
    print()

print("Minimum edit Distance between ",str1," and ",str2," is :", dp[-1][-1])

SCREENSHOT FOR INDENTATION REFERENCE :

Output :

******

I HOPE YOU UNDERSTAND THIS PROGRAM , FOR ANY CLARIFICATION COMMENT, I AM HAPPY TO HELP YOU . PLEASE GIVE ME FEEDBACK AS WELL

PLEASE UPVOTE : )

*******


Related Solutions

what is the time complexity of the edit distance ("ALASKA" vs "ALABAMA" example) dynamic programming algorithm?...
what is the time complexity of the edit distance ("ALASKA" vs "ALABAMA" example) dynamic programming algorithm? what is the space complexity of the edit distance ("ALASKA" vs "ALABAMA" example) dynamic programming algorithm?
Why does NLP need AI? Q2) Consider the dynamic programming (DP) approach to solve the edit...
Why does NLP need AI? Q2) Consider the dynamic programming (DP) approach to solve the edit distant problem, in which the distant between two strings are calculated by ?(?,?)=min{?(?−1,?)+1, ?(?,?−1)+1, ?(?−1,?−1)+?(?,?), where ?(?,?)=2, if the corresponding letters are not matching, and ?(?,?)=0, if they are matching. Apply this DP approach to compute the edit distance in the following example. Y 3 A 2 P 1 # 0 1 2 3 4 # P L A Y Q3) Given the automaton...
Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the...
Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the Weightlifting Walrus is training for a contest where it will have to lift 1000 kg. Wallace has some weight plates lying around, possibly of different weights, and its goal is to add some of the plates to a bar so that it can train with a weight as close as possible to 1000 kg. In case there exist two such numbers which are equally...
Dynamic Programming Question. Make the table for finding a LCS (Longest Common Subsequence) of the strings...
Dynamic Programming Question. Make the table for finding a LCS (Longest Common Subsequence) of the strings SLWOVNNDK and ALWGQVNBKB. You need to Show the traceback for the LCS.
The merry-go-round rotates counterclockwise with a constant angular speed u. The distance between the horse on...
The merry-go-round rotates counterclockwise with a constant angular speed u. The distance between the horse on the merry-go-round and the rotational center is r. (a) Find the position of the horse x and its velocity v, v(t) = d/dt x(t), as vector-functions of time. (b) Find the acceleration of the horse, a(t) = d^2/dt^2 x(t), as a vector-function of time. What is its direction (in comparison with the direction of x)? Now the same horse has a non-constant angular speed...
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic...
Implement a queue - C programming, please read the instruction carefully and implement queue.c using dynamic array structure given dynarray.h and dynarray.c below The second ADT you'll implement for this assignment is a queue. For this assignment, the interface for the queue (i.e. the structures and the prototypes of functions a user of the queue interacts with) is already defined for you in the file queue.h. Your first task in this assignment is to implement definitions for the functions that...
The following two questions can be solved by dynamic programming. For each question, please describe optimal...
The following two questions can be solved by dynamic programming. For each question, please describe optimal substructure and express recurrence relation. Give pseudo-code and analyze time and space complexity of your algorithm. 1. Longest palindrome subsequence A palindrome is a nonempty string over some alphabet that reads the same forward and backward. Examples of palindromes are all strings of length 1, “civic”, “racecar”, and “aibohphobia” (fear of palindromes). Design a dynamic programming algorithm to find the longest palindrome that is...
Using dynamic programming, find an optimal parenthesization of a matrix-chain product of 4 matrices whose dimensions...
Using dynamic programming, find an optimal parenthesization of a matrix-chain product of 4 matrices whose dimensions are p = { 1, 10, 5, 20, 2}. Show your work.
find when the rift between The North American and European plates
The North American and European plates of the Earth’s crust are drifting apart with a relative speed of about 25 mm/yr. Take the speed as constant and find when the rift between them started to open, to reach a current width of 2.9 x10^3 mi.
What is the difference between an active document and a dynamic document? Give example of those...
What is the difference between an active document and a dynamic document? Give example of those document! In electronic mail, what is MIME? Why do we need POP3 or IMAP4 for electronic mail? Explain
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT