















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The problem of determining if one string is a shuffle of another and provides an efficient dynamic programming algorithm for the edit distance problem. The edit distance problem measures the minimum number of edit operations (substitution, insertion, or deletion) required to transform one string into another. The document also covers customizations for substring matching and longest common subsequence problems.
Typology: Slides
1 / 23
This page cannot be seen from the preview
Don't miss anything!
















#define MATCH 0 (* enumerated type symbol for match ) #define INSERT 1 ( enumerated type symbol for insert ) #define DELETE 2 ( enumerated type symbol for delete *)
int string compare(char *s, char t, int i, int j) { int k; ( counter ) int opt[3]; ( cost of the three 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]; for (k=INSERT; k<=DELETE; k++) if (opt[k] < lowest cost) lowest cost = opt[k];
return( lowest cost ); }
int string compare(char *s, char t) { int i,j,k; ( counters ) int opt[3]; ( cost of the three options *)
for (i=0; i<MAXLEN; i++) { row init(i); column init(i); }
for (i=1; i<strlen(s); i++) for (j=1; j<strlen(t); j++) { opt[MATCH] = m[i-1][j-1].cost + match(s[i],t[j]); opt[INSERT] = m[i][j-1].cost + indel(t[j]); opt[DELETE] = m[i-1][j].cost + indel(s[i]);
m[i][j].cost = opt[MATCH]; m[i][j].parent = MATCH; for (k=INSERT; k<=DELETE; k++) if (opt[k] < m[i][j].cost) { m[i][j].cost = opt[k]; m[i][j].parent = k; } }
P y o u - s h o u l d - n o t T pos 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 t: 1 1 1 2 3 4 5 6 7 8 9 10 11 12 13 13 h: 2 2 2 2 3 4 5 5 6 7 8 9 10 11 12 13 o: 3 3 3 2 3 4 5 6 5 6 7 8 9 10 11 12 u: 4 4 4 3 2 3 4 5 6 5 6 7 8 9 10 11 -: 5 5 5 4 3 2 3 4 5 6 6 7 7 8 9 10 s: 6 6 6 5 4 3 2 3 4 5 6 7 8 8 9 10 h: 7 7 7 6 5 4 3 2 3 4 5 6 7 8 9 10 a: 8 8 8 7 6 5 4 3 3 4 5 6 7 8 9 10 l: 9 9 9 8 7 6 5 4 4 4 4 5 6 7 8 9 t: 10 10 10 9 8 7 6 5 5 5 5 5 6 7 8 8 -: 11 11 11 10 9 8 7 6 6 6 6 6 5 6 7 8 n: 12 12 12 11 10 9 8 7 7 7 7 7 6 5 6 7 o: 13 13 13 12 11 10 9 8 7 8 8 8 7 6 5 6 t: 14 14 14 13 12 11 10 9 8 8 9 9 8 7 6 5
reconstruct path(char *s, char *t, int i, int j) { if (m[i][j].parent == -1) return;
if (m[i][j].parent == MATCH) { reconstruct path(s,t,i-1,j-1); match out(s, t, i, j); return; } if (m[i][j].parent == INSERT) { reconstruct path(s,t,i,j-1); insert out(t,j); return; } if (m[i][j].parent == DELETE) { reconstruct path(s,t,i-1,j); delete out(s,i); return; } }
row init(int i) { m[0][i].cost = 0; (* note change ) m[0][i].parent = -1; ( note change *) }
goal cell(char *s, char *t, int *i, int j) { int k; ( counter *)
i = strlen(s) - 1; j = 0; for (k=1; k<strlen(t); k++) if (m[i][k].cost < m[i][*j].cost) *j = k; }