Exam 1 with Solution - Computer Algorithms I | MCS 401, Exams of Algorithms and Programming

Material Type: Exam; Class: Computer Algorithms I; Subject: Mathematical Computer Science; University: University of Illinois - Chicago; Term: Spring 2012;

Typology: Exams

2011/2012

Uploaded on 05/18/2012

koofers-user-7ix
koofers-user-7ix 🇺🇸

5

(2)

7 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MCS 401 Exam 1 29 February 2012
Name:
Do not start until instructed to do so.
In order to get full credit, you need to show your work.
You have 50 minutes to complete the exam.
Good Luck!
Problem 1 /35
Problem 2 /20
Problem 3 /20
Problem 4 /10
Problem 5 /15
Total /100
pf3
pf4
pf5

Partial preview of the text

Download Exam 1 with Solution - Computer Algorithms I | MCS 401 and more Exams Algorithms and Programming in PDF only on Docsity!

MCS 401 Exam 1 29 February 2012

Name:

• Do not start until instructed to do so.

• In order to get full credit, you need to show your work.

• You have 50 minutes to complete the exam.

• Good Luck!

Problem 1 /

Problem 2 /

Problem 3 /

Problem 4 /

Problem 5 /

Total /

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.

  • When j = n, then A[n] is the smallest element among A[n... n].
  • Consider the end of one iteration of the loop on line 7. We need to prove that A[j − 1] is the smallest among A[j − 1... n]. We know from the loop invariant that A[j] is the smallest among A[j... n] and the if on line 4 makes sure that we put the smallest between A[j − 1] and A[j] in location j − 1. Thus A[j − 1] is the smallest among A[j − 1... n] so when the loop wraps around, the invariant will be true at the start of the next iteration.

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,

X =

∑^95

i=

Xi

E[X] =

∑^95

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

E[X] =

∑^95

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