简体   繁体   English

具有不同最坏情况上限、最坏情况下限和最佳情况下界的算法示例?

[英]Example of algorithm which has different worst case upper bound, worst case lower bound and best case bounds?

Is there any algorithm A, such that for a set of worst case instances S for A, A has different worst case upper bound and worst case lower bound?是否有任何算法 A,使得对于 A 的一组最坏情况实例 S,A 具有不同的最坏情况上限和最坏情况下限? Moreover it should have different best case bounds not equal to any worst case bounds,for some set of inputs.此外,对于某些输入集,它应该具有不同的最佳情况边界,不等于任何最坏情况边界。

For example say H is a hypothetical algorithm such that H has worst case upper bound Ο(n^3), worst case lower bound Ω(n^2) and best case running time Θ(n).例如,假设 H 是一个假设算法,使得 H 具有最坏情况上限 Ο(n^3)、最坏情况下限 Ω(n^2) 和最佳情况运行时间 Θ(n)。

Let me know that above situation is actually meaningful or not?让我知道上述情况实际上是否有意义?

Thanks :)谢谢 :)

The way you describe it, pick any quadratic sort algorithm, say bubble sort:你描述它的方式,选择任何二次排序算法,比如冒泡排序:

  • Worst case upper bound can be said to be O(n^3) .最坏情况的上限可以说是O(n^3)

  • Worst case lower bound can be said to be Ω(n^2) .最坏情况下界可以说是Ω(n^2)

  • The best case running time can be said to be Θ(n) , when the input is already sorted and you check this with a single pass before starting the algorithm, or use optimizations that terminate the algorithm prematurely in this case.最好的情况运行时间可以说是Θ(n) ,当输入已经排序并且您在开始算法之前通过单次检查它时,或者在这种情况下使用过早终止算法的优化。

Here's a less natural but perhaps more satisfying definition of H .这是H的一个不太自然但可能更令人满意的定义。 This function computes the cube of the sum of the input list in a rather silly manner.这个函数以一种相当愚蠢的方式计算输入列表总和的立方。

def H(lst):
    s1 = 0
    for x in lst:
        s1 += x
    if s1 == 0:
        return 0
    elif len(lst) % 2 == 0:
        s2 = 0
        for x in lst:
            for y in lst:
                s2 += x * y
        return s1 * s2
    else:
        s3 = 0
        for x in lst:
            for y in lst:
                for z in lst:
                    s3 += x * y * z
        return s3

The best-case bound is Theta(n).最好的情况是 Theta(n)。 The tightest worst-case upper bound of the form O(n^k) is O(n^3).形式 O(n^k) 的最严格的最坏情况上限是 O(n^3)。 The tightest worst-case lower bound of the form Ω(n^k) is Ω(n^2). Ω(n^k) 形式的最严格的最坏情况下限是 Ω(n^2)。

Note, however, that the tightest bound of any form on the worst-case is Θ(f(n)) where f(2m) = m^2 and f(2m+1) = m^3.但是请注意,最坏​​情况下任何形式的最紧边界是 Θ(f(n)),其中 f(2m) = m^2 和 f(2m+1) = m^3。

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

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