Practical Session 2 – “Constants Don’t Matter!!!”
Algorithm Analysis
f(n) = O(g(n)) / There exist c > 0 and n0 > 0 such that:0 ≤ f(n) ≤ cg(n)for each n ≥ n0
f(n) = Ω(g(n)) / There exist c > 0 and n0 > 0 such that:
0 ≤ cg(n) ≤ f(n) for each n ≥ n0
f(n) = Θ(g(n)) / There exist c1, c2 > 0 and n0 > 0 such that:
0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) for each n ≥ n0
Some Basics
Prove: 100 * n + 12 = Θ(n).
Solution:
Prove: .
Solution:
Question 1
Prove : (log n)log n = Ω(n3/2).
Solution:
cn3/2 = c(2log n)3/2 = c(23/2)log n ≤ (log n)log n
Question 2
Prove that log(n!) = Θ(n logn).
Solution:
log (n!) = log(1*2*3*…*n) = log 1 + log 2 + log 3 + … + logn.
log(n!) = O(n logn)
log(n!) =
Ω(n logn)
Explanation of the inequality:
: =>
c = 0.25
n0 = 8
log(n!) = Θ(n log n)
Question 3
Prove : (log n)! = Ω (log(n!)).
Solution:
By the previous question, it is sufficient to prove that (log n)! = Ω(nlogn).
Observe that for an integer k ≥ 4, we have k! > 2k.
Hence, (logn)! = (logn)(logn -1)! > (logn) ∙2logn -1
= (logn) (n/2) ≥ c∙n logn.
(logn)! = Ω(n logn).Question 4
Give an example for two functions g(n) and f(n) such that neither g(n) = O(f(n)) nor g(n) = Ω(f(n)).
Solution:
f(n) = n, g(n) = n1-cos n.
Question 5
Prove: For any two functions f(n) and g(n), f(n) +g(n) = Θ(max(f(n),g(n))).
Solution:
Observethat:
The left inequality implies that f(n) +g(n) = Ω(max(f(n),g(n))),
whereas the right inequality implies that f(n) +g(n) = O(max(f(n),g(n))).
Question 6
Prove or disprove:
1. For any functions f(n) and g(n), f(n)=O(g(n))→g(n)= Ω (f(n)).
2. For any function f(n), f(n)=(f(n/2)) .
Solution:
- f(n)=O(g(n)) → f(n) ≤c∙g(n) → g(n) ≥ 1/c∙f(n) → g(n)= Ω (f(n)).
- False, e.g., f(n) = 2n. Then f(n) = 2n ≠ (2n/2)= f(n/2)
Question 7
Given a sorted array A of n distinct integers.
a)Describe an O(1) time algorithm that determines whether or not there exists an element x, such that A[1] < x < A[n] and x is not in A.
b)Describe an O(logn) time algorithm that finds such an x.
c)Describe an efficient algorithm that determines whether or not there exists an element i in A, such that A[i]=i. Analyze the running time of the algorithm.
Solution:
a)
We describe the algorithm SimpleCheck(A,r,q) where r<q are natural numbers.
SimpleCheck(A,r,q)
{
return (A[q] – A[r] q-r)
}
To solve a we runSimpleCheck(A,1,n).
b)
We describe the algorithm SolveB(A,r,q) where r<q are natural numbers.
SolveB(A,r,q)
{
If (r+1 = q) return A[r]+1;
m = (r+q) div 2;
if (SimpleCheck(A,r,m) == true)
returnSolveB(A,r,m);
else
returnSolveB(A,m,q);
}
To solve bwe use the following algorithm:
if (SimpleCheck(A,1,n) == false)
Print "There is no such element in A";
else
returnSolveB(A,1,n);
c)
We describe a recursive algorithm SolveC(A,r,q) where r<q are natural numbers.
SolveC(A,r,q)
{
if (q < r)
return false;
m = (q + r) div 2;
if (A[m] = m)
return true;
if (A[m] > m)
returnSolveC(A,r,m-1)
else
returnSolveC(A,m+1,q);
}
To solve C we use the call SolveC(A,1,n).
Question 8
You have two jars made from a special secret material. You are in a tall building with N floors, and you must determine what is the top floor M from which you can drop a jar to the ground so that it doesn't break on impact. You can throw a jar out a window as many times as you want as long as it doesn't break. Describe a strategy for finding the highest safe rung that requires you to drop a jar at most f(n) times, for some function f(n) that grows slower than linearly. (In other words, it should be the case that .
Solution:
One is tempted to do a binary search with the first jar, but thisis not that efficient, since if you drop your first sphere from theN/2'th floor and it breaks, you need to do a linear search using thesecond jar from the 1st floor up - which could mean N/2 -1 total throws in the worst case.
The solution is to throw the first jar from floors i*sqrt(N) forincreasing values of i. Once it breaks you have a possible range ofsqrt(N) values of M, so in total you never do more than throws.