Skip to content

Commit

Permalink
Abort Levenshtein distance calculation early when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
porchard committed Nov 8, 2019
1 parent bb89aa1 commit f61211c
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/cta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,17 @@ long long int levenshtein_distance(const std::string& s1, const std::string& s2,
v2[j + 1] = std::min(v2[j] + 1, std::min(v1[j + 1] + 1, v1[j] + cost));
}

long long int minimum = maximum + 1;
for (long long int j = 0; j < v1_size; j++) {
if (v2[j] < minimum) {
minimum = v2[j];
}
v1[j] = v2[j];
}
if (minimum > maximum) {
// abort early
return maximum + 1;
}
}

return v2[s2_length];
Expand Down

0 comments on commit f61211c

Please sign in to comment.