Вы находитесь на странице: 1из 11

CMPSC 465

Data Structures and Algorithms


Spring 2013

Exam I: Introduction to Asymptotic Analysis and Divide-and-Conquer


January 31, 2013
Name: Partial Solution

Last 4 digits of ID: ____ ____ ____ ____

Honor Code Statement


I certify that I have not discussed the contents of the exam with any other student and I will not discuss the contents
of this exam with any student in this class before it is returned to me with a grade. I have not cheated on this exam in
any way, nor do I know of anyone else who has cheated.
Signature: _________________________________________________________

Directions/Notes:

Write your ID on every page of this exam. Write your name just on this cover page.

No outside notes, books, or calculators are permitted. However, you will be provided with a page giving a
few useful bits of information.

Be sure to sign the honor code statement when you are finished.

All questions on this exam are implicitly prefaced with As taught in CMPSC 465 lectures this term.

Always justify your work and present solutions using the conventions taught in class.

The problem that is a take-off on a challenge problem is on the last sheet of the exam. You must solve that
problem first and remove and turn in that page of your exam within the first 30 minutes of the exam period.
Due to the logistics of timing the challenge problem, if you arrive late without having made prior
arrangements with the instructor, you will either forfeit missed time or be permitted to earn only up to half
credit on the problem.

Use pencil to complete this exam. Use of pen will result in an automatic 10-point deduction and we
reserve the right not to read any problem with cross-outs.

Score Breakdown:
#

A.P.

Total

15

10

10

10

12

10

20

--

100

Score
Value

CMPSC 465 Exam 1 Spring 2013 Page 2

1.

Last 4 Digits of ID: ___ ___ ___ ___

Ranking Growth of Functions. Below are expressions that are functions of n. You have two jobs: First, as much as
possible, simplify each expression and write it in terms of -notation. Second, rank the functions in terms of asymptotic
dominance. The fastest-growing function should be #1. If two functions grow at the same rate, they may share a ranking
and the next ranking should be skipped. (For example, if you had a tie for 3rd place, youd have rankings 1, 2, 3, 3, 5, 6,
etc., but no 4.)
[15 pts.]
Note: This is a very important problem. We went over some matters Friday in lecture. Make it a priority to make sure you understand anything you
missed here.

function (and room for work)

asymptotically
simplified form

ranking

(lg n)

11

(10n)

(n50)

(1)

14

3(nn 3n)

(nn)

(n)

10

3n + 150n

(150n)

150 3n

(3n)

n
n3 + + log12 n + 18n!
7

(n!)

12nlog 2 n + n + 18n2

(n2)

log 7 n + 12n5 n3

(n5)

12(log 2 n + 1)

(lg n)

11

log10 n + 2013

(lg n)

11

log 3 n + n!
17

(n!)

nlog7 n
5n
n

10

--- use geometric series formula, get (10n+1 1) / const.

i=0
50

--- again, geo. series, but here r = n. (n50+1 1) / (n 1)

i=0

35n2 + 12n3
n(n + 5)

--- denominator is O(n2)

18

CMPSC 465 Exam 1 Spring 2013 Page 3

2.

Last 4 Digits of ID: ___ ___ ___ ___

Sorting.
a.

[10 pts.]

Illustrate a complete trace of a merge sort of the array below, following the style used in lecture:
index
value

1
92

2
81

3
103

4
72

5
31

Heres the general idea, where vertical bars indicate splits in arrays:
92 81
92 81
92 | 81
81 92
81 92
31 72

103
| 103

|
|
|

72 31
72 | 31
31 72

103
81 92 103

b.

What is the theoretically predicted running time of merge sort? (n lg n)


[This question is just a recall question. No explanation necessary.]

c.

