COMP 3711 – Design and Analysis of Algorithms 2016 Fall Semester

COMP 3711 – Design and Analysis of Algorithms
2016 Fall Semester – Written Assignment # 1
Distributed: Sept 22 2016– Due: October 5, 2016
Your solutions should contain (i) your name, (ii) your student ID #, and (iii)
your email address
Some Notes:
• Please write clearly and briefly.
• Please follow the guidelines on doing your own work and avoiding plagiarism
given on the class home page. Don’t forget to acknowledge individuals who
assisted you, or sources where you found solutions.
• Please make a copy of your assignment before submitting it. If we can’t
find your answers, we will ask you to resubmit the copy.
• For problems (4) you must
1. describe the algorithm
2. prove its correctness and
3. correctly analyze its running time.
• Each problem is worth 25 points.
• In addition to submitting a hard copy, you will be asked to submit a PDF
online for our records. This PDF can be generated by Latex, from Word or
a scan of a (legible) handwritten solution. The online solution submission
method will be announced by September 30.
Problem 1: For each pair of expressions (A, B) below, indicate whether A is O, Ω,
or Θ of B. Note that zero, one, or more of these relations may hold for
a given pair. List all correct ones. It often happens that some students
will get the directions wrong, so please write out the relation in full, i.e.,
A = O(B), A = Ω(B), or A = Θ(B) and not O(B), Ω(B) or Θ(B).
(a) A = n3 − 100n, B = n2 + 50n;
(b) A = log n, B = log1.1 2n;
(c) A = 22n , B = 23n ;
(d) A = 2log n , B = 2n;
(e) A = 22 log n , B = n;
(f) A = log log n, B = 10100 (this number, which is 1 followed by 100
zeroes, is called “googol”).
Solution:
(a) A = Ω(B);
(b) A = O(B), A = Ω(B), A = Θ(B);
(c) A = O(B);
(d) A = O(B), A = Ω(B), A = Θ(B);
(e) A = Ω(B).
(f ) A = Ω(B).
Problem 2: Give asymptotic upper bounds for T (n) satsifying following recurrences.
Make your bounds as tight as possible. IA correct answer will gain full
credits. It is not necessary to show your work BUT, if your answer was
wrong, showing your work steps may gain you partial credits. If showing
your work, you may use theorems shown in class or can prove results from
scratch.
For (a), (b), (d) you may assume that n is a power of 2; for (c) you may
assume that it is a power of 3.
(a) T (1) = 1; T (n) = T (n/2) + 1 for n > 1.
(b) T (1) = 1; T (n) = 2T (n/2) + n2 for n > 1.
(c) T (1) = 1; T (n) = T (2n/3) + 1 for n > 1.
(d) T (1) = 1; T (n) = 3T (n/2) + n for n > 1.
Solution:
(a) T (n) = O(log n).
(b) T (n) = O(n2 ).
(c) T (n) = O(log n).
(d) T (n) = O(nlog 3 ).
Problem 3: Give an asymptotic upper bound for T (n) satsifying the following recurrences. Make your bound as tight as possible. You must prove this bound
from scratch (either using the expansion method or the tree method)
T (1) = 1;
T (n) = 5T (n/4) + n for n > 1
Solution:
T (n)
= 5T (n/4) + n, for n > 1
= 5[5T (n/42 ) + n/4] + n
= 52 T (n/42 ) + (5/4 + 1) · n
...
= 5i T (n/4i ) + [(5/4)i−1 + (5/4)i−2 + ... + 1] · n
= 5log4 n T (1) + [(5/4)log4 n−1 + (5/4)log4 n−2 + ... + 1] · n, by setting i = log4 n
log4 n
= 5log4 n + [ 1−(5/4)
1−5/4
]·n
...
= 5 · 5log4 n − 4n
log5 n
1
log4 5
(5log4 n = 5 log5 4 = (5log5 n ) log5 4 = (n) log4 4 = (n)log4 5 = nlog4 5 )
= 5 · nlog4 5 − 4n,
= O(nlog4 5 )
Problem 4: (spring) Suppose we are given an array A[1 . . . n] of distinct numbers satisfying that that A[1] > A[2] and A[n − 1] < A[n]. We say that an element
A[x] is a local minimum if it is less than both its neighbors, or more formally, if A[x − 1] > A[x] and A[x] < A[x + 1]. For example, there are two
local minima (3 and 1) in the following array:
9 3 7
2 1 4 5
We can obviously find a local minimum in O(n) time by scanning through
the array. Describe and analyze an algorithm that finds a local minimum
in O(log n) time. If there is more than one local minimum, finding any of
them is fine. [Hint: With the given boundary conditions, the array must
have at least one local minimum. ]
Solution: We will construct a divide-and-conquer algorithm.
If n ≤ 3 the problem can be solved trivially by just looking at the three items.
This is the termination condition.
If n > 3 let m = bn/2c.
We look at the three elements A[m − 1], A[m], A[m + 1].
There are three possible cases:
1. If A[m−1] > A[m] and A[m] < A[m+1], then A[m] is a local minimum
and we are done;
2. If A[m − 1] < A[m] < A[m + 1], then by the boundary condition we
know that there must be at one local minimum between A[1] and A[m].
We know this because, from the boundary condition, A[1] > A[2]. Thus
a largest j < m − 1 such that A[j] > A[j + 1] exists and this A[j], by
definition, will be a local mimimum. We can then reculsively solve the
problem on A[1..m];
3. If A[m − 1] > A[m] > A[m + 1], similar to the case above, we recursively solve the problem on A[m..n] since A[m..n] must contain a local
minimum.
4. If A[m − 1] < A[m] > A[m + 1], then, from the anslysis in the previous
two cases both A[1..m] and A[m..n] contain a local minimum. Since
we only need to report one we can just recurse on A[1..m].
In all cases , we either terminate or reduce the problem size by half. So
we have the recurrence T (n) ≤ T (n/2) + O(1), which solves to T (n) =
O(log n).