What is the difference between Θ(n) and O(n)?

后端 未结 9 1057

Sometimes I see Θ(n) with the strange Θ symbol with something in the middle of it, and sometimes just O(n). Is it just laziness of typing because nobody knows how to type th

相关标签:
9条回答
  • 2020-11-22 05:01

    f(n) belongs to O(n) if exists positive k as f(n)<=k*n

    f(n) belongs to Θ(n) if exists positive k1, k2 as k1*n<=f(n)<=k2*n

    Wikipedia article on Big O Notation

    0 讨论(0)
  • 2020-11-22 05:07

    A chart could make the previous answers easier to understand:

    Θ-Notation - Same order | O-Notation - Upper bound

    Θ(n) - Same order O(n) - Upper bound

    In English,

    On the left, note that there is an upper bound and a lower bound that are both of the same order of magnitude (i.e. g(n) ). Ignore the constants, and if the upper bound and lower bound have the same order of magnitude, one can validly say f(n) = Θ(g(n)) or f(n) is in big theta of g(n).

    Starting with the right, the simpler example, it is saying the upper bound g(n) is simply the order of magnitude and ignores the constant c (just as all big O notation does).

    0 讨论(0)
  • 2020-11-22 05:07

    Using limits

    Let's consider f(n) > 0 and g(n) > 0 for all n. It's ok to consider this, because the fastest real algorithm has at least one operation and completes its execution after the start. This will simplify the calculus, because we can use the value (f(n)) instead of the absolute value (|f(n)|).

    1. f(n) = O(g(n))

      General:

                f(n)     
      0 ≤ lim ──────── < ∞
          n➜∞   g(n)
      

      For g(n) = n:

                f(n)     
      0 ≤ lim ──────── < ∞
          n➜∞    n
      

      Examples:

          Expression               Value of the limit
      ------------------------------------------------
      n        = O(n)                      1
      1/2*n    = O(n)                     1/2
      2*n      = O(n)                      2
      n+log(n) = O(n)                      1
      n        = O(n*log(n))               0
      n        = O(n²)                     0
      n        = O(nⁿ)                     0
      

      Counterexamples:

          Expression                Value of the limit
      -------------------------------------------------
      n        ≠ O(log(n))                 ∞
      1/2*n    ≠ O(sqrt(n))                ∞
      2*n      ≠ O(1)                      ∞
      n+log(n) ≠ O(log(n))                 ∞
      
    2. f(n) = Θ(g(n))

      General:

                f(n)     
      0 < lim ──────── < ∞
          n➜∞   g(n)
      

      For g(n) = n:

                f(n)     
      0 < lim ──────── < ∞
          n➜∞    n
      

      Examples:

          Expression               Value of the limit
      ------------------------------------------------
      n        = Θ(n)                      1
      1/2*n    = Θ(n)                     1/2
      2*n      = Θ(n)                      2
      n+log(n) = Θ(n)                      1
      

      Counterexamples:

          Expression                Value of the limit
      -------------------------------------------------
      n        ≠ Θ(log(n))                 ∞
      1/2*n    ≠ Θ(sqrt(n))                ∞
      2*n      ≠ Θ(1)                      ∞
      n+log(n) ≠ Θ(log(n))                 ∞
      n        ≠ Θ(n*log(n))               0
      n        ≠ Θ(n²)                     0
      n        ≠ Θ(nⁿ)                     0
      
    0 讨论(0)
提交回复
热议问题