Algorithm Analysis
Computational Tractability
Brute Force
Brute Force: for many nontrivial probelms, there is a natural brute-force search algorithm that checks every possible solution.
- Typically takes steps (or worse) for inputs of size .
- Unacceptable in practice.
For the stable matching problem: test all perfect matchings for stability.
Polynomial Running Time
Desirable scaling property: when the input size doubles, the algoirhtm should slow down by at most some multiplicative constant factor .
An algorithm is poly-time if the above scaling property holds:
- There exists constants and such that, for every input of size , the algorithm performs primitive computational steps (corresponds to ).
This doesn't have the desireable scaling property:
These have the desireable scaling property: For example, And,
We say that an algorithm is efficient if it has a polynomial running time. Theory: Definition is (relatively) insensitive to model of computation In practice, it really works because
- the poly-time algorithms that people develop have both small constants and small exponents.
- breaking through the exponential barrier of brute force typically exposes some crucial structure of the problem.
The exception is that some poly-time algorithms in the wild have galactic constants and/or huge exponents.
Worst-case Analysis
Worst case: the running time guarantee for any input of size .
- This generally represents efficiency in practice
- Draconian (excessively harsh) view, but hard to find effecive alternative
Exceptions: some exponential-time algorithms are used widely in practice because the worst-case instsances don't arise
Other types of analyses
Probabilistic: expected running time of a randomized algorithm.
- Ex: the expected number of compares to quicksort elements is ~
Amortized: worst-case running time for any sequence of operations
- Ex: starting from an empty stack, any sequence of push and pop operations takes primitive computational steps using a resizing array.
There is also: average-case analysis, smoothed analysis, competitive anlysis
Asymptotic Order of Growth
Big O Notation
Represents the upper bounds. is if there exist constants and such that for all .
Ex:
- is
- is neither nor because there does not exist constants , such that ,
Typical usage: insertion sort makes compares to sort elements.
One-way "equality" is a set of functions, but computer scientists often write instead of .
- Ex: consider and
- we have and , but we cannot conclude that
Domain and codomain: and are real-valued functions.
- The domain is typically the natural numbers where represent the input size, recurrence relations.
- Sometimes we extend to the reals: where represent plotting, limits, calculus
- Or restrict to a subset.
The bottom line is that it is okay to abuse notation in this way, but it is not okay to misuse it.
Properties
Reflexivity: is Constants: If is and then is . Products: If is and is , then is . The proof is as follows: - and such that for all . - and such that for all . - Then, for all .
Sums: If is and is , then is (the max will ignore lower-order terms).
- Ex: and , so .
Transitivity: if is and is , then is .
Big Omega Notation
Represents the lower bounds. is if there exists constants and such that for all .
Ex: .
- is both and , but is not .
- , when .
Typically used for any compare-based sorting algorithm that requires compares in the worst case.
Bit Theta Notation
Represents the tight bounds. is if there exists constants , and such that for all .
Example: .
- (choose
- is neither nor
Typical usage: merge sort makes compares to sort elements.
Asymptotic Bounds
Proposition: If for some constant then is . Proof:
- By definition of the limit, for any , there exists such that for all .
- Choose .
- Multiplying by yields for all ,
- Thus, is by definition, with and
Proposition: If , then is but not . Proposition: If , then is but not .
The asymtomatic bounds for some common functions:
Polynomials: Let with . Then, is . Proof:
Logarithms: is (log base does not need to be specified if it is a constant) for every and every . Proof:
Logarithms and polynomials: is for every and every Proof:
Exponentials and polynomials: is for every and every Proof:
Factorials: is . Proof:
- Stirling's formula says
Algorithm Analysis
We want to implement the gale-shapley to run in time.
- To represent hospitals and students we index them .
- To represent a matching, we maintain two arrays and . When is matched with , then and . Use the value to designate that hospital or student is unmatched.
- Can add/remove a pair from matching in time.
- Maintain a set of unmatched hospitals in a queue (or stack)
- Can find an unmatched hospital in time.
Hospital makes a proposal data representation
- key operaion: find hospital's next favorite student
- for each hospital: maintain a list of students, ordered by preference
- for each hospital: maintain a pointer to student for next proposal.
- making a proposal takes O(1) time.
Student accepts/rejects a proposal data representation
- does student perfer hosiptal to hospital ?
- for each studnet, create inverse of preference list of hospitals. (a ranking list).
- after preprocessing time (to create the ranking arrays), it takes time to accept/reject a proposal.
Theorem: we can implement Gale-Shapley to run in time. Proof:
- preprocessing time to create ranking arrays.
- There are proposals; processing each proprosla takes time.
Common Running Times
Constant time has running time of .
- this means that the runtime does not depend on the input size .
- e.g., conditional branch, arithmetic/logic operation, declare/initialize variable
Linear time has running time of .
- e.g., merge two sorted list.
Logarithmic time has running time of .
- e.g., search in a sorted array, binary search
Linearithmic time has running time of .
- e.g., given an array of elements, rearrange them in ascending order, mergesort
Quadratic time has running time of .
- e.g.,: closest pair of points. Given a list of points in a plane , find the pair of points that is closest to each other; enumerate all pairs of points.
Cubic time: running time is .
- e.g., 3 sym, enumerate all triples
Polynomial time: running tim eis for some constant .
- E.g., Independent set of size (a constant). Given a graph, find nodes such that no two are joined by an edge; enuermate all subsets of nodes, the number k-elemet subsets is n-choose-k.
Exponential time: running time is for some constant .
- e.g., euclidean TSP (given points in th eplane, find a tour of minimum length); algorithm. Enumerate all permutations of length n.
Linked from Algorithms