1、that produces the same result at lower cost. If a character is replaced by a character aand then replaced again by a character b, then both operations can be replaced by “replace b”. This means that all inserted and replaced characters are never changed afterperforming the insertion or replacement.
2、Notice that after removing these operationsthat introduce dependencies, any sequence of insert, delete, and replace operations canbe reordered so that they occur from left to right without affecting the outcome of thetransformation.(c)We show that computing edit distance for strings x and y can be d
3、one by finding theedit distance of subproblems. Define a cost function(i,j) = d(x,y1.i|xj + 1.m) (1)That is(i,j) is the minimum cost of transforming the first j characters of x intothe first i characters of y. Then d(x,y) =(n,m). Now consider a sequence ofoperations S = (o 1 ,o 2 ,.,o k ) that trans
4、forms x to y with cost C(S) = d(x,y).Let S i be the subsequence of S containing the first i operations of S. Let z i be theauxilliary string after performing operations S i , where z 0 = x and z k = y.(d) We can calculate the edit distance d(x,y) using the definition o𝐜 𝐱𝐲 (i
5、,j) from Equation 1. Recall that d(x,y) = (m,n). Since we showed in part (a) that there existsa sequence of operations that achieves d(x,y) without using the “left” operation, weonly need to consider the four operations “right”, “replace”, “delete”, and “insert”.We can calculate (m,n) recursively. T
6、he base case is when no transformation operations have been performed, so 𝐜 (0,0) = 0.(e)Construct a table T where each entry Ti,j = (i,j). Since each valueof (i,j) only depends on 𝐜 (i1 ,j 2) wherei1= i andj1 𝟎 𝐚𝐧𝐝 𝐣𝐢 = 𝐲Ү
7、43;𝐓+ 𝟒 ,𝐢𝐓+ 𝟐 , 𝐢+ 𝟑5 return Tn,mThe running time of this algorithm is(mn). This algorithm requires(mn) space.(f) x: electrical engineeringy: computer scienceEdit Distance: 54Oper | c |Total| zinitial | 0 | 0 | *electrical engineeringdelete |
8、2 | 2 | *lectrical engineeringdelete | 2 | 4 | *ectrical engineeringdelete | 2 | 6 | *ctrical engineeringright | 0 | 6 | c*trical engineeringinsert | 3 | 9 | co*trical engineeringinsert | 3 | 12 | com*trical engineeringinsert | 3 | 15 | comp*trical engineeringinsert | 3 | 18 | compu*trical engineeri
9、ngright | 0 | 18 | comput*rical engineeringinsert | 3 | 21 | compute*rical engineeringright | 0 | 21 | computer*ical engineeringdelete | 2 | 23 | computer*cal engineeringdelete | 2 | 25 | computer*al engineeringdelete | 2 | 27 | computer*l engineeringdelete | 2 | 29 | computer* engineeringright | 0
10、| 29 | computer *engineeringdelete | 2 | 31 | computer *ngineeringreplace | 4 | 35 | computer s*gineeringreplace | 4 | 39 | computer sc*ineeringright | 0 | 39 | computer sci*neeringdelete | 2 | 41 | computer sci*eeringdelete | 2 | 43 | computer sci*eringright | 0 | 43 | computer scie*ringdelete | 2
11、| 45 | computer scie*ingdelete | 2 | 47 | computer scie*ngright | 0 | 47 | computer scien*ginsert | 3 | 50 | computer scienc*greplace | 4 | 54 | computer science*(g)Input File d(x, y)Input 1 1816Input 2 1824Input 3 1829(h)All the transformation operations can be implemented in O(1) time usingtwo sta
12、cks, L and R. Initially, L is empty and R contains all the characters of x inorder.Operation Implementationleft If not E MPTY (L), then P USH (R, P OP (L)right If not E MPTY (R), then P USH (L, P OP (R)replace by c If not E MPTY (R), then P OP (R), P USH (L, c)delete If not E MPTY (R), then P OP (R)
13、insert c P USH (L, c)Each stack operation requires O(1) time, and each transformation operation requiresonly O(1) stack operations. Therefore each operation requires O(1) time.代码部分:#include stdio.hstdlib.hstring.h/ Operation Costs#define MOVE_COST 0#define REPLACE_COST 4#define INSERT_COST 3#define
14、DELETE_COST 2/ Operation Typestypedef enum initial, right, insert, delete, replace, final opType;char* opStrings = initial, rightinsertdeletereplacefinal;/ Recovers and prints out the edits necessary to change x into y.static void recoverEdits(char* x, int m,char* y, int n,int* d, opType* opPerforme
15、d);/ Computes the edit distance between x and ystatic void computeEditDistance(char* x, int m,char* y, int n) / In this dynamic program, dij stores the edit distance between / the string xi.m-1 and yj.n-1. / NOTE! C arrays start indexing at 0.int* d;opType* opPerformed; / store operations for recons
16、tructing the answeropType opDone;int i, j;d = (int*)malloc(m+1)*sizeof(int*);assert(d != NULL);opPerformed = (opType*)malloc(m+1)*sizeof(opType*);assert(opPerformed !for (i = 0; i = m; i+) di = (int*)malloc(n+1)*sizeof(int);assert(di !opPerformedi = (opType*)malloc(n+1)*sizeof(opType);assert(opPerfo
17、rmedi !/ Initialize the base cases./ xm and yn are both null strings. Edit distance between them is 0.dmn = 0;opPerformedmn = final; m;/ To convert xi.m-1 to the null string yn, delete all (m-i)/ remaining characters in x.din = DELETE_COST*(m-i);opPerformedin = delete;for (j = 0; j =0; i-) for (j =
18、n-1; j = 0; j-) int costForReplaceOrMove;int costForInsert;int costForDelete;int minValue;/ Compute dij as the minimum of 4 terms:/ If xi = yj, we could move right./ Otherwise, we can replace xi with yj and/ increment i and j.costForReplaceOrMove = di+1j+1 + REPLACE_COST*(xi != yj) + MOVE_COST*(xi =
19、 yj);/ If we insert a character into x to match yj, then/ we increment j by 1costForInsert = dij+1 + INSERT_COST;/ If we delete a character in x, then we/ increment i by 1.costForDelete = di+1j + DELETE_COST;/ Of the above operations, find one that gives us/ a minimum cost.minValue = costForReplaceO
20、rMove;if (xi != yj)opDone = replace;else opDone = right;if (minValue costForInsert) minValue = costForInsert;opDone = insert; costForDelete) minValue = costForDelete;opDone = delete;dij = minValue;opPerformedij = opDone;/ Final answerprintf(Edit Distance = %dn, d00);/ Recover operations / print out
21、answer only if the strings arent too long.if (m 75) & (n 75) recoverEdits(x, m, y, n, d, opPerformed);free(di);free(opPerformedi);free(d);free(opPerformed); int* d, opType* opPerformed) int i = 0; int j = 0;int k = 0;int a;int opDone;int costSoFar = 0;int stepCost = 0;char* newString;newString = (ch
22、ar*)malloc(sizeof(char) * (m + n);/ Rather than implementing a string object for x that allows us/ constant-time inserts and deletes at the cursor, / we are just going to have two copies of the string./ newString0.k stores everything before the cursor that we have/ changed, and xi.m-1 will store all
23、 the parts after the cursor that/ are still the same.strncpy(newString, x, m);n);( i, j): %8s | c | Total | z nOper-n(%2d, %2d): %8s | %4d | %4d | , i, j, , costSoFar, stepCost);*%sn, x);while (opPerformedij != final) opDone = opPerformedij;switch(opDone) case right:newStringk = xi;i+;j+;stepCost = MOVE_COST;break;case replace:newStringk = yj;stepCost = REPLACE_COST;case insert:stepCost = INSERT_COST;case delete:stepCost = DELETE_COST;default: / We should never a follow a pointer to initial or final.ERROR.nexit(1);if (opDone != delete) k+;costSoFar += stepCost; %8s |
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1