Unformatted text preview:

Introduction to Algorithms November 7, 2005 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik D. Demaine and Charles E. Leiserson Handout 22 Problem Set 7 MIT students: This problem set is due in lecture on Monday, November 14, 2005. There will be two homework labs for this problem set, one held 6–8 P.M on Wednesday, November 9, 2005 and one held 2–4 P.M. on Sunday, November 13, 2005. Reading: Chapter 15, 16.1–16.3, 22.1, and 23. Problem 7-1 is mandatory. Failure to turn in a solution will result in a serious and neg-ative impact on your term grade! Both exercises and problems should be solved, but only the problems should be turned in. Exercises are intended to help you master the course material. Even though you should not turn in the exercise solutions, you are responsible for material covered in the exercises. Mark the top of each sheet with your name, the course number, the problem number, your recitation section, the date and the names of any students with whom you collaborated. Please staple and turn in your solutions on 3-hole punched paper. You will often be called upon to “give an algorithm” to solve a certain problem. Your write-up should take the form of a short essay. A topic paragraph should summarize the problem you are solving and what your results are. The body of the essay should provide the following: 1. A description of the algorithm in English and, if helpful, pseudo-code. 2. At least one worked example or diagram to show more precisely how your algorithm works. 3. A proof (or indication) of the correctness of the algorithm. 4. An analysis of the running time of the algorithm. Remember, your goal is to communicate. Full credit will be given only to correct solutions which are described clearly. Convoluted and obtuse descriptions will receive low marks. Exercise 7-1. Do Exercise 15.4-3 on page 356 of CLRS. Exercise 7-2. Do Exercise 16.1-3 on page 379 of CLRS. Exercise 7-3. Do Exercise 16.3-2 on page 384 of CLRS. Exercise 7-4. Do Exercise 22.1-5 on page 530 of CLRS. Exercise 7-5. Do Exercise 23.1-5 on page 566 of CLRS. Exercise 7-6. Do Exercise 23.2-4 on page 574 of CLRS. Exercise 7-7. Do Exercise 23.2-5 on page 574 of CLRS.2 Handout 22: Problem Set 7 Problem 7-1. Edit distance In this problem you will write a program to compute edit distance. This problem is mandatory. Failure to turn in a solution will result in a serious and negative impact on your term grade! We advise you to start this programming assignment as soon as possible, because getting all the details right in a program can take longer than you think. Many word processors and keyword search engines have a spelling correction feature. If you type in a misspelled word x, the word processor or search engine can suggest a correction y. The correction y should be a word that is close to x. One way to measure the similarity in spelling between two text strings is by “edit distance.” The notion of edit distance is useful in other fields as well. For example, biologists use edit distance to characterize the similarity of DNA or protein sequences. The edit distance d(x, y) of two strings of text, x[1 . . m] and y[1 . . n], is defined to be the minimum possible cost of a sequence of “transformation operations” (defined below) that transforms string 1x[1 . . m] into string y[1 . . n]. To define the effect of the transformation operations, we use an auxiliary string z[1 . . s] that holds the intermediate results. At the beginning of the transformation sequence, s = m and z[1 . . s] = x[1 . . m] (i.e., we start with string x[1 . . m]). At the end of the transformation sequence, we should have s = n and z[1 . . s] = y[1 . . n] (i.e., our goal is to transform into string y[. . n]). Throughout the tranformation, we maintain the current length s of string z, as well as a cursor position i, i.e., an index into string z. The invariant 1 � i � s + 1 holds at all times during the transformation. (Notice that the cursor can move one space beyond the end of the string z in order to allow insertions at the end of the string.) Each transformation operation may alter the string z, the size s, and the cursor position i. Each transformation operation also has an associated cost. The cost of a sequence of transformation operations is the sum of the costs of the individual operations on the sequence. The goal of the edit-distance problem is to find a sequence of transformation operations of minimum cost that transforms x[1 . . m] into y[1 . . n]. There are five transformation operations: 1Here we view a text string as an array of characters. Individual characters can be manipulated in constant time.3 Handout 22: Problem Set 7 Operation Cost Effect left 0 If i = 1 then do nothing. Otherwise, set i � i − 1. right 0 If i = s + 1 then do nothing. Otherwise, set i � i + 1. replace 4 If i = s+1 then do nothing. Otherwise, replace the character under the cursor by another character c by setting z[i] � c, and then incrementing i. delete 2 If i = s+1 then do nothing. Otherwise, delete the character c under the cursor by setting z[i . . s] � z[i + 1 . . s + 1] and decrementing s. The cursor position i does not change. insert 3 Insert the character c into string z by incrementing s, set- ting z[i + 1 . . s] � z[i . . s − 1], setting z[i] � c, and then incrementing index i. As an example, one way to transform the source string algorithm to the target string analysis is to use the sequence of operations shown in Table 1, where the position of the underlined char-acter represents the cursor position i. Many other sequences of transformation operations also transform algorithm to analysis—the solution in Table 1 is not unique—and some other solutions cost more while some others cost less. Operation z Cost Total initial string algorithm 0 0 right algorithm 0 0 right algorithm 0 0 replace by y alyorithm 4 4 replace by s alysrithm 4 8 replace by i alysiithm 4 12 replace by s alysisthm 4 16 delete alysishm 2 18 delete alysism 2 20 delete alysis 2 22 left alysis 0 22 left alysis 0 22 left alysis 0 22 left alysis 0 22 left alysis 0 22 insert n anlysis 3 25 insert a analysis 3 28 Table 1: Transforming algorithm into analysis (a) It is possible to transform algorithm to analysis without using the “left” oper-ation. Give a sequence of operations in the style of Table 1 that has the


View Full Document

MIT 6 046J - Problem Set 7

Download Problem Set 7
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Problem Set 7 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Problem Set 7 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?