CSCI 256
Design and analysis of algorithms
Assignment 7

Due Friday, 4/6/2001


Only turn in problems from the second section.

Practice problems:

  1. Problem 17.2-4 on page 337 of the text.

  2. Problem 17.3-2 on page 344 of the text.

  3. Problem 18.1-3 on page 360 of the text.

Problems to be turned in:

  1. Optimal Merging: Let F1, F2, ..., Fn be files with lengths l1, ..., ln. We would like to merge all of the files together to make a single file, but we are only allowed to merge two at a time. For example if we had 3 files, we could merge the first two, and then merge the last with the new merged file. Alternatively we could have started by merging the last two or the first and third. The cost of merging two files is m+n if the files have length m and n. If the three files have lengths 10, 20, and 30, then the first merging the first two and then the third gives a total cost of 90 steps (30 for merging the first two and then another 60 for merging that new file with the third file), while merging the last two first gives a total cost of 110. In this problem I'd like you to develop an optimal greedy algorithm for merging collections of files. Hint: The development of this algorithm is almost identical to that of Huffman codes, so you should review that algorithm first.

    1. For each ordering of merges for files, we can define a binary merge tree. The leaves should represent the starting files, and should be labelled with the lengths of the files. The interior nodes should represent the result of merging the child nodes (and should be labelled with the cost of merging the children). For example, if files F1 and F2 are merged at one step of the algorithm, then the corresponding leaves should share the common parent node, N, which should be labelled with the sum of the lengths of those files. If you do this properly, the root should be labelled with the sum of the lengths of all of the files.

      For each of the orderings of merging the three files of lengths 10, 20, and 30, draw the corresponding binary merge tree.

    2. By examining the three trees computed in the previous part, create a formula computing the total cost of merging the files. This formula should involve the lengths of the files and the total depth of the leaf representing each file. Hint: Look at the trees and corresponding formulas for Huffman codes.

    3. Describe a greedy algorithm for finding the optimal merge order for a collection of n files with lengths l1, ..., ln. You need not prove that the algorithm gives the optimal merge order (though it would be nice if you did understand why!).

  2. Problem 18.2-2 on page 363 of the text.


Back to:

  • CS256 home page
  • Kim Bruce's home page
  • CS Department home page
  • kim@cs.williams.edu