Question

In: Computer Science

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?

Solutions

Expert Solution

Edit Distance Problem Statement :

Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.
You have the following 3 operations permitted on a word:
Insert a character
Delete a character
Replace a character

Solution :
We use dyanamic programming approach where we build a 2-d array of size n x m , where n is length of word1 and m is the length of word2.
dp[i][j] = number of opearions require to convert word1[0...i] to word2[0....j]

if word1[i] == word2[j] then ,
dp[i][j] = dp[i-1][j-1] i.e whatever is the answer till i-1 and j-1
else
dp[i][j] = minium (dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + 1 i.e taking considersation of delete,insert and replace respectively.

ALASKA ALABAMA

in word allaska : we can replace S with B and K with A and add M
Thus , total 3 operations required.

Time Complexity : O(N*M) equivalent to O(N^2) [ using two loops of size N and M]
where N and M are lengths of two words .

Space Complexity : O(N*M) equivalent to O(N^2) [ Using a 2-d array ]

Here's code:

#include <bits/stdc++.h>
using namespace std;
int EditDistance(string word1, string word2) {
int i,j,n,m;
n=word1.size();
m=word2.size();
if(n==0 || m==0)
{
return((n==0)?m:n);
}
vector<vector<int> >dp(n+1,vector<int>(m+1,0)); //declare a 2-d a
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
if(i==0 && j==0)continue;
if(i==0)
{
dp[i][j]=dp[i][j-1]+1;
// we have to insert
}
else if(j==0)
{
dp[i][j]=dp[i-1][j]+1;
// we have to delete
}
else if(word1[i-1]==word2[j-1])
{
dp[i][j]=dp[i-1][j-1];
}
else
{
dp[i][j] = min(min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1]) +1; //
taking considersation of delete,insert and replace respectively.
}
}
}
return dp[n][m];
}

int main()
{
string s1 = "ALASKA";
string s2 = "ALABAMA";
  
cout<<EditDistance(s1,s2)<<endl;

return 0;
}


Related Solutions

What is the auxiliary space and space complexity for a dynamic programming algorithm with time complexity...
What is the auxiliary space and space complexity for a dynamic programming algorithm with time complexity of O(n^2)? Justify.
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
What is the Average time complexity of sequential search algorithm in a linked list?
What is the Average time complexity of sequential search algorithm in a linked list?
What is the time complexity of my algorithm? It uses an vector E that contains and...
What is the time complexity of my algorithm? It uses an vector E that contains and object made of a string and an integer. It takes an empty vector as parameter and returns the vector with the top three integers from the objects in E . void Trendtracker::top_three_trends(vector<string> &T) {    T.clear();    if (E.size() == 0) {        return;    }    if (E.size() == 1) {        T.push_back(E[0].hashtag);    }    else if (E.size() == 2)...
Show that the worst-case number of entries computed by the refined dynamic programming algorithm for the...
Show that the worst-case number of entries computed by the refined dynamic programming algorithm for the 0-1 Knapsack problem is in Ω(2n). Do this by considering the instance in which W = 2n −2 and wi = 2i−1 for 1 ≤ i ≤ n. Please answer with clear explanations!!! Excerpt From: Richard Neapolitan. “Foundations of Algorithms.”
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...
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, ....
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, . . . , n] of positive integers, an integer K. Decide: Are there integers in A such that their sum is K. (Return T RUE or F ALSE) Example: The answer is TRUE for the array A = [1, 2, 3] and 5, since 2 + 3 = 5. The answer is FALSE for A = [2, 3, 4] and 8. Note that you...
Problem 2. Purpose: practice algorithm design using dynamic programming. A subsequence is palindromic if it is...
Problem 2. Purpose: practice algorithm design using dynamic programming. A subsequence is palindromic if it is the same whether read left to right or right to left. For instance, the sequence A,C,G,T,G,T,C,A,A,A,A,T,C,G has many palindromic subsequences, including A,C,G,C,A and A,A,A,A (on the other hand, the subsequence A,C,T is not palindromic). Assume you are given a sequence x[1...n] of characters. Denote L(i,j) the length of the longest palindrome in the substring x[i,...,j]. The goal of the Maximum Palindromic Subsequence Problem (MPSP)...
Build a Dynamic Programming algorithm in Python for the following problem. Given two sequences of vowels,...
Build a Dynamic Programming algorithm in Python for the following problem. Given two sequences of vowels, find the length of longest subsequence present in both of them. NOTE: A subsequence is a sequence that appears in the same order, but not necessarily contiguous. For example, “aei”, “aeo”, “eio”, “aiu”, ... are subsequences of “aeiou”. Sample Input 1: aeiou aiu Sample Output 1: 3 Sample Input 2: aeaiueoiuaeeoeooaauoi aeuioauuuoaeieeaueuiouaiieuiuuuaoueueauaeiauuo Sample Output 2: 16 Sample Input 3: iioioieiaiauaoeoiioiiue iuuueauiaieooaoaaouaaaae Sample Output 3:...
Recall the dynamic programming algorithm we saw in class for solving the 0/1 knapsack problem for...
Recall the dynamic programming algorithm we saw in class for solving the 0/1 knapsack problem for n objects with a knapsack capacity of K. In particular, we characterized our recurrence OPT(j, W) to be following quantity: OPT(j, W) := The maximum profit that can be obtained when selecting from objects 1, 2, . . . , j with a knapsack capacity of W , where (after filling in our dynamic programming table), we return the value stored at OPT(n, K)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT