Docsity

Midterm Exam Problems - Algorithm Design, Analysis and Implementation | CS 58000, Exams of Computer Science

Toggle sidebar
bg1
CS58000: Midterm Solution Sketch
Problem 1 (Graded by Yongwook Choi)
(a) (5 points) Which is asymptotically smaller: n1+ 1
log nor nlog log n?
n1+ 1
log n= 2log{n1+ 1
log n}= 2log n+1 = 2n.
For all n≥16, 2n≤nlog log n.
Thus, n1+ 1
log nis asymptotically smaller.
(b) (5 points) Solve the recurrence: T(n) = 2√nT (√n) + n. Solve up to
constant factors, and assume constant base cases.
By dividing both sides by n, we get
T(n)
n= 2T(√n)
√n+ 1.
Let A(n) = T(n)
n, then
A(n)=2A(√n)+1.
A(n)=2A(n1
2)+1
= 22A(n1
22)+2+1
= 23A(n1
23)+22+ 2 + 1
·
·
=
log log n
X
i=0
2i
=2log log n+1 −1
2−1=O(log n).
Therefore, T(n) = O(nlog n).
1
pf3
pf4
pf5

Partial preview of the text

Download Midterm Exam Problems - Algorithm Design, Analysis and Implementation | CS 58000 and more Exams Computer Science in PDF only on Docsity!

CS58000: Midterm Solution Sketch

Problem 1 (Graded by Yongwook Choi)

(a) (5 points) Which is asymptotically smaller: n1+^ log^1 n or n log log n?

n1+^ log^1 n = 2log{n

1+ (^) log^1 n (^) } = 2log^ n+1^ = 2n. For all n ≥ 16, 2n ≤ n log log n. Thus, n1+^ log^1 n is asymptotically smaller.

(b) (5 points) Solve the recurrence: T (n) = 2

nT (

n) + n. Solve up to constant factors, and assume constant base cases.

By dividing both sides by n, we get T (n) n

T (

n) √ n

Let A(n) = T^ ( nn ), then A(n) = 2A(

n) + 1.

A(n) = 2 A(n

(^12) ) + 1 = 22 A(n

1 (^22) ) + 2 + 1 = 23 A(n

1 (^23) ) + 2^2 + 2 + 1 · ·

=

log log∑ n

i=

2 i

2 log log^ n+1^ − 1 2 − 1

= O(log n).

Therefore, T (n) = O(n log n).

(c) (5 points) Solve the recurrence: T (n) = T (n/2) + n log n. Solve up to constant factors, and assume constant base cases.

Use the master theorem as given in the text. We have a = 1, b = 2, and f (n) = n log n. f (n) = n log n = Ω(nlog^2 1+)

for a positive constant  = 0.5, and

af

