This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: Introduction to Algorithms Day 29 Massachusetts Institute of Technology 6.046J/18.410J Singapore-MIT Alliance SMA5503 Professors Erik Demaine, Lee Wee Sun, and Charles E. Leiserson Handout 28 Problem Set 8 Solutions MIT students: This problem set is due in lecture on Day 29 . Reading: Chapter 15, 16, 23 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 by the exercises. Mark the top of each sheet with your name, the course number, the problem number, your recitation instructor and time, the date, and the names of any students with whom you collaborated. MIT students: Each problem should be done on a separate sheet (or sheets) of three-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 your essay should provide the following: 1. A description of the algorithm in English and, if helpful, pseudocode. 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. Graders will be instructed to take off points for convo- luted and obtuse descriptions. 2 Handout 28: Problem Set 8 Solutions Exercise 8-1. Do exercise 15.4-4 on page 356 of CLRS Solution: When computing a particular row of the -table, only the previous row is needed. Only two rows need to be kept in memory at a time. . If , compute the LCS-L ENGTH otherwise compute LCS-L ENGTH . In either case, use the method given in Exercise ?? to use space. Actually only one row need be kept in memory at a time. When an element of the current row is computed, it should be stored in a temporary variable. After the next element is computed, the previous element may be placed in the row. Exercise 8-2. Do exercise 16.2-2 on page 384 of CLRS. Solution: represents the total value that can be taken from the first items when the knapsack can hold . Exercise 8-3. Do exercise 16.2-3 on page 384 of CLRS. Solution: Go greedy. Take the item with maximum value. This item also has the smallest weight and therefore, the largest ratio. One wouldn’t ever take something besides the optimal choice because the optimal choice weighs at most as much and is worth as least as much as any other item. Exercise 8-4. Do exercise 23.2-2 on page 573 of CLRS....
View Full Document
- Fall '04