Skip to content

Commit 4e17826

Browse files
Implemented edit distance dp function (#20)
1 parent ce4cb17 commit 4e17826

File tree

4 files changed

+85
-0
lines changed

4 files changed

+85
-0
lines changed

build.zig

+7
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ pub fn build(b: *std.Build) void {
104104
.name = "longestIncreasingSubsequence.zig",
105105
.category = "dynamicProgramming"
106106
});
107+
if (std.mem.eql(u8, op, "dp/editDistance"))
108+
build_algorithm(b, .{
109+
.optimize = optimize,
110+
.target = target,
111+
.name = "editDistance.zig",
112+
.category = "dynamicProgramming"
113+
});
107114

108115
// Math algorithms
109116
if (std.mem.eql(u8, op, "math/ceil"))

dynamicProgramming/editDistance.zig

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const std = @import("std");
2+
const testing = std.testing;
3+
4+
// Function that computes the minimum distance(or operations) to make 2 strings equal
5+
// Well known as the edit distance dp function.
6+
// Arguments:
7+
// word1: The first passed string
8+
// word2: The second passed string
9+
// Returns u32: The minimum operations to make the 2 strings equal
10+
pub fn minDist(comptime word1: []const u8, comptime word2: []const u8) u32 {
11+
if (word1.len == 0 and word2.len == 0) {
12+
return 0;
13+
}
14+
if (word1.len == 0 and word2.len != 0) {
15+
return @as(u32, @intCast(word2.len));
16+
}
17+
if (word1.len != 0 and word2.len == 0) {
18+
return @as(u32, @intCast(word1.len));
19+
}
20+
21+
const n: usize = word1.len;
22+
const w: usize = word2.len;
23+
24+
var dp: [n + 1][w + 1]u32 = undefined;
25+
for (0..(n + 1)) |i| {
26+
dp[i][0] = @as(u32, @intCast(i));
27+
}
28+
29+
for (0..(w + 1)) |i| {
30+
dp[0][i] = @as(u32, @intCast(i));
31+
}
32+
33+
for (1..(n + 1)) |i| {
34+
for (1..(w + 1)) |j| {
35+
if (word1[i - 1] == word2[j - 1]) {
36+
dp[i][j] = dp[i - 1][j - 1];
37+
} else {
38+
dp[i][j] = @min(dp[i - 1][j - 1], @min(dp[i - 1][j], dp[i][j - 1])) + 1;
39+
}
40+
}
41+
}
42+
43+
return dp[n][w];
44+
}
45+
46+
test "Testing edit distance function" {
47+
const word1 = "hello";
48+
const word2 = "world";
49+
50+
try testing.expect(minDist(word1, word2) == 4);
51+
52+
const word3 = "Hell0There";
53+
const word4 = "hellothere";
54+
55+
try testing.expect(minDist(word3, word4) == 3);
56+
57+
const word5 = "abcdefg";
58+
const word6 = "abcdefg";
59+
60+
try testing.expect(minDist(word5, word6) == 0);
61+
62+
const word7 = "";
63+
const word8 = "abasda";
64+
65+
try testing.expect(minDist(word7, word8) == 6);
66+
67+
const word9 = "abcsa";
68+
const word10 = "";
69+
70+
try testing.expect(minDist(word9, word10) == 5);
71+
72+
const word11 = "sdasdafda";
73+
const word12 = "sdasdbbba";
74+
75+
try testing.expect(minDist(word11, word12) == 3);
76+
}

runall.cmd

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ rem Dynamic Programming
2626
%ZIG_TEST% -Dalgorithm=dp/coinChange %Args%
2727
%ZIG_TEST% -Dalgorithm=dp/knapsack %Args%
2828
%ZIG_TEST% -Dalgorithm=dp/longestIncreasingSubsequence %Args%
29+
%ZIG_TEST% -Dalgorithm=dp/editDistance %Args%
2930

3031
rem Sort
3132
%ZIG_TEST% -Dalgorithm=sort/quicksort %Args%

runall.sh

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ $ZIG_TEST -Dalgorithm=ds/lrucache $Args
2626
$ZIG_TEST -Dalgorithm=dp/coinChange $Args
2727
$ZIG_TEST -Dalgorithm=dp/knapsack $Args
2828
$ZIG_TEST -Dalgorithm=dp/longestIncreasingSubsequence $Args
29+
$ZIG_TEST -Dalgorithm=dp/editDistance $Args
2930

3031
## Sort
3132
$ZIG_TEST -Dalgorithm=sort/quicksort $Args

0 commit comments

Comments
 (0)