Dynamic Programming Series

Dynamic Programming Series

Day 9

EDIT DISTANCE

 

 

Problem Description

Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2.

Problem Description

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:

  1. Insert a character
  2. Delete a character
  3. Replace a character

Problem Description

Example :     

word1 = "horse"  and word2 = "ros"

Problem Description

Example :     

word1 = "horse"  and word2 = "ros"

horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')

Problem Description

Example :     

word1 = "horse"  and word2 = "ros"

horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')

Minimum steps required  = 3

Problem Description

Example :     

word1 = "intention"  and word2 = "execution"

Problem Description

Example :     

word1 = "intention"  and word2 = "execution"

intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')

Problem Description

Example :     

word1 = "intention"  and word2 = "execution"

intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')

Minimum steps : 5

If you are following the series , the steps we follow while solving any question is:

1. Making observations of the problem.

If you are following the series , the steps we follow while solving any question is:

1. Making observations of the problem.

2. Developing a recursive approach.

If you are following the series , the steps we follow while solving any question is:

1. Making observations of the problem.

2. Developing a recursive approach.

3. Developing an iterative approch.

Making observations of the problem..!!

Suppose we have a string1 = "bcd" and string2 = "bde"

Making observations of the problem..!!

Suppose we have a string1 = "bcd" and string2 = "bde"

                   b                c                d
                   b                 d                e

Making observations of the problem..!!

Suppose we have a string1 = "bcd" and string2 = "bde"

                   b                c                d
                   b                 d                e

This element is same

Making observations of the problem..!!

Suppose we have a string1 = "bcd" and string2 = "bde"

                   b                c                d
                   b                 d                e

This element is same

Answer will come from these part of the strings..!!

Making observations of the problem..!!

Suppose we have a string1 = "bcd" and string2 = "bde"

               c                d
                d                e

Now we have 3 options according to the problem..!!

Making observations of the problem..!!

Suppose we have a string1 = "bcd" and string2 = "bde"

               c                d
                d                e

Option 1: Insert the character..!!

Making observations of the problem..!!

Suppose we have a string1 = "bcd" and string2 = "bde"

                 d                c                d
                d                e

Option 1: Insert the character..!!

These two element matches..!!

Making observations of the problem..!!

Suppose we have a string1 = "bcd" and string2 = "bde"

                 d                c                d
                d                e

Option 1: Insert the character..!!

These two element matches..!!

Making observations of the problem..!!

Suppose we have a string1 = "bcd" and string2 = "bde"

               c                d
                d                e

Option 2: Delete the character..!!

Making observations of the problem..!!

Suppose we have a string1 = "bcd" and string2 = "bde"

               c                d
                d                e

Option 2: Delete the character..!!

Making observations of the problem..!!

Suppose we have a string1 = "bcd" and string2 = "bde"

               c                d
                d                e

Option 3:  Replace  the character..!!

Making observations of the problem..!!

Suppose we have a string1 = "bcd" and string2 = "bde"

               d                d
                d                e

Option 3:  Replace  the character..!!

Now get the answer by applying recursive call

Now by these observations we are very much closer towards a recursive solution..!!

Now lets write down these points..!!

If the character are same of string s1 and s2..!!

recur(s1+1,s2+1)

If the character are same of string s1 and s2..!!

recur(s1+1,s2+1)

else 

1 + recur(s1,s2+1) ..... (condition1)

1 + recur(s1+1,s2+1) ..... (condition3)

1 + recur(s1+1,s2) ..... (condition2)

If the character are same of string s1 and s2..!!

recur(s1+1,s2+1)

else 

1 + recur(s1,s2+1) ..... (condition1)

1 + recur(s1+1,s2+1) ..... (condition3)

1 + recur(s1+1,s2) ..... (condition2)

Minimum of these will be the answer

Now lets look at the recursive approach code..!!

Now lets look at the recursive approach code..!!

And this time by passing a dp array..!!

