Question: Let n > 0 ( usually n is the size of the input ) and let f ( n ) and g ( n )
Let n usually n is the size of the input and let f n and gn be two
monotone and nonnegative functions. We say that f n Ogn if there
exists a constant c and a constant n such that: f n cgnn n
The exercise asks you to use the definition to prove the following:
Let f nnn Then f n On
Let f nn n log n Then f n On log n
Let f nn log n n Then f n On
Feel free to use standard properties or inequalities for the logarithm without
proof.
Ex Imagine you had an algorithm with runtime T n satisfying the recursion
T n T nand T You have probably seen the binary search
algorithm that exactly obeys that recursion. Prove that T n Olog n Then,
imagine another algorithm with Qn Qn n and Q Prove that
Qn On
Ex Describe an On log ntime algorithm that, given a set S of n integers
and another integer x determines whether or not there exist two elements in S
whose sum is exactly x
Ex Follow the O definition & prove: Is n On Is n On
Ex Imagine you came up with an algorithm that depends on a parameter
k in n you can control. After calculations, the runtime ends up being
Onk n
k What choice of k would you make for your algorithm?
Ex This is from a common coding interview question: Imagine you are given a
list of n integers. Assume they are distinct if that helps you. Give an algorithm
that finds the smallest and the second smallest element in the list, using at most
n log n comparisons, and explain why your algorithm needs at most n log n
comparisons. Hint: think again of the pairing process we saw in class, when
trying to find the max and the min simultaneously; where is the second smallest
element located? Here, thinking about tennis or chess tournaments may help
you.
For asymptotic notation exercises, if it helps you can think that n is a power of
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
