This Study Resource Was: ECS 122A Midterm Exam I (April 25, 2014) - Solutions
This Study Resource Was: ECS 122A Midterm Exam I (April 25, 2014) - Solutions
This Study Resource Was: ECS 122A Midterm Exam I (April 25, 2014) - Solutions
3. (30 points) Solve the following recurrence relations. Give your answer in Θ(·) notation. Assume
that T (n) ∈ Θ(1) for sufficiently small n.
(a) T (n) = 2 · T (n − 2)
Answer: By the iteration
m
T (n) = 2 · T (n − 2) = 22 T (n − 2 · 2) = · · · = 2k T (n − 2 · k) = Θ(2k ) = Θ(2n/2 )
er as
co
√
eH w
n
(b) T (n) = 2 · T 4 + 10 n
o.
Answer. Since √ √
10 n n
rs e log
n 4 2
= 1/2 = 10.
n
ou urc
√
By case 2 of the master theorem, we have T (n) = Θ(nlog4 2 lg n) = Θ( n lg n).
4. (20 points) Suppose we use Huffman coding to compress a 50,000-symbol file containing 5
o
different symbols, each of which occurs equally often. What will the output size be, measured
aC s
5. (30 points) Consider a set of S of n ≥ 2 distinct numbers given in unsorted order. Each of the
following problem asks you to give an algorithm to determine two distinct numbers x, y ∈ S
ed d
satisfying a stated condition. In as few words as possible, by using algorithms from lectures
ar stu
and homeworks as subroutines (no pseduocodes necessary), describe your algorithm and justify
its running time.
(a) In O(n) time, determine x, y ∈ S such that |x − y| ≥ |w − z| for all w, z ∈ S.
sh is
https://www.coursehero.com/file/22386754/midtermIsol2014/
6. (30 points) Consider the following algorithm that given an array A[1, 2, . . . , n] and a value x
checks whether there is an index i such that A[i] = x. Assume for simplicity that n is a power
of 2.
Initially, we call Find(A,0,n,x). When we call Find(A,start,end,x), we get TURE if and
only if there is an index i such that start < i ≤ end and A[i] = x.
Find(A,start,end,x)
if end - start == 1
if A[end] == x then
return TRUE
else
return FALSE
endif
else
middle = start + ( end - start )/2
return Find(A,start,middle,x) OR Find(A,middle,end,x)
endif
m
er as
co
eH w
(a) Write the recurrence relation that defines the running time of Find(A,0,n,x).
(b) Solve it with the master method.
o.
rs e
Answer: (a) The important part of this question is figuring out what the OR operation does
ou urc
in the worst case. If the first FIND returns FALSE, then the second FIND must be run to
determine the final value returned. In the worst case, both FIND functions are executed. The
if-else statement is a constant time operation for each iterations
o
T (n) = 2T (n/2) + c
aC s
vi y re
https://www.coursehero.com/file/22386754/midtermIsol2014/