ACM算法-时间复杂度分析(3.渐进符号)

Analysis of Algorithms | Set 3 (Asymptotic Notations)

We have discussed Asymptotic Analysis, and Worst, Average and Best Cases of Algorithms. The main idea of asymptotic analysis is to have a measure of efficiency of algorithms that doesn’t depend on machine specific constants, and doesn’t require algorithms to be implemented and time taken by programs to be compared. Asymptotic notations are mathematical tools to represent time complexity of algorithms for asymptotic analysis. The following 3 asymptotic notations are mostly used to represent time complexity of algorithms.

thetanotation1) Θ Notation: The theta notation bounds a functions from above and below, so it defines exact asymptotic behavior.
A simple way to get Theta notation of an expression is to drop low order terms and ignore leading constants. For example, consider the following expression.
3n3 + 6n2 + 6000 = Θ(n3)
Dropping lower order terms is always fine because there will always be a n0 after which Θ(n3) has higher values than Θn2) irrespective of the constants involved.
For a given function g(n), we denote Θ(g(n)) is following set of functions.

Θ(g(n)) = {f(n): there exist positive constants c1, c2 and n0 such 
                 that 0 <= c1*g(n) <= f(n) <= c2*g(n) for all n >= n0}

The above definition means, if f(n) is theta of g(n), then the value f(n) is always between c1*g(n) and c2*g(n) for large values of n (n >= n0). The definition of theta also requires that f(n) must be non-negative for values of n greater than n0.

BigO2) Big O Notation: The Big O notation defines an upper bound of an algorithm, it bounds a function only from above. For example, consider the case of Insertion Sort. It takes linear time in best case and quadratic time in worst case. We can safely say that the time complexity of Insertion sort is O(n^2). Note that O(n^2) also covers linear time.
If we use Θ notation to represent time complexity of Insertion sort, we have to use two statements for best and worst cases:
1. The worst case time complexity of Insertion Sort is Θ(n^2).
2. The best case time complexity of Insertion Sort is Θ(n).

The Big O notation is useful when we only have upper bound on time complexity of an algorithm. Many times we easily find an upper bound by simply looking at the algorithm.

O(g(n)) = { f(n): there exist positive constants c and 
                  n0 such that 0 <= f(n) <= cg(n) for 
                  all n >= n0}

BigOmega3) Ω Notation: Just as Big O notation provides an asymptotic upper bound on a function, Ω notation provides an asymptotic lower bound.

Ω Notation< can be useful when we have lower bound on time complexity of an algorithm. As discussed in the previous post, the best case performance of an algorithm is generally not useful, the Omega notation is the least used notation among all three.

For a given function g(n), we denote by Ω(g(n)) the set of functions.

Ω (g(n)) = {f(n): there exist positive constants c and
                  n0 such that 0 <= cg(n) <= f(n) for
                  all n >= n0}.

Let us consider the same Insertion sort example here. The time complexity of Insertion Sort can be written as Ω(n), but it is not a very useful information about insertion sort, as we are generally interested in worst case and sometimes in average case.

Exercise:
Which of the following statements is/are valid?
1. Time Complexity of QuickSort is Θ(n^2)
2. Time Complexity of QuickSort is O(n^2)
3. For any two functions f(n) and g(n), we have f(n) = Θ(g(n)) if and only if f(n) = O(g(n)) and f(n) = Ω(g(n)).
4. Time complexity of all computer algorithms can be written as Ω(1)

### ACM竞赛中常见算法时间复杂度总结 #### 1. 排序算法时间复杂度ACM竞赛中,排序是一个常见的操作。以下是几种常用的排序算法及其时间复杂度- **插入排序** - 最好时间复杂度:$O(n)$ (当输入数组已经有序时)[^2]。 - 最坏时间复杂度:$O(n^2)$ (当输入数组完全逆序时)- **合并排序** - 最好时间复杂度:$O(n \log n)$。 - 最坏时间复杂度:$O(n \log n)$。 - **堆排序** - 最好时间复杂度:$O(n \log n)$。 - 最坏时间复杂度:$O(n \log n)$。 - **快速排序** - 最好时间复杂度:$O(n^2)$ (极端情况下,每次划分都极不平衡)[^2]。 - 最坏时间复杂度:$O(n \log n)$ (通常情况下)。 #### 2. 图论算法时间复杂度 图论问题是ACM竞赛中的重要部分,涉及多种经典算法。以下是一些常用图论算法时间复杂度- **Dijkstra算法** - 如果采用朴素实现,在稠密图上的时间复杂度为 $O(n^2)$[^3]。 - 使用优先队列优化后,在稀疏图上的时间复杂度为 $O(m \log n)$,其中 $m$ 是边的数量,$n$ 是顶点数量。 - **SPFA算法** - 平均时间复杂度为 $O(km)$,其中 $k$ 是一个小于等于 2 的常数。 - SPFA适用于处理带负权边的情况,但在网格图或非常稠密的图上表现较差。 #### 3. 动态规划与递归算法时间复杂度 动态规划和递归也是ACM竞赛的核心内容之一。它们的时间复杂度取决于状态转移方程的设计和问题规模。 - **斐波那契数列计算** - 迭代方法的时间复杂度为 $O(n)$[^5]。 - 矩阵快速幂方法可以将时间复杂度降低至 $O(\log n)$。 - **背包问题** - 对于0/1背包问题,如果物品数量为$n$,容量为$c$,则其时间复杂度为 $O(nc)$。 #### 4. 其他基础算法时间复杂度 除了上述类别外,还有一些基本算法也经常用于ACM竞赛中: - **二分查找** - 时间复杂度为 $O(\log n)$,前提是数据结构支持随机访问且已排序。 - **暴力枚举** - 枚举所有可能解的空间大小决定了时间复杂度。例如对于长度为$n$的序列全排列,时间复杂度为 $O(n!)$。 ```python def factorial_recursive(n): if n == 0 or n == 1: return 1 else: return n * factorial_recursive(n - 1) print(factorial_recursive(5)) # 输出120 ``` 以上代码展示了通过递归方式求解阶乘函数的一个例子,该过程具有指数级增长特性即 $O(n!)$。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值