Hufman Coding - Efficient Algorithms and Intractable Problems - Solved Exams, Exams of Algorithms and Programming

Main points of this past exam are: Hufman Coding, Optimal Prefix-Free, Variable-Length, Binary Encoding, Optimal Encoding, Binary Tree, Structure Supports, Amortized Running, Running Time, Decreasing Subsequence

Typology: Exams

2012/2013

Uploaded on 04/02/2013

shameem_99
shameem_99 🇮🇳

4.4

(12)

58 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 170 Algorithms
Spring 2009 David Wagner MT2 Soln
Midterm 2 solutions
Please— do not read or discuss these solutions
in the exam room while others are still taking
the exam.
CS 170, Spring 2009, MT2 Soln 1
pf3
pf4

Partial preview of the text

Download Hufman Coding - Efficient Algorithms and Intractable Problems - Solved Exams and more Exams Algorithms and Programming in PDF only on Docsity!

CS 170 Algorithms

Spring 2009 David Wagner MT2 Soln

Midterm 2 solutions

Please— do not read or discuss these solutions

in the exam room while others are still taking

the exam.

Problem 1. [Huffman coding] (25 points)

Suppose we have an alphabet with only five letters, A, B,C, D, E, which occur with the following frequencies:

letter A B C D E frequency 0. 25 0. 05 0. 2 0. 1 0. 4

Use Huffman coding to find the optimal prefix-free variable-length binary encoding of this alphabet.

(a) Draw a binary tree that represents the optimal encoding. Make sure to label the leaves with the letters A, B,C, D, E.

B D

C

A

E

(b) Fill in the table below with the binary encoding of each letter.

letter encoding A 01 B 0000 C 001 D 0001 E 1

Problem 2. [Fill in the blank] (20 points)

Fill in the boxes below with the best answer. You don’t need to justify your answer.

(a) Suppose that a data structure supports an operation ADD, so that any sequence of n ADD’s takes at most O(n lg n) time in the worst case. Then the amortized running time of an ADD operation is O(lg n) , while actual running time of a single ADD operation could be as much as O(n lg n) in the worst case.

(b) Let a 1 , a 2 ,... , an be a sequence of integers. Recall that a subsequence is a subset of these integers taken in order, i.e., ai 1 , ai 2 ,... , aik such that 1 ≤ i 1 < i 2 < · · · < ik ≤ n. A decreasing subsequence is one for which every integer is smaller than previous one, i.e., it is a subsequence ai 1 , ai 2 ,... , aik satisfying ai 1 > ai 2 > ai 3 > · · · > aik. In the longest decreasing subsequence problem, we are given the sequence a 1 , a 2 ,... , an, and we want to compute the length of the longest decreasing subsequence of a 1 , a 2 ,... , an. Example: If we are given the sequence 5, 2 , 4 , 8 , 3 , 40, then the longest decreasing subsequence is 5, 4 , 3. (Other decreasing subsequences include 5, 2 and 8, 3, but they are shorter.) Let’s build a dynamic programming algorithm for this problem. I will give you the definition of the subproblems. We will let L( j) denote the length of the longest decreasing subsequence that starts with

Problem 4. [Rental car scheduling] (30 points)

You’re managing a branch office of Hurtz, the infamous car rental company. You just received a single Tesla Roader (a sporty electric car), and it is proving to be very popular: all sorts of customers have called up to request to reserve the Roadster for a range of days at some point in the future. You have the brilliant idea to run an auction, asking customers to specify a range of days and to bid on how much they are willing to pay to rent the Roadster for those days.

In particular, you want to plan a schedule for the Roadster for the next T days. On each of the next T days, there is a customer who has submitted a bid to rent the Roadster for a certain range of days starting on that day. In particular, for each i ∈ { 1 , 2 ,... , T }, there is a customer ci who has offered to pay P[i] dollars if you will rent him the Roadster for days i, i + 1 , i + 2 ,... , E[i]. (Note that on each day, there is only one customer who wants to start on that day.) If you accept customer ci’s offer, you won’t be able to rent the Roadster out to anyone else during those days. For each customer, you must either accept or reject their bid, with no modifications (for instance, you can’t give them the Roadster for fewer days than requested). You’d like to select a subset of bids that maximizes your total revenue, subject to the constraint that the Roadster can be rented to at most one person on each day. You may assume that E[i] ≤ T for every i.

Your job is to design an efficient dynamic programming algorithm to compute the maximum amount of money you can collect over days 1,... , T , given P[ 1 ..T ], E[ 1 ..T ], and T. The running time of your algorithm should be at most O(T ). Do not show the pseudocode of your algorithm—instead, just answer the following two questions. You do not need to justify your answers.

(a) Prof. Indigo suggests you identify T subproblems and define an array A[ 1 ..T ] so that A[i] holds the solution to the ith subproblem—but then she absent-mindedly walks out of the room and leaves it up to you to work out the rest of the details of the algorithm. With this hint, specify the value of A[i] precisely, in English:

A[i] = The maximum revenue we can make on days i..T by renting out the Roadster only on those days (or some subset of those days).

(b) Show us a recursive expression for A[i] that can be used to obtain a dynamic programming algorithm with the stated running time. (You don’t need to show us the base case(s). You don’t need to justify your answer.) A[i] = max(A[i + 1 ], P[i] + A[E[i] + 1 ]).