



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Exam; Class: Computer Algorithms I; Subject: Mathematical Computer Science; University: University of Illinois - Chicago; Term: Spring 2012;
Typology: Exams
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Problem 1 (35 points) Bubble sort is the following algorithm:
1: n = A.length 2: for i = 1 → n − 1 do 3: for j = n downto i + 1 do 4: if A[j] < A[j − 1] then 5: exchange A[j] with A[j − 1] 6: end if 7: end for 8: end for
Use the following loop invariant for the loop in lines 3-7:
At the beginning of each iteration of the for loop in lines 3-7, the element A[j] is the smallest among A[j... n].
(a) Prove this loop invariant is correct (initialization and maintenance.)
Answer. We check initialization and maintenance.
Problem 2 (20 points) Show that the solution of T (n) = T (n/3) + 2 is O(log n). To get full credit, you must clearly state what you are doing. Don’t just write a bunch of inequalities without a few sentences of explanation.
Answer. Substitution Method To show this, we need to find a constant d such that T (n) ≤ d log 2 n. (You could use any base you like, I just chose base two.) We will make the computations with d as a variable and then at the end decide what d should be.
T (n) = T (n/3) + 2
≤ d log 2
(n
3
= d log 2 n − d log 2 3 + 2.
We win if −d log 2 3 + 2 is negative since then T (n) ≤ d log 2 n. So
−d log 2 3 + 2 ≤ 0 2 ≤ d log 2 3 2 log 2 3
≤ d.
So choose a d like d = 10. The computation then shows T (n) ≤ 10 log 2 n which implies T (n) = O(log n).
Master Method. T (n) = aT (n/b) + f (n) so a = 1, b = 3, and f (n) = 2. The master method we need to compare
nlogb^ a^? f (n) nlog^3 1? 2 n^0? 2 1? 2
For the question mark, we can insert Θ so the second case of the master method applies. The answer is then Θ(nlogb^ a^ log n) = Θ(nlog^2 1 log n) = Θ(n^0 log n) = Θ(log n).
Problem 3 (20 points) Consider flipping a fair coin 100 times. What is the expected number of runs of 6 heads observed in the sequence? (A run of 7 heads is counted twice since it is made up of two runs of 6 heads.) Hint: Define indicator random variables to mark if a run of 6 starts at a particular position. To get full credit, you must clearly state the indicator random variables you are using and show the computation with them.
Answer. Let Xi be the indicator random variable for a run of 6 heads starting at the ith flip.
Xi =
1 i, i + 1, i + 2, i + 3, i + 4, i + 5 are all heads 0 else
Then if X is the expected number of runs of 6,
i=
Xi
i=
E[Xi].
(Side note: these variables are dependent upon one another since for example X 5 and X 6 will be true and false depending mostly on the same flips. Thus P[X 5 = 1 or X 6 = 1] 6 = P[X 5 ] + P[X 6 ] but the linearity of expectation does not care about dependence.)
We need to compute the expected value of Xi, which is the probability 6 heads come up starting from i. Thus
E[Xi] =
Thus
i=
E[Xi] =
Problem 5 (15 points) This question asks you to determine if two functions are O, Θ, Ω, o, ω of each other. For each question, circle the entries among O, Θ, Ω, o, ω that can be inserted in for the “?” to make the statement true (more than one could apply.) You just need to circle the values, no justification needs to be written.
(a) 8 n^3 + 2n − 16 = ?(6n^3 + 100n^2 + 6) O, Θ, Ω
(b) log 3 n = ?(
n) O, o
(c) 5 n^ = ?(log 5 n) Ω, ω
(d) 0. 04 n^2 − 50 n = ?(10000n + 4000) Ω, ω
(e) 8 n^70 + n^42 = ?(2n) O, o