Practice Assignment 8 - Fundamental Algorithms | CS 231, Assignments of Algorithms and Programming

Material Type: Assignment; Class: Fundamental Algorithms; Subject: Computer Science; University: Wellesley College; Term: Spring 2009;

Typology: Assignments

Pre 2010

Uploaded on 08/18/2009

koofers-user-a80
koofers-user-a80 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Assignment 8
Computer Science 231
Spring 2009
Due: Friday, April 17
Reading. CLRS Sections 16.1, 16.2, 16.3, pages 370 -- 392
CLR Sections 17.1, 17.2, 17.3, pages 330 -- 344
Note. To complete Exercises 16.2-1, 16.2-2, and 16.2-3, you will need to read section
16.2 of CLRS (section 17.2 of CLR).
Exercise 16.2-1. Prove that the fractional knapsack problem has the greedy-choice
property.
Exercise 16.2-2. Give a dynamic-programming solution to the 0-1 knapsack problem that
runs in O(n W) time, where n is the number of items and W is the maximum weight of the
items that the thief can put in his knapsack.
Exercise 16.2-3. Suppose that in a 0-1 knapsack problem, the order of the items when
sorted by increasing weight is the same as their order when sorted by decreasing value.
Give an efficient algorithm to find an optimal solution to this variant of the knapsack
problem, and argue that your algorithm is correct.
Exercise 16.2-4. Professor Midas drives an automobile from Newark to Reno along
Interstate 80. His car's gas tank, when full holds enough gas to travel n miles, and his map
gives the distance between gas stations on his route. The processor wishes to make as few
gas stops as possible along the way. Give an efficient method by which Professor Midas
can determine at which gas stations he should stop, and prove that your strategy yields an
optimal solution.
Exercise 8-5. (Adapted from Abelson and Sussman, Structure and Interpretation of
Computer Programs, p. 125).
Here we consider various approaches to compressing the following 1950s style song
lyrics:
get a job>
sha na na na na na na na na>
get a job>
sha na na na na na na na na>
wah yip yip yip yip yip yip yip yip>
sha boom>
For simplicity, the only characters are lower-case letters, spaces, and line-ending carriage
returns (written >). Here are some useful facts about the lyrics:
Total number of characters: 121
Character frequency table:
a
b
e
g
h
i
j
m
n
o
p
s
t
w
y
>
22
3
2
2
4
8
2
1
16
4
8
3
2
1
8
29
6
pf2

Partial preview of the text

Download Practice Assignment 8 - Fundamental Algorithms | CS 231 and more Assignments Algorithms and Programming in PDF only on Docsity!

Assignment 8

Computer Science 231 Spring 2009 Due: Friday, April 17 Reading. CLRS Sections 16.1, 16.2, 16.3, pages 370 -- 392 CLR Sections 17.1, 17.2, 17.3, pages 330 -- 344 Note. To complete Exercises 16.2-1, 16.2-2, and 16.2-3, you will need to read section 16.2 of CLRS (section 17.2 of CLR ). Exercise 16.2-1. Prove that the fractional knapsack problem has the greedy-choice property. Exercise 16.2-2. Give a dynamic-programming solution to the 0-1 knapsack problem that runs in O(n W) time, where n is the number of items and W is the maximum weight of the items that the thief can put in his knapsack. Exercise 16.2-3. Suppose that in a 0-1 knapsack problem, the order of the items when sorted by increasing weight is the same as their order when sorted by decreasing value. Give an efficient algorithm to find an optimal solution to this variant of the knapsack problem, and argue that your algorithm is correct. Exercise 16.2-4. Professor Midas drives an automobile from Newark to Reno along Interstate 80. His car's gas tank, when full holds enough gas to travel n miles, and his map gives the distance between gas stations on his route. The processor wishes to make as few gas stops as possible along the way. Give an efficient method by which Professor Midas can determine at which gas stations he should stop, and prove that your strategy yields an optimal solution. Exercise 8-5. ( Adapted from Abelson and Sussman, Structure and Interpretation of Computer Programs, p. 125). Here we consider various approaches to compressing the following 1950s style song lyrics: get a job> sha na na na na na na na na> get a job> sha na na na na na na na na> wah yip yip yip yip yip yip yip yip> sha boom> For simplicity, the only characters are lower-case letters, spaces, and line-ending carriage returns (written >). Here are some useful facts about the lyrics: Total number of characters: 121 Character frequency table: a b e g h i j m n o p s t w y > 22 3 2 2 4 8 2 1 16 4 8 3 2 1 8 29 6

Assignment 8 Page 2 Computer Science 231 a. There are 17 distinct characters in the song (counting space and carriage return). What is the smallest number of bits that it would take to represent one character in a fixed-length binary code? How many bits would it take to encode the entire song using this fixed-length code? b. Using the character frequency table, derive a Huffman code tree for this example. Make a table showing the codes for the 17 individual characters. Ignoring the space for encoding the tree/table, how many bits would it take to encode the song? c. Some encoding of the tree/table must be sent along with the encoded message to allow it to be decoded. There are many ways to encode the table. Let’s assume it is encoded as a sequence of bindings followed by a distinguished 8-bit byte that separates the table from the message. Each binding consists of an 8-bit ASCII representation of the character being encoded, followed by an 8-bit binary number indicating the length n of the code for the character, followed by the n bits that are the code for the character. For example, if the character ‘a’ had the five-bit code 10011, then the binding would consist of the following bits: ASCII ‘a’ Binary 5 5 - bit code 01100001 00000101 10011 Based on this encoding of the table, how many bits would be required to encode the song, including the encoding of the table and the byte separating the table from the message?