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

落爺英雄遲暮 提交于 2019-12-06 12:38:53

问题


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? 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).

Let me know that above situation is actually meaningful or not?

Thanks :)


回答1:


Here's a less natural but perhaps more satisfying definition of 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). The tightest worst-case upper bound of the form O(n^k) is O(n^3). The tightest worst-case lower bound of the form Omega(n^k) is Omega(n^2).

Note, however, that the tightest bound of any form on the worst-case is Theta(f(n)) where f(2m) = m^2 and f(2m+1) = m^3.




回答2:


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).

  • Worst case lower bound can be said to be Ω(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.



来源:https://stackoverflow.com/questions/25835500/example-of-algorithm-which-has-different-worst-case-upper-bound-worst-case-lowe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!