Skip to content

Commit d7267e9

Browse files
committed
Update code and README for Swift 3.0
1 parent 2924e18 commit d7267e9

File tree

4 files changed

+43
-31
lines changed

4 files changed

+43
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
extension String {
2-
2+
33
public func minimumEditDistance(other: String) -> Int {
44
let m = self.characters.count
55
let n = other.characters.count
6-
var matrix = [[Int]](count: m+1, repeatedValue: [Int](count: n+1, repeatedValue: 0))
7-
8-
6+
var matrix = [[Int]](repeating: [Int](repeating: 0, count: n + 1), count: m + 1)
7+
98
// initialize matrix
109
for index in 1...m {
1110
// the distance of any first string to an empty second string
12-
matrix[index][0]=index
11+
matrix[index][0] = index
1312
}
13+
1414
for index in 1...n {
1515
// the distance of any second string to an empty first string
16-
matrix[0][index]=index
16+
matrix[0][index] = index
1717
}
18-
18+
1919
// compute Levenshtein distance
20-
for (i, selfChar) in self.characters.enumerate() {
21-
for (j, otherChar) in other.characters.enumerate() {
20+
for (i, selfChar) in self.characters.enumerated() {
21+
for (j, otherChar) in other.characters.enumerated() {
2222
if otherChar == selfChar {
2323
// substitution of equal symbols with cost 0
24-
matrix[i+1][j+1] = matrix[i][j]
24+
matrix[i + 1][j + 1] = matrix[i][j]
2525
} else {
26-
// minimum of the cost of insertion, deletion, or substitution added to the already computed costs in the corresponding cells
27-
matrix[i+1][j+1] = min(matrix[i][j]+1, matrix[i+1][j]+1, matrix[i][j+1]+1)
26+
// minimum of the cost of insertion, deletion, or substitution
27+
// added to the already computed costs in the corresponding cells
28+
matrix[i + 1][j + 1] = min(matrix[i][j] + 1, matrix[i + 1][j] + 1, matrix[i][j + 1] + 1)
2829
}
29-
3030
}
3131
}
3232
return matrix[m][n]
3333
}
3434
}
35+
36+
"Door".minimumEditDistance(other: "Dolls")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Minimum Edit Distance/MinimumEditDistance.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Minimum Edit Distance/README.markdown

100644100755
Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The minimum edit distance is a possibility to measure the similarity of two stri
66

77
A common distance measure is given by the *Levenshtein distance*, which allows the following three transformation operations:
88

9-
* **Inseration** (*ε→x*) of a single symbol *x* with **cost 1**,
9+
* **Insertion** (*ε→x*) of a single symbol *x* with **cost 1**,
1010
* **Deletion** (*x→ε*) of a single symbol *x* with **cost 1**, and
1111
* **Substitution** (*x→y*) of two single symbols *x, y* with **cost 1** if *x≠y* and with **cost 0** otherwise.
1212

@@ -15,37 +15,38 @@ When transforming a string by a sequence of operations, the costs of the single
1515
To avoid exponential time complexity, the minimum edit distance of two strings in the usual is computed using *dynamic programming*. For this in a matrix
1616

1717
```swift
18-
var matrix = [[Int]](count: m+1, repeatedValue: [Int](count: n+1, repeatedValue: 0))
18+
var matrix = [[Int]](repeating: [Int](repeating: 0, count: n + 1), count: m + 1)
1919
```
2020

2121
already computed minimal edit distances of prefixes of *w* and *u* (of length *m* and *n*, respectively) are used to fill the matrix. In a first step the matrix is initialized by filling the first row and the first column as follows:
2222

2323
```swift
2424
// initialize matrix
2525
for index in 1...m {
26-
// the distance of any prefix of the first string to an empty second string
27-
matrix[index][0]=index
26+
// the distance of any first string to an empty second string
27+
matrix[index][0] = index
2828
}
29+
2930
for index in 1...n {
30-
// the distance of any prefix of the second string to an empty first string
31-
matrix[0][index]=index
31+
// the distance of any second string to an empty first string
32+
matrix[0][index] = index
3233
}
3334
```
35+
3436
Then in each cell the minimum of the cost of insertion, deletion, or substitution added to the already computed costs in the corresponding cells is chosen. In this way the matrix is filled iteratively:
3537

3638
```swift
3739
// compute Levenshtein distance
38-
for (i, selfChar) in self.characters.enumerate() {
39-
for (j, otherChar) in other.characters.enumerate() {
40+
for (i, selfChar) in self.characters.enumerated() {
41+
for (j, otherChar) in other.characters.enumerated() {
4042
if otherChar == selfChar {
4143
// substitution of equal symbols with cost 0
42-
matrix[i+1][j+1] = matrix[i][j]
44+
matrix[i + 1][j + 1] = matrix[i][j]
4345
} else {
44-
// minimum of the cost of insertion, deletion, or substitution added
45-
// to the already computed costs in the corresponing cells
46-
matrix[i+1][j+1] = min(matrix[i][j]+1, matrix[i+1][j]+1, matrix[i][j+1]+1)
47-
}
48-
46+
// minimum of the cost of insertion, deletion, or substitution
47+
// added to the already computed costs in the corresponding cells
48+
matrix[i + 1][j + 1] = min(matrix[i][j] + 1, matrix[i + 1][j] + 1, matrix[i][j + 1] + 1)
49+
}
4950
}
5051
}
5152
```
@@ -58,8 +59,6 @@ return matrix[m][n]
5859

5960
This algorithm has a time complexity of Θ(*mn*).
6061

61-
### Other distance measures
62-
63-
**todo**
62+
**TODO**: Other distance measures.
6463

65-
*Written for Swift Algorithm Club by Luisa Herrmann*
64+
*Written for Swift Algorithm Club by Luisa Herrmann*

0 commit comments

Comments
 (0)