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

Homework Assignment #1 CMPUT 204

Due: Oct 9th, 2017 Department of Computing Science


University of Alberta

Note: All logarithms are in base 2 unless specified otherwise. We denote R+ = {x R : x > 0} and
R1 = {x R : x 1}.
This assignment is partitioned into Exercises and Problems. Exercises are optional and will not be
graded. Problems are mandatory and will be graded. You are, however, strongly advised to work out
a full solutions for both.
Your max-grade for this assignment is 105.

Exercise I. In the binary-search problem we take as input a sorted array A with n elements, and a key
k and we return the largest element in A that which isnt strictly greater than k. That is we return

max{x A : x k} , or if all x A satisfy x > k

Write a pseudo-code for binary search that (a) uses recursion and (b) runs in O(log(n)) time.
Exercise II. Prove that for any n 1 and 0 k n we have that nk = n1 n1
  
k + k1 .
Use this fact to give a recursive algorithm that computes nk . Prove the correctness of your algorithm.


Exercise III. True or False? Prove.



1. n O(n).

2. 2n O(2 n )

3. There exists a > 1 such that n (n log n).

4. For any function f : N R+ it holds that o(f ) (f ) = .

5. For any function f : N R+ it holds that (f ) = (f ) (f ).

Exercise IV. For any two positive functions f, g : N R+ we denote f g if (f (n) g(n)) o(1).
Show that the relation is (i) reflexive (i.e., for any f : N R+ we have f f ), (ii) symmetric
(i.e., for any f, g : N R+ if f g then g f ) and (iii) transitive (i.e., for any f, g, h : N R+ , if
f g and g h then f h).
Exercise V. Pick your favorite programming language.
For each of the following value of n from 1000 to 50, 000 in increments of 1000 do the following:

1. Generate an array A large enough to hold n integers.


2. Start the clock.
3. Repeat n times:
(a) Generate an arbitrary integer.
(b) Insert the integer into A.
4. Stop the clock and compute the runtime.

Now draw the plot of the runtime as a function of n.


However, you should repeat this experiment twice: once where the insertion of Step 3(b) is done
using the standard Insert, and once where the insertion is done using Insert2 (see Slides of Unit 01).
Draw both plots on the same figure. Explain the two plots.

1
Problem 1. (15 pts) True or False? Provide a short explanation.

(i) n19 (log(n))75 + 19 n75 O(n 1975 ) (Note: 1975 44.44)
1
(ii) n95 (log(n))17 + 19 75 n O(75n 9 )
75 19
(iii) 19n O(75n )
(iv) 1975n ((19 75)n ).
(v) 1.975n + 1.025n O(3n ).

