



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!




UC Berkeley—CS 170 Midterm 2 Lecturer: Gene Myers November 9
Print your name:
(last) (first)
Sign your name:
Write your section number (e.g. 101):
Write your sid:
One page of notes is permitted. No electronic devices, e.g. cell phones and calculators, are permitted. Do all your work on the pages of this examination. If you need more space, you may use the reverse side of the page, but try to use the reverse of the same page where the problem is stated.
You have 80 minutes. The questions are of varying difficulty, so avoid spending too long on any one question.
In all algorithm design problems, you may use high-level pseudocode.
DO NOT TURN THE PAGE UNTIL YOU ARE TOLD TO DO SO.
Problem Score/Points 1 / 2 / 3 / 4 / 5 / Total /
(a) Consider the Lempel-Ziv compression scheme where the dictionary is limited to 8 words and 3 bits are used to encode an index into the dictionary. Recall that the dictionary begins with the empty string as the sole entry with index 0. Give the Lempel-Ziv encoding for the string below and list the words in the dictionary in the order in which they were added to it.
(b) Uncompress the Lempel-Ziv string below under the assumptions that each index is coded with 3 bits and the dictionary holds a maximum of 8 words. Also list, in order, the words in the dictionary.
(c) Consider the alphabet { a, b, c, d, e, f } and assume the characters occur within a file with frequencies .01, .25, .35, .20, .03, .16., respectively. Give the optimal Huffman encoding tree for this situation.
Put a 0 and a 1 label on the pair of edges out of each vertex as you please.
Consider a directed graph G = (V, E) where each edge is labeled with a character from an alphabet Σ, and we designate a special vertex s as the start vertex, and another f as the final vertex. We say that G accepts a string A = a 1 a 2... an if there is a path from s to f of n edges whose labels spell the sequence A. Design an O((|V | + |E|)n) dynamic programming algorithm to determine whether or not A is accepted by G.
(a) Formally define the set of sub-problems you will solve. For v ∈ V and i ∈ [0, n], let: A[v, i] = ∃ path from s to v spelling a 1 a 2... ai.
(b) Give your recurrence for the solution of a given sub-problem in terms of other sub- problems.
A[v, i] =
ORw→v (label(v → w) = ai and A[w, i − 1]) if i > 0 v = s if i = 0
(c) Give a non-recursive pseudo-code specification of the algorithm.
A[s, 0] ←true for v ∈ V − {s} do A[v, 0] ←false for i ← 1... n do for v ∈ V − s do { A[v, i] ← false for w → v ∈ E do if label(w → v) = ai and A[w, i − 1] then A[v, i] ← true } if A[t, n] then print “Accept” else print “Reject”
A palindrome is a word w 1 w 2... wk whose reverse wkwk− 1... w 1 is the same string, e.g. “abbabba”. Consider a string A = a 1 a 2... an. A partitioning of the string is a palindrome partitioning if every substring of the partition is a palindrome. For example, “aba|b|bbabb|a|b|aba” is a palindrome partitioning of “ababbbabbababa”. Design a dynamic programming algo- rithm to determine the coarsest (i.e. fewest cuts) palindrome partition of A.
(a) Formally define the set of sub-problems you will solve. For all 1 ≤ i ≤ j ≤ n we solve two subproblems: P [i, j] = aiai+1... aj is a palindrome (true/false), and C[i, j] = no. of cuts in the best palindrome partition of aiai+1... aj.
(b) Give your recurrence for the solution of a given sub-problem in terms of other sub- problems.
P [i, j] =
ai = aj and P [i + 1, j − 1] if i < j − 1 ai = aj if i = j − 1 true if i = j
C[i, j] = min{ mink∈[i,j−1]{C[i, k] + C[k + 1, j]}, if P [i, j] then 0 else ∞}
(c) Give a non-recursive pseudo-code specification of the algorithm and state its complexity in terms of n.
for i ← 1... n do (P [i, i], C[i, i]) ← (true, 1) for l ← 1... n − 1 do for i ← 1... n − l do { j ← i + l if l > 1 then P [i, j] ← (ai = aj ) and P [i + 1, j − 1] else P [i, j] ← (ai = aj ) C[i, j] ← ∞ for k ← i... j − 1 do C[i, j] ← min{C[i, j], C[i, k] + C[k + 1, j]} if P [i, j] then C[i, j] ← 0 } Best palindrome partition has C[1, n] cuts
The algorithm is O(n^3 ). (This is the complexity you were expected to acheive.)
Suppose that for alphabet Σ the probability of seeing character a is pa. Recall the formula for the entropy of the alphabet E(Σ) =
a∈Σ pa^ log 2 1 pa.^ We asserted in class that the length of a Huffman encoded file is O(nE(Σ)). The essential fact needed to show this is that for a character a, its depth in an optimal Huffman tree is O(log (^) p^1 a ). Prove this with a careful argument (Hint: Consider a node with this frequency and think about what must be true of the frequencies of all the children immediately off the path from this node to the root.)
Suppose that the leaf node v for character a is placed at depth k in an optimal Huffman tree. Let w 0 = v, w 1 , w 2 ,... wk− 1 be the sequence of nodes encountered from v going back to the root of the tree, and let si be the child of wi not on this path. Let p(v) be the probability of the tree rooted at v. The figure illustrates.
Consider sj for any j ≥ 2. Because sj− 1 and wj− 2 are joined before sj is joined to the subtree rooted at wj− 1 it must be that p(sj ) ≥ p(sj− 1 ) and p(sj ) ≥ p(wj− 2 ). Therefore, p(sj ) ≥ (p(sj− 1 ) + p(wj− 2 ))/2 = p(wj− 1 )/2 and p(wj ) = p(wj− 1 ) + p(sj ) ≥ 1. 5 p(wj− 1 ). By induction it then follows that p(wk− 1 ) ≥ 1. 5 k−^2 p(w 1 ). But p(w 1 ) ≥ pa and p(wk− 1 ) = 1, so it must be that pa 1. 5 k−^2 ≤ 1 which implies that k ≤ log 1. 51 /pa + 2 = O(log 1/pa).