int res(char* s1, char* s2, int m, int n, int** dp)
{
    if(m == 0)
        return n;
    if(n == 0)
        return m;
    int ans;
    if(dp[m][n] >-1)
        return dp[m][n];
    if(s1[0] == s2[0])
        ans = res(s1+1, s2+1, m-1, n-1, dp);
    else
    {
        int op1 = 1+res(s1+1, s2+1, m-1, n-1, dp);
        int op2 = 1+res(s1, s2+1, m, n-1, dp);
        int op3 = 1+res(s1+1, s2, m-1, n, dp);
        ans = min(min(op1, op2), op3);
        
    }
    dp[m][n] = ans;
    return ans;
        
}

Code of the problem..!!

Now Let's Move to DP iterative solution using same approach

We have already discussed the approach using the Top down recursive approach

DP[i][j] = minimum\ number\ of\ operations\ to\ convert\ string\ s[0...i]\ to\ t[0...j]

Convert string s to t

Let's learn this using a DP matrix

Let s = "horse"

t = "ros"

Convert\ s\ to\ t

DP

0

1

2

3

0

1

2

3

4

5

Let's learn this using a DP matrix

Let s = "horse"

t = "ros"

Convert\ s\ to\ t
0 1 2 3
1
2
3
4
5

DP

0

1

2

3

0

1

2

3

4

5

horse

ros

Let's learn this using a DP matrix

Let s = "horse"

t = "ros"

Convert\ s\ to\ t
0 1 2 3
1 1
2
3
4
5

DP

0

1

2

3

0

1

2

3

4

5

horse

ros

Replace

Delete

Insert

Let's learn this using a DP matrix

Let s = "horse"

t = "ros"

Convert\ s\ to\ t
0 1 2 3
1 1 2
2
3
4
5

DP

0

1

2

3

0

1

2

3

4

5

horse

ros

Replace

Delete

Insert

Let's learn this using a DP matrix

Let s = "horse"

t = "ros"

Convert\ s\ to\ t

horse

ros

0 1 2 3
1 1 2 3
2
3
4
5

DP

0

1

2

3

0

1

2

3

4

5

Replace

Delete

Insert

Let's learn this using a DP matrix

Let s = "horse"

t = "ros"

Convert\ s\ to\ t
0 1 2 3
1 1 2 3
2 2 1 2
3 2 2 2
4 3 3 2
5 4 4 3

DP

0

1

2

3

0

1

2

3

4

5

horse

ros

Similarly we can fill for all other entries.

Now Let's Code the Problem Using the same approach.

int minDistance(string source, string target) {
    int n = source.size();
    int m = target.size();
    vector <vector<int>> dp(n+1, vector<int>(m+1));
    for (int i = 0;i <= n;i++)
    {
        dp[i][0] = i;
    }
    for (int j = 0;j <= m;j++)
    {
        dp[0][j] = j;
    }
    for (int i = 1;i <= n;i++)
    {
        for (int j = 1;j <= m;j++)
        {
            if (source[i-1] == target[j-1])
            {
                dp[i][j] = dp[i-1][j-1];
            }
            else
            {
                int replace = dp[i-1][j-1] + 1;
                int delet =  dp[i-1][j] + 1;
                int insert = dp[i][j-1] + 1;
                dp[i][j] = min(replace, min(delet, insert));
            }
        }
    }
    return dp[n][m];
}

So that's It for Day 9

Concepts that we have learnt

So that's It for Day 9

We have taken a very first step in Dynamic Programming.

Concepts that we have learnt

1. DP on Strings.

So that's It for Day 9

We have taken a very first step in Dynamic Programming.

Concepts that we have learnt

1. DP on Strings.

2. DP on strings can be broken down in two parts when s[i] == t[j] and when s[i] != t[j]

So that's It for Day 9

We have taken a very first step in Dynamic Programming.

Concepts that we have learnt

1. DP on Strings.

2. DP on strings can be broken down in two parts when s[i] == t[j] and when s[i] != t[j]

Still, Have any Doubts???

Still, Have any Doubts???

Comment it You will get a reply

OR

Send your doubt to email provided in description.

Made with Slides.com