



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CS231 Algorithms Handout # 33 Prof. Lyn Turbak November 27, 2001 Wellesley College
Reading: Handouts 29 (Greediness), 31 (Compression), 32 (Priority Queues), 34 (Amortization); CLRS Chapter 6 (Heapsort), Chapter 16.1–16.3 (Greediness), Chapter 17 (Amortized Analysis); CLR Chapter 7 (Heapsort), Chapter 17.1–17.3 (Greediness), Chapter 18 (Amortized Analysis)
Sugggested Problems: CLRS 6.1-4, 6.1-5, 6.2-1, 6.3-1, 6.3-3, 6,4-1, 6.4-3, 6.5-6. 6.5-8; 12.1-2; 16.1-1, 16.1-4, 16.2-2, 16.2-5, 16.2-6, 16.3-1, 16.3-7; CLR 7.1-4, 7.1-5, 7.2-1, 7.3-1, 7.3-3, 7.4-1, 7.4-2, 7.5-3, 7.5-6; 13.1-2, 17.1-1, 17.1-3, 17.2-2, 17.2-3, 17.2-5, 17.2-6, 17.3-1, 17.3-7;
Required Problems: You should write up and turn in the solutions to the problems listed below: The points awarded per problem are given in brackets.
Problem 1 [10]: More Snake Oil Snake Oil Software’s CEO Kim Priest has just announced an amazing compression program that is guaranteed to compress every binary file by a factor of two, effectively doubling the size of your computer’s memory. (A length-n binary file is just a sequence of n 0 s and 1 s.) You have been asked to review this program.
a. [5] Based on a simple counting argument, explain why the program cannot possibly work as advertised. Hint: How many length-n binary files are there?
b. [3] Extend the reasoning in part a to explain why any compression technique that shrinks some files must increase the size others.
c. [2] Given the problems raised by parts a and b, explain why compression techniques are still valuable in practice.
Problem 2 [15]: Professor Midas Do CLRS Exercise 16.2-4/CLR Exercise 17.2-4. Formally prove that your algorithm is optimal. As shown in Handout 29, CLRS Chapter 16, and CLR Chapter 17 there are two parts to a proof that a greedy algorithm is optimal:
a. [7] Show that the algorithm exhibits the greedy choice property: an optimal solution can begin with the greedy choice. The strategy for proving this is as follows. Assume you a given an optimal solution S. Then show that (1) S begins with the greedy choice or (2) if S does not begin with the greedy choice, then replacing whatever it begins with by the greedy choice leads to another optimal solution. (Note: there may be many optimal solutions.)
b. [8] Show that the problem exhibits the optimal substructure property: the optimal solution for the whole problem can be derived from the optimal solution for the parts. This is usually shown as follows. Assume that you are given an optimal solution S for the whole problem and show that you can extract the optimal solution Si for subproblem i from the optimal solution to the whole problem. The proof proceeds by contradiction: assume that there is a better solution S i′ to subproblem i and show that you could construct a better whole solution S′^ using S i′. But that contradicts your original assumption about the optimality of S. So it must be the case that your other assumption is incorrect: S i′ cannot be better than Si. That is, each Si is optimal.
Problem 3 [15]: When a Queue Made of Two Lists Seems So New To You – That’s Amortization! Below is an implementation of a mutable first-in-first-out (FIFO) queue that represents the queue as a pair of two lists, labeled front and back:
Empty-Queue() return new record {front = EmptyList(), back = EmptyList()}
Empty-Queue?(Q) return EmptyList?(front[Q]) and EmptyList?(back[Q])
Enq(x, Q) back[Q] ← Prepend(x, back[Q])
Deq(Q) if EmptyList?(front[Q]) then front([Q]) ← Reverse(back[Q]) B Assume a linear-time Reverse back([Q]) ← EmptyList() if EmptyList?(front[Q]) then error "empty queue" else let h = Head(front[Q]) in front[Q] ← Tail(front[Q]) return h
a. [5] Show the state of the queue (i.e., the front and back lists) after each of the operations in the following sequence: q ← Empty-Queue() Enq(1,q) Enq(2,q) Enq(3,q) Deq(q) Enq(4,q) Enq(5,q) Deq(q) Deq(q) Deq(q) Enq(6,q) Deq(q) Deq(q)
b. [5] Using the banker’s (accounting) method, argue that the amortized cost of Enq and Deq are each 0(1).
c. [5] Using the physicist’s (potential) method, argue that the amortized cost of Enq and Deq are each 0(1).
For simplicity, all letters are lower case, and all punctuation has been omitted except for dashes (written - ) spaces (written ), and carriage returns (written ↵). We will refer to the above 9 lines as “the poem”. We will consider the number of bits needed to represent the poem under various compression schemes. Here are some useful facts about the poem:
a. [1] What is the number of bits that it would take to represent the poem if each character is represented by an 8-bit ASCII character?
b. [4]
c. [10]
a
b
c (^) d
e
has the post-order representation a b c d ◦ e ◦ ◦ ◦ (where ◦ denotes the post-order node constructor) and yields a table with the following bytes:
00000000 00000101 ︸ ︷︷ ︸ 5 leaves
01100001 ︸ ︷︷ ︸ a (97)
01100010 ︸ ︷︷ ︸ b (98)
01100011 ︸ ︷︷ ︸ c (99)
01100100 ︸ ︷︷ ︸ d (100)
10000001 ︸ ︷︷ ︸ 1 ◦
01100101 ︸ ︷︷ ︸ e (101)
10000011 ︸ ︷︷ ︸ 3 ◦s
d. [10] Repeat part c, except use the words of the poem (rather than its characters) as symbols in the Huffman code. Assume that the Huffman tree is represented as in part c except that the leaves (which are now strings, not just characters) are represented as (1) an 8-bit byte with leading bit 0 and 7 bits encoding the number of characters n in the string and (2) 7n bits representing the n 7-bit ASCII encodings of the characters in the string.
e. [10] Use one of (1) the LZ77 encoding or (2) the LZ78 encoding (you choose!) described in Handout #31 to encode the poem, and indicate the total number of bits taken by your representation.
Problem Set Header Page Please make this the first page of your hardcopy submission.
In the Time column, please estimate the time you spend on the parts of this problem set. Please try to be as accurate as possible; this information will help me design future problem sets. I will fill out the Score column when grading your problem set.