Example of algorithm which has different worst cas

2019-04-16 02:11发布

问题:

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.