(vi) 1.975n + 1.025n O(3 n ).
log log n
(vii) n log n O(n0.1975 ).
(viii) n! O(2n log n )
(ix) 1c + 2c + . . . + nc (nc+1 ) for every constant c 0.
(x) For any two positive constants c, d > 0 we have that nc o(2dn )
(xi) For any functions f, g : N R+ , either f O(g) or g O(f ).
(xii) For any functions f, g : N R+ , denoting min{f, g}(n) = min{f (n), g(n)} then min{f, g}(n)
(f (n) + g(n)).
(xiii) For any functions f, g : N R+ , denoting max{f, g}(n) = max{f (n), g(n)} then max{f, g}(n)
(f (n) + g(n)).
p p p
(xiv) For any functions f, g : N R+ , f (n) + g(n) ( f (n) + g(n)).
(xv) For any functions f, g : N R1 , log(f (n)) (log(g(n)) if and only if f (n) (g(n)).

Problem 2. (20 pts) Order the following list of functions by increasing big-O growth rate. Group
together (for example, by underlining) those functions that are big- of one another.

5
1975n 197 log((log n)2 ) (log(n))1975 2log(n+1975)
2


2 n times log(n)
log(2 ) 2 1975 2(log(n)+1975) 2n + 2n1 + 2n2 + . . . + 20 19n3/4 + 75n3/2
n
log(n)
19n0.75 + 2 2log (n) n2 log10 (n) + 210 n2 22 /2n n log(1975n)

4n n3 n2.99 (log(n))1975 b2n(log(n))2 c 4log n log1975 n

Problem 3. (15 pts) The set f (n) (1 + o(1)): Given two positive functions f, g : N R+ We say that
f (n)
f (n) = g(n) (1 + o(1)) if the function g(n) 1 is in o(1).
(i) (2 pts) Show that for any two positive functions f, g : N R+ , we have that f (n) = g(n) (1 + o(1))
iff for any  > 0 there exists N such that for all n N we have g(n)(1 ) < f (n) < g(n)(1 + ).
(ii) (2 pts) Show that if f (n) = g(n)(1 + o(1)) then f (n) (g(n)).
(iii) (2 pts) Show that the inverse is not correct, i.e. that there exist two positive functions f, g such
that f (n) (g) and yet f (n) 6= g(n)(1 + o(1)).
(iv) (2 pts) Show that for any c > 0 we have that log(n + c) = log(n)(1 + o(1)).

2
(v) (3 pts) For any two positive functions, let us denote f g if f (n) = g(n)(1 + o(1)). Show that the
relation is reflexive (f : N R+ , f f ), symmetric (f, g : N R+ , f g g f ) and transitive
(f, g, h : N R+ , f g g h f h).
A relation satisfying reflexivity, symmetry and transitivity creates congruency classes, so for every
f : N R+ we denote [f ] = {g : N R+ : f g} as the congruency class of f and this set is
well-defined.1
Similarly to the previous definition, for any two positive functions f and g we say f (n) = g(n) + o(1)
if (f (n) g(n)) o(1). We denote f g if f (n) = g(n) + o(1). You can check and see that in-
deed is reflexive, symmetric and transitive. This allows us to define for any f : N R+ the set
[f ] = {g : N R+ : f g}.

(vi) (2 pts) Show that for any positive function thats lower-bounded by 1, f : N R1 , we have that
[f ] [f ] .
(vii) (1 pts) Give a function f : N R1 such that [f ] 6= [f ] .
(viii) (1 pts) Give a function f : N R1 for which [f ] = [f ] .

Problem 4. (15 pts) Recall that the sequence of Fibonacci numbers is defined as F (0) = 0, F (1) = 1
and F (i) = F (i 1) + F (i 2) for i 2.

(i) (3 pts) Denote = 1+2 5 . Let f (x) = x2 x 1. Show that f () = 0, and that f 0 (x) > 0 for any
x 1. Infer that for any a, b such that 1 a < < b we have that a + 1 > a2 and b + 1 < b2 .
(ii) (7 pts) Prove that for any a, b such that 1 a < < b we have that F (n) O(bn ) and that
F (n) (an ).
(iii) (5 pts) Prove that for any a, b such that 1 a < < b we have that Fn o(bn ) and that
Fn (an ). (Hint: use (ii))

Problem 5. (20 pts) Consider the following program:


procedure Foo(A, f, l)
**Precondition: A[f . . . l] is an array of integers, f, l are two naturals 1 with f l.
if (f = l) then
return A[f ]
else
m b f +l
2 c
return min(Foo(A, f, m), Foo(A, m + 1, l))
end if
(i) (2 pts) In a sentence: what does Foo return? State a formal claim.
(ii) (4 pts) Prove that indeed Foo returns what you claim it returns. (You may assume all elements in
A are unique.)
(iii) (4 pts) Using induction, argue that Foo invokes min at most n 1 times.
(iv) (3 pts) Write a non-recursive version of Foo(A, f, l) (in pseudo-code).
(v) (4 pts) Prove the correctness of your algorithm.
(vi) (3 pts) Find the worst case running time of this new program and express it in notation.

1
Namely, for every f and any g [f ] we have that [g] = [f ] .

3
Problem 6. (20 pts) A powerful queen has a cellar containing n barrels of expensive wine. Her guards
have just caught a spy after poisoning precisely one of the queens wine barrels. To make matters
worse, the poison the spy used was very deadly just on drop from the poisoned barrel suffices to kill
someone. Even so, the poison works slowly it takes a full month for the person to die.
Design an algorithm that allows the queen to determine exactly which one of his wine barrels was
poisoned in just one months time by forcing at most dlog(n)e people to taste wine. Prove your claims.

Hint: Start by thinking recursively. Assuming you have a tasting scheme for n barrels with k
tasters, devise a poison-finding tasting scheme for 2n barrels using k + 1 tasters. Then unravel the
recursion to see what barrels will the j-th taste drink from. It is OK if you think of n as a power of 2 for
the purpose of finding the algorithm, but your algorithm should work for any n. (Also, it is probably a
tad more convenient to number the barrels from 0 to n 1 rather than from 1 to n.)

You might also like