Consider this claim: For any array of size n where n is particularly large, it is guaranteed that merge sort will
perform faster than insertion sort. Is this claim true or false? If its true, give a rigorous argument why insertion
sort will always perform worse than what youve claimed above. If its false, describe the best scenario in which
insertion sort would do better than what youve claimed above and rigorously derive the running time.
False. Insertion sorts best case running time happens when the input is sorted; then insertion sort runs in linear time,
beating merge sorts n lg n, which is blind to the distribution of the input.
Heres why:
Consider an insertion sort input A[1..n] s.t. A[1] A[2] A[n].
Insertion sort starts its outer loop with k = 2, finding a proper position for key A[2] looking backwards among
A[1..k-1] until it finds the proper position for A[k]. Since A[k] belongs it position k, A[k] only needs to be compared
to A[k-1] to determine where it belongs, using only one comparison. This repeats for the elements at positions 3..n,
or, including position 2, n 2 + 1 = n 1 elements being compared. Thus, the running time is linear.
Note: Part (a) was to be the easy problem. Part (b) was really just to help us judge your part (c). Part (c) was to follow up on whether or not you
got the takeaway point from trying sorted inputs in the programming assignment and could rigorously explain what was going on. Go back to that
assignment if you didnt get it right.

CMPSC 465 Exam 1 Spring 2013 Page 4

3.

Running Time of a Code Fragment. Consider the following code fragment:


b=1
for i = 1 to n + 1
{
a=0
for k = 2 to i / 2

Last 4 Digits of ID: ___ ___ ___ ___

[10 pts.]

{
a=a+1
}
b = b * (a + 5)

1 operation
2 operations

}
In terms of n, how many additions and multiplications (total) are performed when this code is executed with an even
input of n?
Strategy for solving this problem:
1. Figure out how many iterations the outer loop runs. (This is the easy part.)
2. Figure out how many iterations the inner loop runs. (This is the hard part.)
a. Start with a trace table of i and k. You need to take it far enough to see whats going on.
b. Use the trace table to notice how many iterations are done for each value of i.
c. Set up a sum.
d. Translate that sum to sigma notation and evaluate it. (This is where you need the tools from Epp 5.1/5.2)
3. Observe the number of operations per iteration in the inner and outer loops.
4. Set up an expression for the total number of operations, plug in prior results, and simplify.
Note: This was a harder version of the kind of problems in Epp 11.3 and similar problems we did in lecture. Well go
over this more in recitation before I release a solution. The solution, as we discussed in recitation, is now on the next
page. If you didnt get it completely right, thats okay. However, if you didnt set up a trace table or use a summation,
that was a problem.

CMPSC 465 Exam 1 Spring 2013 Page 5

Last 4 Digits of ID: ___ ___ ___ ___

Solution:
The outer loop runs n + 1 1 + 1 = n + 1 times.
Here is a trace table for the inner loop:
i
1 2 3 4 5 6
7
k

8
3

n
3

n+1
3

n
2

n + 1
2

Since n is even, n + 1 is odd. So, using the cases of the n / 2 thm.,


the i = n iteration of the outer loop ends in k = n/2.
the i = n + 1 iteration of the outer loop ends in k = (n+1-1)/2 = n/2

The inner loop has 1 operation/iteration, so its number of iterations is its number of operations:
# inner loop ops =

n
n

1+ 1+ 2 + 2 + ...+ 2 + 1 + 2 + 1
2
2

n n
1+ 1+ 2 + 2 + ...+ 1 + 1
2 2

n
2 1+ 2 + ...+ 1
2

2 i

by # consec. ints in list [m..n]

n/21
i=1

(n / 2 1)(n / 2 1+ 1)
2
(n / 2 1)(n / 2)

n2 / 4 n / 2

So, the total number of operations is


# ops

=
=
=

