Big O and Big Omega are the same but in reverse?

我怕爱的太早我们不能终老 提交于 2019-12-02 16:50:19

问题


Is this true?

f(n) = O(g(n))  === g(n) = Omega(f(n))

Basically are they interchangeable because they are opposites?

So if F is in Big O of G, then G is Big Omega of F?


回答1:


I've never particularly cared for the notation myself. The easiest way to think of this is that Big-O notation is the "worst case" and Big Omega is the "best case."

There are, of course, other things you might be interested in. For example, you could state that the following (rather dumb) linear search algorithm is O(n), but it would be more precise to state that it'll always be exactly proportional to n:

public bool Contains(List<int> list, int number)
{
    bool contains = false;
    foreach (int num in list)
    {
      // Note that we always loop through the entire list, even if we find it
      if (num == number)
         contains = true;
    }

    return contains;
}

We could, alternatively, state that this is both O(n) and Omega(n). For this case, we introduce the notation of Theta(n).

There are other cases too, such as the Average case. The average case will often be the same as either the best or worst case. For example, the best case of a well-implemented linear search is O(1) (if the item you're looking for is the first item on the list), but the worst case is O(n) (because you might have to search through the entire list to discover that the item's not there). If the list contains the item, on average, it'll take n/2 steps to find it (because we will, on average, have to look through half of the list to find the item). Conventionally, we drop the "/2" part and just say that the average case is O(n).

They don't strictly have to be the same though. I've seen some argument about whether the "best" case for a binary search tree search should be considered O(1) (because the item you're looking for might be the first item on the tree) or if it should be considered O(log n) (because the "optimal" case for a binary search is if the tree is perfectly balanced). (See here for a discussion of BST insertion). The average case is definitely O(log n) though. The worst case is O(n) (if the binary search tree is completely unbalanced). If we take the best case to be O(1), the average case to be O(log n), and the worst case to be O(n), then the average, worst, and best case would all obviously be different.




回答2:


It helps to look at the definitions:

f(n) is in O(g(n)) if and only if:
There are positive constants c and k, such that 0 ≤ f(n) ≤ cg(n) for all n ≥ k.


g(n) is in Omega(f(n)) if and only if: 
There are positive constants c and k, such that g(n)  ≥ cf(n)  ≥ 0 for all n ≥ k.  

If you can find values of c and k which make f(n) in O(g(n)), then the same values will also show g(n) to be Omega(f(n)) (just divide both sides of the inequality by c). That's why they are interchangeable.

F(n) is not Theta(g(n)) unless it is both in O(g(n) and Omega(g(n)).

Hope that helps!



来源:https://stackoverflow.com/questions/39799848/big-o-and-big-omega-are-the-same-but-in-reverse

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