Solution

advertisement
Practical Session 2 – “Constants Don’t Matter!!!”
Algorithm Analysis
There exist c > 0 and n0 > 0 such that:
f(n) = O(g(n))
f(n) = Ω(g(n))
f(n) = Θ(g(n))
0 ≤ f(n) ≤ cg(n)
for each n ≥ n0
There exist c > 0 and n0 > 0 such that:
0 ≤ cg(n) ≤ f(n)
for each n ≥ n0
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:
š‘1 š‘› ≤ 100š‘› + 12 → š’„šŸ = šŸ, š‘”š‘Ÿš‘¢š‘’ š‘“š‘œš‘Ÿ š’†š’—š’†š’“š’š š’
100š‘› + 12 ≤ š‘2 š‘› →
š‘2 ≥ 100 +
12
→
š‘›
š’„šŸ = šŸšŸŽšŸ’, š‘”š‘Ÿš‘¢š‘’ š‘“š‘œš‘Ÿ š‘’š‘£š‘’š‘Ÿš‘¦ š’ ≥ šŸ‘
Prove: š‘›3 + š‘›2 + š‘› = š‘¶(š‘›3 ).
Solution:
š‘›3 + š‘›2 + š‘› ≤ š‘š‘›3 → š’„ = šŸ, š‘”š‘Ÿš‘¢š‘’ š‘“š‘œš‘Ÿ š’†š’—š’†š’“š’š š’ ≥ šŸ
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
cn3/2 ≤ (log n)log n
c=1
n0 = 8
Question 2
Prove that log(n!) = Θ(n logn).
Solution:
log (n!) = log(1*2*3*…*n) = log 1 + log 2 + log 3 + … + logn.
n
ļƒ„ log i ļ‚£ n log n
ļƒž log(n!) = O(n logn)
i ļ€½1
n
n
ļƒ„ log i ļ‚³ ļƒ„ log i ļ‚³
i ļ€½1
iļ€½
n
2
n
n
n
n
log n2 ļ€½ (log n ļ€­ log 2) ļ€½ log n ļ€­ ļ‚³* cn log n ļƒž log(n!) =
2
2
2
2
Ω(n logn)
Explanation of the inequality:
n
n
1
1
ļ‚³ * : log n ļ€­ ļ‚³ cn log n => log n ļ€­ ļ‚³ c log n
2
2
2
2
1
1
log n ļ€­ ļ‚³ c log n
2
2
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)! = Ω(n logn).
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.
We will prove that g(n) ≠ O(f(n)):
Suppose there exists a constant c such that: n ≤ cāˆ™ n1-cos n for each n ≥ n0. Then
1 ≤ cāˆ™ n-cos n ļƒž ncos n ≤ c for each n ≥ n0.
There exists an infinite series of numbers nk for which 0.1 < cos(nk). So there exist
infinite many numbers n for which ncos n is a positive polynomial, while c is a
constant. So there do not exist c and n0 such that for each n ≥ n0 ncos n ≤ c.
Contradiction!
We will prove that g(n) ≠ ā„¦(f(n)):
Suppose there exists a constant c such that: cāˆ™n ≤ n1-cos n for each n ≥ n0. Then
c ≤ n-cos n for each n ≥ n0.
There exists an infinite series of numbers nk for which cos(nk) < -0.1. So there exist
infinite many numbers n for which n-cos n is a positive polynomial, while c is a
constant. So there do not exist c and n0 such that for each n ≥ n0 c ≤ n-cos n.
Contradiction!
Question 5
Prove: For any two positive functions f(n) and g(n), f(n) +g(n) = Θ(max(f(n),g(n))).
Solution:
Observe that: max( f (n), g (n)) ļ‚£ f (n) ļ€« g (n) ļ‚£ 2 ļƒ— max( f (n), g (n)).
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:
1. f(n)=O(g(n)) → f(n) ≤cāˆ™g(n) → g(n) ≥ 1/cāˆ™f(n) → g(n)= ā„¦ (f(n)).
2. False, e.g., f(n) = 2n. Then f(n) = 2n ≠ ļ‘(2n/2)= f(n/2).
We will prove that 2n ≠ O (2n/2):
Suppose there exists a constant c such that: 2n ≤ cāˆ™2n/2 for each n ≥ n0. Then
2n/2 ≤ c for each n ≥ n0. So c is not a constant, because it is bigger than a
diverging expression. Contradiction!
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
integer 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 (1≤i≤n). 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 run SimpleCheck(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)
return SolveB(A,r,m);
else
return SolveB(A,m,q);
}
To solve b we use the following algorithm:
if (SimpleCheck(A,1,n) == false)
Print "There is no such element in A";
else
return SolveB(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)
return SolveC(A,r,m-1)
else
return SolveC(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 lim š‘› = 0 ).
š‘›→∞
Solution:
One is tempted to do a binary search with the first jar, but this is not that efficient,
since if you drop your first sphere from the N/2'th floor and it breaks, you need to do a
linear search using the second 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) for increasing values of i.
Once it breaks you have a possible range of sqrt(N) values of M, so in total you never
do more than š‘“(š‘›) = 2√š‘› − 1 throws.
Download