简体   繁体   English

算法分析 - 渐近分析

[英]Algorithm Analysis - Asymptotic analysis

Hi i have started learning algorithm analysis.嗨,我已经开始学习算法分析。 Here i have a doubt in asymptotic analysis.这里我对渐近分析有疑问。 Let's say i have a function f(n) = 5n^3 + 2n^2 + 23. Now i need to find the Big-Oh, Big-Omega and Theta Notations for the above function,假设我有一个函数 f(n) = 5n^3 + 2n^2 + 23。现在我需要找到上述函数的 Big-Oh、Big-Omega 和 Theta Notations,

Big-Oh: 
    f(n) <= (5 + 2 + 23) n^3  // raising all the n's to the power of 3 will give us a value which will be always greater than f(n)
    f(n) <= 30n^3
    f(n) belongs to Big-Oh(n^3)

Big-Omega:
    n^3 <= f(n)
    f(n) belongs to Big-Omega(n^3)

Theta: 
    n^3 <= f(n) <= 30 n^3 
    f(n) belongs to Theta ( n^3)
So here, 
    f(n) belongs to Big-Oh(n^3)
    f(n) belongs to Big-Omega(n^3)
    f(n) belongs to Theta(n^3)

Like this for any polynomial,the order of growth for Oh,Omega and Theta Notations are same(in our case it is order of n^3).对于任何多项式,像这样,Oh、Omega 和 Theta Notations 的增长顺序是相同的(在我们的例子中是 n^3 的顺序)。 When order of growth will be same for all the notations, then what is the use of showing them with different notations and where exactly it can be used?当所有符号的增长顺序都相同时,那么用不同的符号显示它们有什么用以及它可以在哪里使用? Kindly give me a practical example if possible.如果可能的话,请给我一个实际的例子。

Big theta (Θ) is when our upper bound (O) and lower bound (Ω) are the same, in other words it's a tight bound then.大 theta (Θ) 是当我们的上限(O) 和下限(Ω) 相同时,换句话说,这是一个紧边界。 That's one reason to show both O and Ω (or well all three).这是同时显示OΩ (或全部三个)的原因之一。

Why is this useful?为什么这很有用? Because Θ is a tight bound - it's much stronger than O .因为Θ是一个严格的界限- 它比O强得多。 With O you can say that the above is O(n^1000) and you are still technically correct.对于O,您可以说以上是 O(n^1000) 并且您在技术上仍然是正确的。 A lot of times O != Ω and you don't have that tight bound.很多时候O != Ω并且您没有那么严格的界限。

Why are we usually talking about O usually, then?那么,为什么我们通常通常谈论O呢? Well because in most cases we are interested in the upper bound (of the worst case scenario) of our algorithm.嗯,因为在大多数情况下,我们对算法的上限(最坏情况)感兴趣。 Sometimes we simply don't know the Θ and we go with O instead.有时我们根本不知道Θ而我们用O代替。 Also it's important to notice that many people simply misuse those symbols, don't fully understand them and/or are simply not precise enough and use O in places where Θ could be used.同样重要的是要注意,许多人只是滥用了这些符号,没有完全理解它们和/或不够精确,并在可以使用Θ 的地方使用O。

For instance quicksort does not have a tight bound (unless we are talking specifically about either best/average or worst case analysis) as it's Ω(nlogn) in the best and average cases but O(n^2) in the worst case.例如,快速排序没有严格的界限(除非我们专门讨论最佳/平均或最坏情况分析),因为它在最好和平均情况下是 Ω(nlogn) 而在最坏情况下是 O(n^2)。 On the other hand mergesort is both Ω(nlogn) and O(nlogn) therefore it's also Θ(nlogn).另一方面,归并排序既是 Ω(nlogn) 又是 O(nlogn),因此它也是 Θ(nlogn)。

All in all it's all theoretical as quicksort in practics is in most cases faster as you usually don't hit the worst case and the operations done by quicksort are easier.总而言之,这都是理论上的,因为实践中的快速排序在大多数情况下更快,因为您通常不会遇到最坏的情况,并且快速排序完成的操作更容易。

In the example you gave, the execution time is known and seems to be fixed.在您给出的示例中,执行时间是已知的并且似乎是固定的。 So it is in O(n^3), Ω(n^3) and thus in Θ(n^3) for all cases.所以它在 O(n^3)、Ω(n^3) 中,因此在所有情况下都在 Θ(n^3) 中。

However, for an algorithm, the execution time may, and not so rarely, depend on the input the algorithm is running on.然而,对于一个算法来说,执行时间可能取决于运行算法的输入,但并非如此。

Eg.: to search a key in a linked list takes going through all the list members in the worst case and that's linear time.例如:在最坏的情况下搜索链表中的键需要遍历所有列表成员,这是线性时间。 In the best case, the key you're looking for at that time is in the very beginning of the list and that's constant time.在最好的情况下,您当时正在寻找的键位于列表的最开头,并且是恒定时间。 So the algorithm is in O(n) and in Ω(1)=O(1).所以算法在 O(n) 和 Ω(1)=O(1) 中。 There's no valid f(n) to specify Θ(f(n)) for this algorithm.没有有效的 f(n) 来指定该算法的 Θ(f(n))。

Above Function Running Time can be computed in the following manner上述函数运行时间可以通过以下方式计算

  • Is it omega and big o of n ?是 omega 和 n 的大 o 吗? 5n^3+2n^2+23 >=cn 5n^2+2n+23/n >= c as n grows and it tends to infinite such constant c can exist which is smaller than or equals to left hand side of the inequality so the function running time is omega of n. 5n^3+2n^2+23 >=cn 5n^2+2n+23/n >= c 随着 n 的增长并且趋于无限,这样的常数 c 可以存在,它小于或等于不等式的左侧所以函数运行时间是 n 的欧米茄。 5n^2+2n+23/n <= c when n tends to infinite this inequality doesn't hold true because such constant can not exist which is equal to or greater than Left hand side of the inequality so the function running time is not big o of n. 5n^2+2n+23/n <= c 当 n 趋于无穷大时,这个不等式不成立,因为这样的常数不能存在等于或大于不等式的左侧,所以函数运行时间不是n. 的大 o
  • is it omega and Big o of n ^3 ?是 n ^3 的 omega 和 Big o 吗? 5n^3+2n^2+23 >=cn^3 5 + 2/n + 23/n^3 >=c this inequality holds true so it's omega of n^3. 5n^3+2n^2+23 >=cn^3 5 + 2/n + 23/n^3 >=c 这个不等式成立,所以它是 n^3 的欧米茄。 5 + 2/n + 23/n^3 <=c this inequality also holds true so it's big o of n^3. 5 + 2/n + 23/n^3 <=c 这个不等式也成立,所以它是 n^3 的大 o。 Since it's omega and big o of n^3 hence it's theta of n^3 as well.由于它是 omega 和 n^3 的大 o,因此它也是 n^3 的 theta。 Similarly its omega of n^ 2 but not big o of n^2.类似地,它的 omega 为 n^2,但不是 n^2 的大 o。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM