The input to this problem is a pair of strings A = a1 · · · am and B = b1 · · · bn. The goal is to convert A into B as cheaply as possible. The rules and cost are defined as follow. For a cost of 3 you can delete any letter. For a cost of 4 you can insert a letter in any position. For a cost of 5 you can replace any letter by any other letter. For example, you can convert A = abcabc to B = abacab via the following sequence: abcabc at a cost of 5 can be converted to abaabc, which at cost of 3 can be converted to ababc, which at cost of 3 can be converted to abac, which at cost of 4 can be converted to abacb, which at cost of 4 can be converted to abacab. Thus the total cost for this conversion would be 19. This is probably not the cheapest possible conversion. (Note that the Unix diff command essentially solves this problem.)
(a) Give a dynamic programming algorithm to solve this problem.
(b) Apply your algorithm in (a) to convert A = abcabc to B = abacab with the minimum total cost. Show all your work
ANSWER:
Construct a table T where each entry T[i, j] = cxy(i, j). Since each value
of cxy(i, j) only depends on cxy(i , j� )
由留学作业帮www.homeworkhelp.cc整理编辑
-
where i � i and j� � j, we can compute the entries of T row by row
- EDIT-DISTANCE (x[1…m], y[1…n]) 1 T[0, 0] � 0 2 for i � 1 to n 3 for j � 1 to m ⎧ T[i − 1, j − 1] if i > 0 and j > 0 and x[i − 1] = y[j − 1] ⎧� T[i − 1, j − 1] + 4 if i > 0 and j > 0 4 do T[i, j] � min ⎧⎧ T[i − 1, j] + 2 if i > 0 T[i, j − 1] + 3 if j > 0 5
return T[n, m]
The running time of this algorithm is �(mn). This algorithm requires �(mn) space.
SORRY I ONLY SURE ABOUT FIRST ONE THAT’S WHY I DO THAT SORRY
IF U NEED ANY HELP PLZ LEAVE A COMMENT !!
AND PLZZ DO THUMBS UP IT’S REALLY URGENT. THANK YOU