Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

COSC 3P03: Algorithms

Midterm, Oct. 28, 2020

Note: your answers must be precise, clear, legible, and as short as possible. Feel free to use the
results/theorems/etc. that are covered in class or done in assignments without having to provide a
proof. You can use your course notes and course notes only. No materials from other sources such
as books and internet or discussions with others are allowed, please. The work submitted must be
your own. The total time is 1 hour and 30 minutes. Your work will be accepted up to 10 minutes
late. Please submit your finished midterm to Sakai.

1. (5) Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0,
and returns the total number of 1’s in n’s binary representation. Note that the input is n, not
its binary representation. For example, A(9) should return 2 as 9’s binary representation is 1001,
while A(7) should return 3 since 7 is 111 in binary. Note that your algorithm should have a running
time of O(log n). Justify your answer. You need to do the following: (1) give a description of your
algorithm in English or high level pseudo code; and (2) give its analysis, ie. recurrence equation
and its solution.

A(n)
if n<2
return n
else
return (n mod 2)+A(n/2)

Solution:
Note that all operations are integer operations: 7/2 = 3, 7 mod 2 = 1. Note that mod is the
same as % which gives the remainder.
Let t(n) be the time required for input n (note that n is NOT the size of the input but the
input itself), then we have

t(n) = t(n/2) + c
t(0) = t(1) = C

with a solution of t(n) = O(log n) where c and C are constants, using the master method, it is Case
2. Or the recurrence is the same as that for binary search which has a time of O(n).

2. (2) What is the running time and its analysis of the following program fragment? Assume that
n = 3k for some k.
j = n
while (j >= 1) {
for i=1 to j
x = x + 1
end for

j = j/3
}

Solution:
O(n). This is because the statement x = x + 1 is executed

n + n/3 + n/32 + · · · + n/3k−1 + n/3k = n(1 + 1/3 + 1/32 + · · · + 1/3k ) = cn = O(n).

where c = 1 + 1/3 + · · · + 1/3k is a geometric series with a value less than 3/2.
Note that it is a simple while loop, not recursive, thus recurrence is not involved.

3. (3) Let A be a 2 by 2 matrix where


!
a b
A=
c d

Show how to compute A × A in 5 multiplications.

Solution:
Since
!
a2 + bc ab + bd
A×A =
ac + cd bc + d2

which is
!
a2 + bc b(a + d)
c(a + d) bc + d2

we only need the following: aa, bc, dd, b(a + d), and c(a + d), requiring 5 multiplications.

4. (3) Rank these functions from the lowest to the highest. Functions with the same asymptotic
growth should be grouped together using { }.

log n5 , n5 , 5n , 25n , 2n , 5log n , nlog 5 , log53 n, log nn , n+(log n)2 , 100n+log n, log n, n log2 n, n2 / log n,
(log n)5 , n1/5 .

Solution:

{log n5 , log n}
{log53 n, (log n)5 }
{n1/5 }
{n + (log n)2 , 100n + log n}
{log nn }
{n log2 n}
{n2 / log n}
{5log n , nlog 5 }
{n5 }
{2n }
{5n }
{25n }

5. (10) (a) Fot the following recurrence, if we are to use the substitution method to solve it, is
t(n) = O(log n) a good guess? Why or why not? (2) Use recursion tree, substitution, and master
method to solve the following recurrence. You can assume that n = 10k for some k. Show your
work.

t(n) = 3t(n/10) + n
t(1) = 1

Solution:
(a) O(log n) is not a good guess as t(n) = 3t(n/10) + n, t(n) = Ω(n).
(b) Iteration (not required):

t(n) = 3t(n/10) + n
= 3(3t(n/102 ) + n/10) + n
= 32 t(n/102 ) + (3/10)n + n
..
.
= 3k t(1) + (3/10)k−1 n + (3/10)k−2 n + · · · + (3/10)n + n
= 3k (n/10k ) + (3/10)k−1 n + (3/10)k−2 n + · · · + (3/10)n + n
= n(1 + (3/10) + (3/10)2 + · · · + (3/10)k−1 + (3/10)k )
= O(n)

Recursion tree:
A ternary tree with the following nodes:

Level 0 : n
Level 1 : n/10, n/10, n/10
Level 2 : n/102 , n/102 , n/102 , n/102 , n/102 , n/102 , n/102 , n/102 .n/102
......
Level k : n/10k , n/10k , · · · n/10k (3k of them)
with a total of n(1 + (3/10) + (3/10)2 + · · · + (3/10)k ), which is O(n).

Substitutaion:
Guess t(n) = O(n). Assume t(n/10) ≤ c(n/10), need to show t(n) ≤ cn.

t(n) = 3t(n/10) + n
≤ 3c(n/10) + n
= cn − 7cn/10 + n
≤ cn if c is suf f iently large

Master method:
a = 3, b = 10, f (n) = n, and log10 3 < 1. So there exists some ǫ > 0 such that f (n) =
n = Ω(nlog10 3+ǫ ). Need to verify another condition af (n/b) ≤ cf (n) for some c < 1 to make
sure it is Case 3: af (n/b) = 3(n/10) = (3/10)n ≤ (3/10)n for c = 3/10. So it is Case 3 and
t(n) = Θ(f (n)) = Θ(n).

6. (2) Let f (n), g(n), and h(n) be positive and increasing functions of n. Using the definitions of
big Oh O, little oh o, Ω, and Θ, indicate True or False for each of the following statements and
give your proof if it is true and counter example if it is false.

1. If f (n) = O(g(n)), then g(n) = Ω(f (n)).


True. Since f (n) = O(g(n)), by definition of big Oh, there exist n0 and c > 0 such that
when n ≥ n0 , f (n) ≤ cg(n). Thus g(n) ≥ (1/c)f (n), which means, by the definition of Ω,
g(n) = Ω(f (n).

2. Suppose f (n) = O(h(n)) and g(n) = O(h(n)). Then f (n)/g(n) = O(1).


False. A counter example: f (n) = n2 , g(n) = n, and h(n) = n2 , then f (n) = O(h(n)) and
g(n) = O(h(n)) but f (n)/g(n) = n 6= O(1).

You might also like