Docsity

Assignment 7 Questions - Fundamental Algorithms | CS 231, Assignments of Algorithms and Programming

Toggle sidebar
bg1
CS231 Algorithms Handout # 33
Prof. Lyn Turbak November 27, 2001
Wellesley College
Problem Set 7
Due: Tuesday, December 4
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-nbinary file is just a sequence
of n0sand1s.) 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-nbinary 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) Sbegins with the greedy choice or (2) if Sdoes 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:theoptimal
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 Sfor the whole problem
and show that you can extract the optimal solution Sifor subproblem ifrom the optimal solution
to the whole problem. The proof proceeds by contradiction: assume that there is a b etter solution
S0
ito subproblem iand show that you could construct a better whole solution S0using S0
i.But
that contradicts your original assumption about the optimality of S. So it must be the case that
your other assumption is incorrect: S0
icannot be better than Si.Thatis,eachSiis optimal.
1
pf3
pf4
pf5

Partial preview of the text

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

CS231 Algorithms Handout # 33 Prof. Lyn Turbak November 27, 2001 Wellesley College

Problem Set 7

Due: Tuesday, December 4

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:

  • There are 19 distinct characters in the poem (counting dash, space, and carriage return).
  • Character frequency table: a d e g h i k l m n o r s t u y - ↵ 21 6 11 6 6 14 4 4 17 7 8 2 9 10 1 1 8 31 8
  • Total number of characters: 174
  • There are 16 distinct words in the poem (counting individual dashes, spaces and carriage returns as words).
  • Word frequency table: am and do eggs green ham like not i sam that them you - ↵ 7 2 4 2 2 2 4 3 10 7 3 1 1 8 31 8
  • Total number of words: 95

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]

  1. What is the smallest number of bits k that it would take to represent one character of the poem in a uniform-length binary code?
  2. What is the number of bits it would take to represent a table mapping 7-bit ASCII characters to their equivalent in the binary code? Assume that the table has the following format: (a) 3 bits to represent k. (b) 7 bits to represent the number n of table entries. (c) n table entries, where each entry is the k-bit encoding of a character followed by its 7-bit ASCII character representation.
  3. How many bits total would it take to represent the table followed by the k-bit representations of the contents of the poem?

c. [10]

  1. Using the character frequency table, derive a Huffman code tree for this example, where each symbol is a character.
  2. Make a table showing the codes for the 19 individual characters.
  3. Ignoring the space for encoding the tree/table, how many bits would it take to encode the poem using your table?
  1. 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 tree. How many bits would be required to represent the tree using the following encoding? (a) 16 bits indicating the number of leaves in the tree. (b) A sequence of 8-bit bytes representing a post-order traversal of the tree, where: i. A byte with leading bit 0 stands for a leaf denoting a character represented by the remaining 7 bits. ii. A byte with leading bit 1 stands for n “node” constructors, where n is the number represented by the remaining 7 bits. For example, the tree

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

  1. What is the total number of bits (table + contents) needed to represent the poem using this encoding?

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.

  • The LZ77 encoding should consist of triples of the form (1) back (2) length and (3) character. Assume that the encoding begins with 8 bits indicating the maximum number of bits b needed to encode a single back/length number. Each triple can then be represented by 2 · b + 7 bits.
  • The LZ78 encoding should consist of pairs of the form (1) codeword index and (2) character. Assume that the encoding begins with 8 bits indicating the maximum number of bits b needed to encode a single codeword (index). Each pair can then be represented by b + 7 bits.

Problem Set Header Page Please make this the first page of your hardcopy submission.

CS231 Problem Set 7

Due Tuesday, December 4

Name:

Date & Time Submitted:

Collaborators (anyone you worked with on the problem set):

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.

Part Time Score

General Reading

Problem 1 [10]

Problem 2 [15]

Problem 3 [15]

Problem 4 [25]

Problem 5 [35]

Extra Credit [135]

Total