Вы находитесь на странице: 1из 5

Practical Session 2 Constants Dont Matter!!!

Algorithm Analysis

f(n) = O(g(n))
There exist c > 0 and n
0
> 0 such that:

0 f(n) cg(n) for each n n
0

f(n) = (g(n))
There exist c > 0 and n
0
> 0 such that:

0 cg(n) f(n) for each n n
0

f(n) = (g(n))
There exist c
1
, c
2
> 0 and n
0
> 0 such that:

0 c
1
g(n) f(n) c
2
g(n) for each n n
0


Some Basics
Prove: 100 * n + 12 = (n).
Solution:


Prove:

).
Solution:




Question 1
Prove : (log n)
log n
= (n
3/2
).
Solution:
cn
3/2
= c(2
log n
)
3/2
= c(2
3/2
)
log n
(log n)
log n



cn
3/2
(log n)
log n

c = 1
n
0
= 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 n i
n
i
log log
1
s

=
log(n!) = O(n logn)

n cn
n
n
n
n
n n
i i
n
n
n
i
n
i
log
2
log
2
) 2 log (log
2
log
2
log log
*
2
2
1
> = = > >

=
=
log(n!) =
(n logn)
Explanation of the inequality:
*
> : n cn
n
n
n
log
2
log
2
> => n c n log
2
1
log
2
1
>









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! > 2
k
.

Hence, (logn)! = (logn)(logn -1)! > (logn) 2
logn -1

= (logn) (n/2) cn logn.


n c n log
2
1
log
2
1
>

c = 0.25
n
0
= 8
log(n!) = (n log n)
(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) = n
1-cos n
.
Note that the running times has to be greater than zero therefore sin(n),cos(n) do no fit
here.

Question 5
Prove: For any two functions f(n) and g(n), f(n) +g(n) = (max(f(n),g(n))).
Solution:
Observe that: )). ( ), ( max( 2 ) ( ) ( )) ( ), ( max( n g n f n g n f n g n f s + s
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)=O(f(n/2)) .
Solution:
1. f(n)=O(g(n)) f(n) cg(n) g(n) 1/cf(n) g(n)= (f(n)).
2. False, e.g., f(n) = 2
n
. Then f(n) = 2
n
O(2
n/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 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

()

).

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 () throws.

Вам также может понравиться