Big O,Big Omega,Big Theta功能

时间:2016-01-22 21:43:12

标签: algorithm big-o

This is the question

我正在上一个数据结构课,我对如何处理这类问题感到困惑。任何线索都会有所帮助,在此先感谢。

1 个答案:

答案 0 :(得分:1)

让我解释其中一个,看看你是否可以尝试其余的。

f(n) is O( g(n) )   "function f of n" is ("big O") **Order** g(n)

if for some n (maybe not f(0) or f(1)  or... but eventually for some n)
and for some **constant** (1, 2, 50, 80 million, something)

f(n) <= c * g(n)

So if we say some function f is "O(log n)" than means that starting at
some  n  that we pass into f(), and some number  c   then

    f(n) <= c * log(n)

让我们举一个非常简单的例子:

function f ( n ) {

     answer = 0;
     for (i = 0; i < n; ++i) {        // loops n times
         answer += n+3;                // 2 ops: add, add
         answer /= (7*n);              // 2 ops: mult, div
         answer ^= 2;                  // 1 op:  xor
     }                                 // 2 + 2 + 1 = 5
     return answer;
}

所以,我们可能会说'c'是5,g(n)是n(我们显然循环n次)。

f (n)   <=   5 * g(n)
f (n)   <=   5 * n

f () is O(n)

基本上这就是说,当n变得足够大时,常数因素根本不重要。如果f(n)是(5n)或(7n)或(27n),我们可以将它与其他函数(87log(n))或(0.01n²)进行比较,几乎没有区别。

     \ n   10     1000      100000
 f(n) \-----------------------------
    7n |   70     7000      700000       O(n) grows linearly with prob size
87logn | ~200     ~600       ~1000       O(log n) grows slowly [good!]
 .01n² |   10    10000   100000000       O(n²) grows fast [bad!]