c++ - Edit distance recursive algorithm -- Skiena -
i'm reading algorithm design manual steven skiena, , i'm on dynamic programming chapter. has example code edit distance , uses functions explained neither in book nor on internet. i'm wondering
a) how algorithm work?
b) functions indel , match do?
#define match 0 /* enumerated type symbol match */ #define insert 1 /* enumerated type symbol insert */ #define delete 2 /* enumerated type symbol delete */ int string_compare(char *s, char *t, int i, int j) { int k; /* counter */ int opt[3]; /* cost of 3 options */ int lowest_cost; /* lowest cost */ if (i == 0) return(j * indel(' ')); if (j == 0) return(i * indel(' ')); opt[match] = string_compare(s,t,i-1,j-1) + match(s[i],t[j]); opt[insert] = string_compare(s,t,i,j-1) + indel(t[j]); opt[delete] = string_compare(s,t,i-1,j) + indel(s[i]); lowest_cost = opt[match]; (k=insert; k<=delete; k++) if (opt[k] < lowest_cost) lowest_cost = opt[k]; return( lowest_cost ); }
on page 287 in book:
int match(char c, char d) { if (c == d) return(0); else return(1); } int indel(char c) { return(1); }
Comments
Post a Comment