( (^) n b

= f

( (^) n 2

n 2

log n 2

≤ cf (n) = cn log n

for c = 12 and all n. Therefore, T (n) = Θ(f (n)) = Θ(n log n).

(d) Given an array A of n elements we define the majority element of A as one which occurs more than n/2 times. Assume that the only operation allowed on the array elements is equality checking, i.e., given two array indices i and j, we can check whether A[i] = A[j] or not.

  1. (5 points) Assuming that a majority element exists in S, give a Las Vegas randomized algorithm to find the majority element. Analyze the number of operations on average that it takes to find the majority element.
  2. (5 points) Suppose you don’t know whether a majority element exists or not. Give a Monte-Carlo algorithm that will output the majority element if it exists with high probability (i.e., with probability at least 1 − 1 /n). How many operations are needed?
  3. Randomly select an element. Then, check whether it is the majority element or not (by comparing randomly selected element with all the other elements and counting the number of occurrences.) Repeat this until the majority element is found. In each repetition, at most n − 1 comparisons are needed. The probability that the majority element is found is at least 1/2. Thus, the number of operations on average is 2(n − 1).
  4. Repeat the same steps as above log n times. The probability that this algorithm fails to output the majority element even if it exists is at most ( 1 2

)log n

n

The number of operations needed is at most (n − 1) log n.

Let X be the random variable denoting the number of edges across the cut. Then X =

e∈E

Xe.

E[X] = E

[

e∈E

Xe

]

e∈E

E[Xe]

e∈E

m 2

(b) Repeat the algorithm in part (a) until there are m/2 edges across the cut. Let pi be the probability that there are i edges across the cut. ∑^ m

i=

pi · i = m 2 m/ ∑ 2 − 1

i=

pi · i +

∑^ m

i=m/ 2

pi · i =

m 2 m/ ∑ 2 − 1

i=

pi ·

( (^) m 2

∑^ m

i=m/ 2

pi · m ≥ m 2

( (^) m 2

) m/ ∑^2 −^1

i=

pi + m

∑^ m

i=m/ 2

pi ≥ m 2

( (^) m 2

∑^ m

i=m/ 2

pi

 (^) + m

∑^ m

i=m/ 2

pi ≥ m 2

( (^) m 2

∑^ m

i=m/ 2

pi

∑^ m

i=m/ 2

pi ≥

m + 2

Therefore, the expected number of iterations is m 2 + 1. It takes O(n) time to randomly assign the vertices to two sets and O(m) time to

check if there are m/2 edges across the cut. Hence, the total expected running time is O(m(m + n)).

Problem 4 (Graded by Yongwook Choi)

Suppose you are the “algorithmatician” of your company and the manager comes to you with the following problem. The company has to buy n dif- ferent software products. Due to various constraints, the company can only buy these software products at the rate of at most one per month. Each soft- ware is currently selling for a price of $100. However, they are all becoming more expensive according to exponential growth curves: in particular, the cost of software j increases by a factor rj > 1 each month, where rj is a known parameter. This means that if software j is purchased t months from now, it will cost 100(rj )t. It is given that all price growth rates are distinct: that is, ri 6 = rj for software i 6 = j (even though at the start they all have the same price of $100.) The question the manager poses for you is this: Given that the company can only buy at most one software product a month, in which order should it buy the products so that the total amount of money spent is as small as possible? In particular he has the following questions: (i) (3 points) Consider the following example to get started. Suppose r 1 = 2, r 2 = 3, and r 3 = 4. What is the best order to buy the three products and why? An easy calculation of the costs show that decreasing order is the best.

(ii) (7 points) Give a greedy algorithm to find the optimal order assuming that we have n products to buy and the growth rate of product i is ri, 1 ≤ i ≤ n. What is the time complexity of your algorithm? Sort in decreasing order. Time = Θ(n log n).

(iii) (10 points) Prove that your greedy algorithm always gives the optimal ordering. Let G = {g 1 , g 2 ,... , gn} be the greedy solution and let O = {f 1 , f 2 ,... , fn} be an optimal solution. Let k be the first index such that gk 6 = fk. Then, by the decreasing order, gk > fk. Let gk = fm for some m > k. We exchange fk and fm in O to get a solution O′^ which agrees with greedy in one more index. We now show that O′^ is no worse than O. cost(O) − cost(O′) = 100(f (^) kk + f (^) mm − f (^) mk − f (^) km ). = 100(f (^) kk − f (^) mk + f (^) mm − f (^) km )

Problem 5 (Graded by Gopal Pandurangan)

We are a given a path graph G = (V, E), i.e., G is a simple path v 1 , v 2 ,... , vn, where n = |V |. Each node vi has an associated weight wi. An independent set of G is a subset of V such that no two nodes in the subset are joined by an edge. Give a dynamic programming algorithm to find an independent set in G whose total weight (i.e., the sum of the weights of the vertices cho- sen in the independent set) is the largest. Note that you should first give a recursive formulation of the dynamic programming solution, clearly ex- plaining your notation. Then describe the algorithm (also give pseudocode) and analyze its running time. Your algorithm should run in time that is polynomial in n, independent of the values of the weights (in other words, strongly polynomial, not pseudopolynomial). Your algorithm should output the optimal set and not just its value. (10 points for the formulation and 10 points for algorithm description and analysis.)

Let fi denote the maximum weight independent set for the path graph v 1 ,... , vi. Recursive formulation: f 0 = 0 and f 1 = w 1. fi = max{wi + fi− 2 , fi− 1 }, i ≥ 2. The first choice corresponds to in- cluding vertex vi in the optimal solution and the second corresponds to not including vi. Optimal substructure property guarantees correctness. This can be easily turned into a DP algorithm. To find the optimal set, maintain an additional array A, such A[i] = 1 if vi is included in fi, and 0 otherwise. To output the maximum independent set, look at A[n] and trace backwards.