(# ops in inner loop) + (2 ops/outer loop iteration)(n + 1 outer loop iterations)


1 2 1
n n + 2n + 2
4
2
1 2 3
n + n+2
4
2

CMPSC 465 Exam 1 Spring 2013 Page 6

4.

Last 4 Digits of ID: ___ ___ ___ ___

Order Notation. Using the formal definitions of order notation and theorems from class, prove the claims.
a.

[7 pts.]

When f (x) = 12x 6 6x 3 + 2x 15 and g (x) = x6, f (x) is O(g (x)).


Assume x > 1. Then x6 > x3, x6 > x1, and x6 > x0

(*)

Consider 12x 6 6x 3 + 2x 15 :

12x 6 6x 3 + 2x 15

12x 6 + 6x 3 + 2x + 15

by triangle inequality

12x 6 + 6x 3 + 2x + 15
12x 6 + 6x 6 + 2x 6 + 15x 6
35x 6

as x > 1
by subs. from (*)

35 x 6

as x > 1 (or, really, even power nonneg)

So, with B = 35 and b = 1, f (x) is O(g (x)).


b.

When h(x) = 7x 3 + 2x 2 + 20 and g (x) is x3, h (x) is (g (x)).


Assume x > 0. Then 2x2 + 20 > 0.
Consider 7x3. By adding the positive quantity noted above,
7x3 7x 3 + 2x 2 + 20
Then, as x > 0 gives that all quantities in the expression are positive,

7 x 3 7x 3 + 2x 2 + 20
So, with A = 7 and a = 0, h (x) is (g (x)
c.

If we wanted to prove that h (x) from (b) is (g (x)), what else would we need to do?
[Dont actually prove this, but tell what you would do at a high level. This question is worth one point and it should
take you less than 30 seconds to write down your answer.]
Prove h (x) is O(g (x)) too.

5.

Master Theorem. Using the Master Theorem, give (and explain) tight asymptotic bounds for each recurrence.
a.

T (n) = 3T (n/9) +

[6 pts.]

log a

log 3

a = 3, b = 9, so n b = n 9 = n1/2
f (n) = n1/2
n1/2 = n1/2, so this is Case 2: T (n) = (n1/2 lg n).
b.

T (n) = 8 T (n/2) + (n2)


log a

log 8

a = 8, b = 2, so n b = n 2 = n3
f (n) = (n2)
(n2) < n3, polynomially so: with = 1, f (n) = O(n3-), so this is Case 1: T (n) = (n3).
Note: No further commentary or follow up on this page. If #5 didnt go well for you, you are responsible for studying that on your own. #4 was like the
lecture examples and homework in Epp 11.2; I think being able to do a problem like #1 is more important in the long run, but testing the formality had its
place on this exam.

CMPSC 465 Exam 1 Spring 2013 Page 7

6.

Last 4 Digits of ID: ___ ___ ___ ___

Closed Form of a Recurrence. Consider the following recurrence:


a1 = 12

ak = 3a k /2

[10 pts.]

for k 2

Using some form of induction and the formal proof style used in class and Epp, prove that the closed form of this
lg n

recurrence is an = 12 3

. To save time, you only need to prove the inductive step for odd values of k.
lg n

To Prove:

For all n Z s.t. n 1, P(n): The closed form an = 12 3

Proof:

By strong induction, for odd n only.

matches the recurrence given above.

Base:
Consider n = 1:
By the recurrence, a1 = 12
lg1

By the closed form, an = 12 3

= 12 30 = 12

As 12 = 12, P(1).
Inductive Step:
Let k Z s.t. k 1 and assume i Z s.t. 1 i k, P(i), i.e.
lg i

ai = 12 3

[inductive hypothesis]

Consider ak+1 for odd k only (which means k + 1 will be even):


ak+1 = 3a ( k+1)/2
by recursive branch of recurrence, as k 1 k + 1 2
by n / 2 theorem, as k + 1 is even

= 3a( k+1)/2
lg(( k+1)/2)

= 312 3

lg( k+1)lg 2

= 312 3

lg( k+1)1

= 312 3

lg( k+1) 1

= 312 3

lg( k+1) 1

= 12 33

1+ lg( k+1) 1

= 12 3

lg( k+1)

= 12 3

by subs. from inductive hypothesis, as 1 (k+1)/2 k


by laws of logs
as lg 2 = 1
by log property 3
by commutativity
by laws of exponents
by commutativity and addition

So, P(k + 1).


By the principle of strong mathematical induction, P(n) for all [odd] n 1.

Notes: This is an important problem and results were mixed. Im not going to do any follow up on this one, but if you did badly here, you are definitely
responsible for working back through how to do a proof by strong induction. Review the lecture notes and homework from Epp 11.4; this problem was
similar to something there. Some very important points to note: (1) Some of you never defined your P(n), but tried to use it. Nothing else follows
without it. (2) Remember, P(n) is a proposition, not a quantity. (3) Your inductive hypothesis and substitution from it should be labeled clearly. (4)
Your inductive hypothesis needed to be strong here.

CMPSC 465 Exam 1 Spring 2013 Page 8

7.

Last 4 Digits of ID: ___ ___ ___ ___

Designing an Algorithm. Consider an array A containing n distinct integers. We define a local minimum of A to be an x
such that x = A[i], for some 0 i < n, with A[i-1] > A[i] and A[i] < A[i+1]. In other words, a local minimum x is less than
its neighbors in A (for boundary elements, there is only one neighbor). We want to solve the problem of finding a local
minimum. Note that A might have multiple local minima, but you only need to locate and return one.
[12 pts.]
As an example, suppose A = [10, 6, 4, 3, 12, 19, 18]. Then A has two local minima: 3 and 18.
a.

Describe an algorithm using the divide and conquer mindset to solve this problem.
Let m = n/2, and examine the value A[m] (that is, the element in the middle of the array).
Case 1: A[m 1] > A[m] and A[m + 1] > A[m]. Then A[m] is a local minimum, so return it.
Case 2: A[m 1] < A[m]. Then the left half of the array must contain a local minimum, so recurse on the left half.
We can show this by contradiction: assume that A[i] is not a local minimum for each 0 i < m. Then A[m1] is not a
local minimum, which implies that A[m2] < A[m1]. Similarly, A[m3] < A[m2]. Continuing in this fashion, we
obtain A[0] < A[1]. But then A[0] is a local minimum, contrary to our initial assumption.
Case 3: A[m + 1] < A[m]. Then the right half of the array must contain a local minimum, so recurse on the right half.
This is symmetrical to Case 2.

b.

Express a recurrence for the running time of your algorithm. Explain why this recurrence fits. [If you are unable to
solve (a) and want some partial credit here, explain how to derive a recurrence from a divide-and-conquer algorithm
in general and draw a large X over your work in (a).]
We divide the original problem into 1 subproblem, which is roughly 1/2 as large (floors and ceilings dont matter
asymptotically) as the original problem. There is no combine work to do in this case and dividing takes constant
time. So, our recurrence is:
T(n) = T(n/2) + (1)
[and, by case 2 of the Master Theorem, T(n) = (lg n).]
[Recall the general D&C recurrence form of T(n) = a T(n/b) + D(n) + C(n). See your notes for definitions.]

Note: Well do more with this as we go. We think you need more practice. Expect to see more of divide-and-conquer in Unit 2.

CMPSC 465 Exam 1 Spring 2013 Page 9

8.

Last 4 Digits of ID: ___ ___ ___ ___

Use a recursion tree to guess a bound for the closed form of the recurrence T(n) = 2T(n/5) + 3T(n/6) + cn that is as
accurate as possible. You do not need to prove your closed form correct.
[10 pts.]
Note: Im going to release the solution to this now, but well go over this in recitation. If you didnt like this problem,
its probably better to solve a problem of the form T(n) = a T(n/b) + f (n), i.e. solvable by master theorem, but you need
to understand those kinds of trees to understand this kind (unbalanced). The strategy to started, balanced or unbalanced:
1. Draw a tree. 2. Observe cost per level, generalize. 3. Figure out height.
4. Set up a sum for total cost.
Recurrence Tree:
cn

cn/5

cn/5
log5n

cn

cn/6

cn/6

cn/6

9/10cn

cn/30 cn/30 cn/36 cn/36 cn/36

2
(9/10)
(3/2)2ncn

...
cn/25 cn/25 cn/30 cn/30 cn/30

.
.
.

...

Total: (n)
Since the subproblem T(n/6) will hit the ground (when n down to 1) earlier than the subproblem T(n/5), when we
consider the depth of the recurrence tree, we can just take the subproblem T(n/5) into account. Then, we find that the
subproblem size for a node at depth i is n/5i. Thus, the subproblem size hits n = 1 when n/5i = 1 or, equivalently, when i
= log5n. Thus, the tree has log5n + 1 levels (at depth 0, 1, 2, 3, , log5n).
Next we determine the cost at each level of the tree. We can find that for a problem T(m), 5 subproblems are derived
from T(m) at the level below. They are 3 subproblems T(m/6) and 2 subproblems T(m/5). The total cost of these 5
subproblems are: 3T(m/6) + 2T(m/5) = 3*cm/6 + 2*cm/5 = (9/10)cm = (9/10)T(m). Thus, we can conclude that the total
cost over all nodes at depth i is 9/10 of the total cost over all nodes at depth i 1. Given that the cost at depth 0 is cn, we
see that the total cost over all nodes at depth i, for i = 0, 1, 2, 3, , log5n, is (9/10)icn.
Next, we consider the leaves. Since the problem splits into 5 subproblems at each level (or the branching factor of the
tree is 5), we would have 5k nodes on level k, for some integer k, provided the level were full. But, even if the level is not
full, this value serves as an upper bound. We use the number of leaves that would be on the last level as an upper bound
on the number of leaves: 5 log5 n . Since we assume leaves do constant work, this is also the contribution of the leaves.
Using the last of the log properties, we rewrite this as n log5 5 = n1 = n .
Now we add up the costs over all levels to determine the cost for the entire tree:
T(n) =

log5 n1

(9 / 10) k cn + n

by summing work of internal levels and adding leaves

k=0

log n+1

cn((9 / 10) 5 1)
+n
(9 / 10) 1

cn(1 (9 / 10) 5
1 (9 / 10)

log n+1

= 10cn(1 (9 / 10)

log5 n+1

by geometric series summation formula

+n

by multiplying both numerator and denominator by -1

) +n

by evaluating the fraction

Thus, we have derived a guess of the closed form of T(n): T(n)= 10cn(1 (9 / 10)
T(n) = 2T(n/5) + 3T(n/6) + cn.

log5 n+1

) + n for our original recurrence

CMPSC 465 Exam 1 Spring 2013 Page 10

9.

Last 4 Digits of ID: ___ ___ ___ ___

Algorithm Correctness [From Challenge Problem]. The following code fragment implements Horners rule for
evaluating a polynomial
n

P(x)

a x

a0 + x(a1 + x(a2 + ...+ x(an1 + xan )...))

k=0

given the coefficients a0, a1, . an and a value for x:


y=0
for i = n downto 0
y = ai + x y

[20 pts.]

Note: This problem must be solved and turned in within the first 30 minutes of the exam period. Remove this sheet.
a.

Consider the following loop invariant:


At the start of each iteration of the for loop,

y=

n(i+1)

ak+i+1x k

k=0

Interpret a summation with no terms as equaling 0. Prove the correctness of this algorithm.
Initialization:
We start by showing that the loop invariant holds before the first two iterations. Before the first iteration (i = n), y =
0; before the second iteration (i = n-1), y = an according to the algorithm. Since

n( n+1)
k=0

ak+n+1x k = 0 , and

n( n1+1)
k=0

ak+n1+1x k = an ,

we can guarantee that the loop invariant holds prior to the first and second iterations of the loop.
Maintenance:
Next we show that each iteration maintains the loop invariant. Consider an iteration for a given value of i in [0, n].
By the loop invariant, before the (n-i+1)st iteration, we have

y = k=0

n(i+1)

ak+i+1x k .

By the algorithm, after the (n-i+1)st iteration,

y = ai + x k=0

n(i+1)

= ai + k=0

n(i+1)

= k=0 ak+i x k
ni

ak+i+1x k

ak+i+1x k+1

which is in accordance with the loop invariant before the (n-i+2)nd iteration (i = i-1).
Termination:
The for loop terminates after the last iteration and leaves i = -1. By the loop invariant,

y = k=0

n(1+1)

ak+1+1x k

= k=0 ak x k
n

which is what we wanted.

Note: Part (a) was especially tough. More important than the details of part (a) is the concept of correctness proofs. Did you
solve the recitation problem from last Monday and write it up formally yet? If you didnt, you should do that as soon as
possible. It should precede this problem on your list of priorities.

CMPSC 465 Exam 1 Spring 2013 Page 11

b.

Last 4 Digits of ID: ___ ___ ___ ___

What is the exact number of additions and multiplications performed by this code fragment? [Show work neatly.]
The loop body has 2 operations. The loop executes n 0 + 1 = n + 1 iterations. So, there are 2(n + 1) = 2n + 2
operations.

c.

In -notation, what is the running time?


(n)

Now consider the following pseudocode, which uses the same symbols as the problem statement and where methods have
preconditions that k 0 and n 0:
EVAL-MONOMIAL(x, k)
if k == 0
return 1
if k == 1
return x
else
h = k / 2

EVAL-POLYNOMIAL(a[0..n], x, n)
sum = 0
for k == 0 to n
sum = sum + a[k] EVAL-MONOMIAL(x, k)
return sum

b = EVAL-MONOMIAL(x, k mod 2)
m = EVAL-MONOMIAL(x, h)
return b m m
d.

The book had you implement nave polynomial evaluation, which ran in (n2) time. How does the performance of
EVAL-POLYNOMIAL compare to nave polynomial evaluation and Horners rule? Explain. You do not need a
rigorous argument, but should be able to see a bound on the running time of the above code and include that bound
in your explanation.
The short answer: Horners rule ((n)), outperforms this algorithm ((n lg n)), but this algorithm outperforms nave
evaluation at (n2).
The algorithm shown here takes a divide-and-conquer approach to computing a monomial in EVAL-MONOMIAL: We
split the problem into subproblems. We can compute xk as the product of xk/2xk/2 in the case k is even or xxk/2xk/2 in the
case k is odd. The factor b exploits that x0 = 1 to handle this without if statements. We dont need to compute xk/2
twice, so by saving it, we cut running time over nave evaluation. You could intuitively see that EVAL-MONOMIAL
runs in (lg k) time and its called by EVAL-POLYNOMIAL n 0 + 1 = n + 1 times, so with a little bit of handwaving,
we could say this work is done in (n lg n) time (or O(n lg n) with less handwaving since k < n).
[A bit more rigor that was not necessary here: Computing the factor b is technically a subproblem, but its only a
subproblem of size 1. Computing m requires one subproblem of size k/2. The divide work and combine work
involves a constant number of operations, so the recurrence is T(k) = T(k/2) + T(1) + (1) or T(k) = T(k/2) + (1)
since we know T(1) is constant. Case 2 of the master theorem establishes that this is (lg n).
EVAL-POLYNOMIALs time is technically

k=0

k=0

k=0

k=1

(lg k) = c lg k = c lg k =c lg k =c lg n! = (nlg n) , but Ill leave

you to ponder that. By the time of Exam 2, you should be able to follow the steps.]

IMPORTANT NOTES:
This last problem must be solved in the first 30 minutes of the exam period.
Remove this sheet. Listen for time to be called to pass it in.
Make sure the last 4-digits of your ID number on this sheet match the rest of your exam or you
will not get credit for this problem.
Write the first letter (only) of your last name in the box to the right to assist us with sorting exams
while relatively maintaining anonymity during grading.

First Letter of
Last Name:

Вам также может понравиться