I.1.2 Analysis of Algorithms
Algorithm I Week 1: Analysis of Algorithms
Last updated
Algorithm I Week 1: Analysis of Algorithms
Last updated
Observations -> Mathematical Models -> Order-of-Growth
Observations RunTime ->
Mathematical Models -> f(N)
Order
Original Solution
Runtime:
Improvements
Best Case
Worst Case
Average Case
Establish "difficulty" of a problem
~(approximate model)
(Θ(N2) and larger, used to develop lower bounds)
(Θ(N2) and smaller, used to develop upper bounds) (often misused)
(asymptotic order of growth, used to classify algorithms)
When lower bound equals upper bound (to within a constant factor), it is optimal. The one we often times use to represent the runtime of a model is actually the tilde notation (Big Oh is a misuse, according to prof. Robert Sedgewick )
1. 3-SUM in quadratic time. Design an algorithm for the 3-SUM problem that takes time proportional to n2 in the worst case. You may assume that you can sort the nn integers in time proportional to n2 or better.
Note: these interview questions are ungraded and purely for your own enrichment. To get a hint, submit a solution.
2. Search in a bitonic array. An array is bitonic if it is comprised of an increasing sequence of integers followed immediately by a decreasing sequence of integers. Write a program that, given a bitonic array of nn distinct integer values, determines whether a given integer is in the array.
Standard version: Use ∼3lgn cmpares in the worst case.
Signing bonus: Use ∼2lgn compares in the worst case (and prove that no algorithm can guarantee to perform fewer than ∼2lgn compares in the worst case).
monotonic side lgn + bitonic side 2*lgn
see search_bitonic.py
Egg drop. Suppose that you have an n-story building (with floors 1 through n) and plenty of eggs. An egg breaks if it is dropped from floor T or higher and does not break otherwise. Your goal is to devise a strategy to determine the value of T given the following limitations on the number of eggs and tosses:
Version 0: 1 egg, ≤T tosses.
Version 1: ∼1lgn eggs and ∼1lgn tosses.
Version 2: ∼lgT eggs and ∼2lgT tosses.
Version 3: 2 eggs and ∼2n tosses.
Version 4: 2 eggs and ≤cT tosses for some fixed constant cc.
Hints:
Version 0: sequential search.
Version 1: binary search.
Version 2: find an interval containing of size , then do binary search.
0, 2, 4, 8, 16, 32 -> 16, +2, +4, +8, +16 -> 24, +2, +4, +8 -> 28, +2, +4 -> 30, 31
0, 2, 4, 8, 16, 32 .. T ->
Version 3: find an interval of size , then do sequential search. Note: can be improved to tosses.
for i in range(1, n, step = ceil(sqrt(n))): if break: for j in range(n-step, n)
Version 4: . Aim for .