算法分析
Analysis of Algorithms
学到这里才发现之前用 Big-Oh notation
Mathematical Models
The total running time of an algorithm is the sum of cost
However, taking exactly all operations into consideration seems too tedious.
Simplification 1: cost model
Cost model. Use some basic operations as a proxy for running time.
When a proper cost model is chosen, other operations could be ignored.
Simplification 2: tilde notation
We use tilde notation
- When
is large, lower-order terms are negligible. - When
is small, we simply don't care.
Technically,
Order-of-Growth Classifications
Good news. the small set of functions
Here are the common order-of-growth classifications:
- constant
: statement - logarithmic
: divide in half - linear
: loop - linearithmic
: divide and conquer - quadratic
: double loops - cubic
: triple loops - exponential
: exhaustive search
Typically, better order of growth
Commonly Used Notations
| notation | provides | used to |
|---|---|---|
| Tilde |
leading term | provide approximate model |
| Big Theta |
asymptotic growth rate | classify algorithms |
| Big Oh |
develop upper bounds | |
| Big Omega |
develop lower bounds |
Common mistake. Interpreting big-Oh as an approximate model.
Example: 1-SUM problem
-
Upper bound. A specific algorithm.
Brute-force algorithm for 1-SUM: Look at every array entry. Running time of the optimal algorithm for 1-SUM is
. -
Lower bound. Proof that no algorithm can do better.
Have to examine all
entries (any unexamined one might be ). Running time of the optimal algorithm for 1-SUM is .
If for a specific problem, upper bound
Algorithm design aims to reduce the gap between upper bound
