Reputation: 4690
I have a homework assignment in which I need to compute the edit distance between two strings. I got the initial function to work but I've been having trouble with this part
Now add the cutoff into the edit distance. This shouldn't change what result are produced, but will drastically speed up the performance.
Here's my original function:
static unsigned int compute_edit_distance(const char *const a,
const char *const b)
{
if (strcmp(a, b) == 0) return 0;
if (a[0] == '\0') return strlen(b);
if (b[0] == '\0') return strlen(a);
unsigned int remove_from_a =
compute_edit_distance(a + 1, b) + 1;
unsigned int remove_from_b =
compute_edit_distance(a, b + 1) + 1;
unsigned int remove_from_both =
compute_edit_distance(a + 1, b + 1);
if (tolower(a[0]) != tolower(b[0])) ++remove_from_both;
return get_min(get_min(remove_from_a, remove_from_b),
remove_from_both);
}
I've tried a few things, but none of them work. My latest change is this
if (depth == MAX_EDIT_DISTANCE_DEPTH)
{
size_t a_length = strlen(a);
size_t b_length = strlen(b);
size_t max_length = (a_length > b_length) ? a_length : b_length;
return MAX_EDIT_DISTANCE_DEPTH + max_length;
}
with a new function signature
static unsigned int compute_edit_distance(const char *const a,
const char *const b, unsigned int depth)
but that doesn't work either.
Can I get a hint on how to do this right? Thanks!
Upvotes: 2
Views: 490
Reputation: 8449
The easiest way is to pass in the "depth remaining" as a parameter. That is, the first call gets passed the cut-off depth, and all the recursive calls get passed a smaller number, which you determine by the type of edit made.
The fundamental idea is that in your first solution, the depth is calculated after the branch is explored recursively. That is, the calls are all made down the branch, then the numbers added together on the way back up the branch.
You can still do this to calculate the depth, but to prevent the branch from going too far, you pass in a running total of the editing budget you have already used in the calls on the way down the branch, or equivalently the editing budget that remains.
You'll need some trick to pass back a number from the failing branch to make sure the number will be rejected. For example return a number that you know will be too large, then check the result at the end. E.g., return MAX_DEPTH + 1, or similar.
Upvotes: 2