Complexity of Algorithms
#Math213
Algorithm
Basic
Definition
Comparison
- Time Complexity: number of machine operations
- Space Complexity: amount of memory needed
Instance: 即问题对应的具体输入
Growth of Functions
-
Big-O Notation
注意: big-O 只要求量级大于等于即可,意味着只要找到下界,比下界大的函数都可以
注意 $\log(n!)$ 的量级
证明:
$$
\begin{align}
& \text{On the one hand,}
& \log(n!) = \sum_{k=1}^{n}\log(k) \leq n\log(n)
& \text{On the other hand,}
& \log(n!) = \sum_{k=1}^{n}\log(k) \geq \log\left( \frac{n}{2}+1 \right) + \dots +\log(n) \geq \left( \frac{n}{2} \right)\log\left( \frac{n}{2} \right)
\end{align}
$$
Combination:
-
Big-Omega Notation
Big-Omega: tells us a lower bound of growth rate
-
Big-Theta Notation
同时控制上界与下界,两函数量级相同
Complexity
Example
Insertion Sort:
对于 Average Complexity: 考虑所有情况等可能,计算其复杂度均值(一般情况下难以计算)