Skip to content
Merged

Day8 #99

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions day8/Python/minimum_edit_distance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""
@author : imkaka
@date : 31/12/2018

"""

import sys


def min_edit_distance(str1, str2):
len1 = len(str1)
len2 = len(str2)

# Matrix inilization
dp = [[0 for i in range(len2 + 1)]
for j in range(len1 + 1)]

for i in range(1, len1 + 1):
dp[i][0] = i

for j in range(1, len2 + 1):
dp[0][j] = j

# Fill the DP matrix.

for j in range(1, len2 + 1):
for i in range(1, len1 + 1):
if(str1[i - 1] == str2[j - 1]):
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = 1 + min(dp[i - 1][j - 1],
dp[i][j - 1],
dp[i - 1][j])
return dp[len1][len2]


def main():

print("==================Minimum Edit Distance====================")
print()

print(min_edit_distance("kitten", "sitting"))
print(min_edit_distance("abcdef", "abcdhgikll"))


if __name__ == '__main__':
main()
143 changes: 100 additions & 43 deletions day8/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ Given two strings, and operations like replace, delete and add, write a program

## Example (source: [Wikipedia](https://en.wikipedia.org/wiki/Levenshtein_distance))

For example, the (Minimum Edit) Levenshtein distance between `kitten` and
`sitting` is `3`, since the following three edits change one
into the other, and there is no way to do it with fewer than
For example, the (Minimum Edit) Levenshtein distance between `kitten` and
`sitting` is `3`, since the following three edits change one
into the other, and there is no way to do it with fewer than
three edits:

1. **k**itten → **s**itten (substitution of "s" for "k")
Expand All @@ -27,13 +27,13 @@ Mathematically, the Levenshtein distance between two strings `a` and `b` (of len

![def](https://wikimedia.org/api/rest_v1/media/math/render/svg/f0a48ecfc9852c042382fdc33c19e11a16948e85)

where
where
![def](https://wikimedia.org/api/rest_v1/media/math/render/svg/52512ede08444b13838c570ba4a3fc71d54dbce9)
is the indicator function equal to `0` when
![def](https://wikimedia.org/api/rest_v1/media/math/render/svg/231fda9ee578f0328c5ca28088d01928bb0aaaec)
and equal to 1 otherwise, and
![def](https://wikimedia.org/api/rest_v1/media/math/render/svg/bdc0315678caad28648aafedb6ebafb16bd1655c)
is the distance between the first `i` characters of `a` and the first
is the distance between the first `i` characters of `a` and the first
`j` characters of `b`.

Therefore, the minimum edit distance between `a` and `b` is the last element in the edit distance matrix
Expand Down Expand Up @@ -110,50 +110,50 @@ console.log(minEditDist('kitten', 'sitting'));
* @date : 31/12/2018
*/

#include<bits/stdc++.h>
using namespace std;
int min(int x, int y, int z)
{
return min(min(x, y), z);
}
int levenshtein_distance(string str1, string str2, int m, int n)
{
int ld[m+1][n+1];
for (int i=0; i<=m; i++)
{
for (int j=0; j<=n; j++)
{
if (i==0)
#include<bits/stdc++.h>
using namespace std;

int min(int x, int y, int z)
{
return min(min(x, y), z);
}

int levenshtein_distance(string str1, string str2, int m, int n)
{
int ld[m+1][n+1];

for (int i=0; i<=m; i++)
{
for (int j=0; j<=n; j++)
{
if (i==0)
ld[i][j] = j;
else if (j==0)

else if (j==0)
ld[i][j] = i;
else if (str1[i-1] == str2[j-1])
ld[i][j] = ld[i-1][j-1];


else if (str1[i-1] == str2[j-1])
ld[i][j] = ld[i-1][j-1];

else
ld[i][j] = 1 + min(ld[i][j-1], ld[i-1][j], ld[i-1][j-1]);
}
}
return ld[m][n];
}
int main()
{
ld[i][j] = 1 + min(ld[i][j-1], ld[i-1][j], ld[i-1][j-1]);
}
}

return ld[m][n];
}

int main()
{
string str1,str2;
cin >> str1 >> str2;
cout << levenshtein_distance(str1, str2, str1.length(), str2.length());
return 0;

cout << levenshtein_distance(str1, str2, str1.length(), str2.length());

return 0;
}
```
```

### C Implementation

Expand Down Expand Up @@ -216,6 +216,62 @@ int main()
return 0;
}

```

## Python Implementation

### [Solution](./Python/minimum_edit_distance.py)

```py

"""
@author : imkaka
@date : 31/12/2018

"""

import sys


def min_edit_distance(str1, str2):
len1 = len(str1)
len2 = len(str2)

# Matrix inilization
dp = [[0 for i in range(len2 + 1)]
for j in range(len1 + 1)]

for i in range(1, len1 + 1):
dp[i][0] = i

for j in range(1, len2 + 1):
dp[0][j] = j

# Fill the DP matrix.

for j in range(1, len2 + 1):
for i in range(1, len1 + 1):
if(str1[i - 1] == str2[j - 1]):
dp[i][j] = dp[i - 1][j - 1]
else:
dp[i][j] = 1 + min(dp[i - 1][j - 1],
dp[i][j - 1],
dp[i - 1][j])
return dp[len1][len2]


def main():

print("==================Minimum Edit Distance====================")
print()

print(min_edit_distance("kitten", "sitting"))
print(min_edit_distance("abcdef", "abcdhgikll"))


if __name__ == '__main__':
main()

```

## Java Implementation
Expand Down Expand Up @@ -280,4 +336,5 @@ public class Levenshtein {
System.out.println("Minimum no of operations are "+dist);
}
}

```