Difference between Big-Theta and Big O notation in simple language

后端 未结 6 989
既然无缘
既然无缘 2020-12-04 09:08

While trying to understand the difference between Theta and O notation I came across the following statement :

The Theta-no         


        
6条回答
  •  难免孤独
    2020-12-04 09:59

    I am going to use an example to illustrate the difference.

    Let the function f(n) be defined as

    if n is odd f(n) = n^3
    if n is even f(n) = n^2
    

    From CLRS

    A function f(n) belongs to the set Θ(g(n)) if there exist positive constants c1 and c2 such that it can be "sandwiched" between c1g(n) and c2g(n), for sufficiently large n.

    AND

    O(g(n)) = {f(n): there exist positive constants c and n0 such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0}.

    AND

    Ω(g(n)) = {f(n): there exist positive constants c and n0 such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0}.

    The upper bound on f(n) is n^3. So our function f(n) is clearly O(n^3).

    But is it Θ(n^3)?

    For f(n) to be in Θ(n^3) it has to be sandwiched between two functions one forming the lower bound, and the other the upper bound, both of which grown at n^3. While the upper bound is obvious, the lower bound can not be n^3. The lower bound is in fact n^2; f(n) is Ω(n^2)

    From CLRS

    For any two functions f(n) and g(n), we have f(n) = Θ(g(n)) if and only if f(n) = O(g(n)) and f(n) = Ω(g(n)).

    Hence f(n) is not in Θ(n^3) while it is in O(n^3) and Ω(n^2)

提交回复
热议问题