1、算法导论上机报告编辑距离编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数。许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符,删除一个字符。一般来说,编辑距离越小,两个串的相似度越大。例如将kitten一字转成sitting:sitten (ks)sittin (ei)sitting (g)(a)Solution:Operation z Cost Totalinitial string 0 0right 0 0insert n 3 3insert a 3 6right analgorithm 0 6repl
2、ace by y 4 10replace by s 4 14replace by i 4 18replace by s 4 22delete 2 24delete 2 26delete analysis 2 28(b)Solution: We argue that there is a sequence S that transforms x to y with cost d(x, y)without using any “left” operations by contradiction. Suppose that no such sequenceS exists. Consider a s
3、equence S 1 that does transform x to y with cost d(x, y) thatuses “left” operations. Consider the characters inserted by the operations in S1. If acharacter is inserted and then later deleted, then both operations can be removed froms1 to produce a sequence S2 that produces the same result at lower
4、cost. If a charactera is inserted and then later replaced by a character b, then the insert operation can bechanged to insert b and the replace operation can be removed to produce a sequence S2that produces the same result at lower cost. If a character is replaced by a character aand then replaced a
5、gain 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. Notice that after removing these operationsthat introduce dependencies, any sequence of insert, delete, and
6、 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 done by finding theedit distance of subproblems. Define a cost function(i,j) = d(x,y1.i|xj + 1.m) (1)That is
7、(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 transforms 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
8、. 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,j) from Equation 1. Recall that d(x,y) = (m,n). Since we showed in part (a) that there existsa sequence of
9、 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. The base case is when no transformation operations have been performed, so 𝐜 𝐱𝐲 (0,0
10、) = 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 𝟎 𝐚𝐧𝐝 𝐣 𝟎 𝐚𝐧𝐝 𝐱𝐢 𝟏 = 𝐲𝐣 ⼮
11、3;𝐓 𝐢 𝟏,𝐣 𝟏+ 𝟒 ,𝐢𝐟 𝐢 𝟎 𝐚𝐧𝐝 𝐣 𝟎𝐓 𝐢 𝟏,𝐣+ 𝟐 , 𝐢𝐟 𝐢 𝟎𝐓 𝐢,𝐣 𝟏+ 𝟑 ,⻐
12、2;𝐟 𝐣 𝟎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 | 2 | 2 | *lectrical engineeringdelete | 2 | 4 |
13、 *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 engineeringright | 0 | 18 | comput*rical engineeringins
14、ert | 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 | 29 | computer *engineeringdelete | 2 | 31 |
15、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 | 45 | computer scie*ingdelete | 2 | 47 | comp
16、uter 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 stacks, L and R. Initially, L is empty and R cont
17、ains 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)insert c P USH (L, c)Each stack operation requ
18、ires O(1) time, and each transformation operation requiresonly O(1) stack operations. Therefore each operation requires O(1) time.代码部分:#include #include #include #include / Operation Costs#define MOVE_COST 0#define REPLACE_COST 4#define INSERT_COST 3#define DELETE_COST 2/ Operation Typestypedef enum
19、 initial, right, insert, delete, replace, final opType;char* opStrings = initial, right, insert, delete, replace, final;/ 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* opPerformed);/ Computes the edit distance be
20、tween 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 reconstructing the answeropType opDone;i
21、nt i, j;d = (int*)malloc(m+1)*sizeof(int*);assert(d != NULL);opPerformed = (opType*)malloc(m+1)*sizeof(opType*);assert(opPerformed != NULL);for (i = 0; i = m; i+) di = (int*)malloc(n+1)*sizeof(int);assert(di != NULL);opPerformedi = (opType*)malloc(n+1)*sizeof(opType);assert(opPerformedi != NULL);/ I
22、nitialize the base cases./ xm and yn are both null strings. Edit distance between them is 0.dmn = 0;opPerformedmn = final;for (i = 0; i m; i+) / 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-) f
23、or (j = 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_CO
24、ST*(xi = 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 = costFor
25、ReplaceOrMove;if (xi != yj)opDone = replace;else opDone = right;if (minValue costForInsert) minValue = costForInsert;opDone = insert;if (minValue costForDelete) minValue = costForDelete;opDone = delete;dij = minValue;opPerformedij = opDone;/ Final answerprintf(Edit Distance = %dn, d00);/ Recover ope
26、rations / print out answer only if the strings arent too long.if (m 75) & (n 75) recoverEdits(x, m, y, n, d, opPerformed);for (i = 0; i = m; i+) free(di);free(opPerformedi);free(d);free(opPerformed); static void recoverEdits(char* x, int m,char* y, int n,int* d, opType* opPerformed) int i = 0; int j
27、 = 0;int k = 0;int a;int opDone;int costSoFar = 0;int stepCost = 0;char* newString;newString = (char*)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./
28、 newString0.k stores everything before the cursor that we have/ changed, and xi.m-1 will store all the parts after the cursor that/ are still the same.strncpy(newString, x, m);printf(n);printf( i, j): %8s | c | Total | z n, Oper);printf(-n);printf(%2d, %2d): %8s | %4d | %4d | , i, j, initial, costSo
29、Far, stepCost);printf(*%sn, x);while (opPerformedij != final) opDone = opPerformedij;switch(opDone) case right:newStringk = xi;i+;j+;stepCost = MOVE_COST;break;case replace:newStringk = yj;i+;j+;stepCost = REPLACE_COST;break;case insert:newStringk = yj;j+;stepCost = INSERT_COST;break; case delete:i+;stepCost = DELETE_COST;break;default: / We should never a follow a pointer to initial or final.printf(ERROR.n);exit(1);break;if (opDone != delete) k+;costSoFar += stepCost; printf(%2d, %2d): %8s |
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1