简体   繁体   English

O(1)+ O(2)+ ... + O(n)的阶数之和

[英]Sum of order of O(1)+O(2)+ … +O(n)

What does the sum O(1)+O(2)+ .... +O(n) evaluate to? 总和O(1)+O(2)+ .... +O(n)评估为什么?

I have seen its solution somewhere it was written: 我在其编写的某个地方看到了它的解决方案:

O(n(n+1) / 2) = O(n^2)

but I am not satisfied with it because O(1) = O(2) = constant , so according to me it must evaluate to O(n) only. 但是我对它不满意因为O(1) = O(2) = constant ,所以据我说它必须只评估为O(n) I have also seen this in Cormen: 我在Cormen也见过这个:

Σ(i=1 to n) O(i)

In above expression there is only a single anonymous function. 在上面的表达式中,只有一个匿名函数。 This function is not the same as O(1) + O(2) + ... + O(n) which doesn't really have a clean interpretation. 此功能与O(1) + O(2) + ... + O(n) ,它实际上没有清晰的解释。

The question seems to be perfectly on-topic since there is a tag asymptotic_complexity ... 这个问题似乎完全是主题,因为有一个标签asymptotic_complexity ...

According to CLRS , p. 根据CLRS ,p。 49, 49,

"The number of anonymous functions in an expression is understood to be equal to the number of times the asymptotic notation appears. For example, in the expression “表达式中匿名函数的数量被理解为等于渐近符号出现的次数。例如,在表达式中

sum(O(i), i=1..n) sum(O(i),i = 1..n)

there is only a single anonymous function (a function of i). 只有一个匿名函数(i的函数)。 This expression is thus not the same as O(1) + O(2) + ... + O(n), which doesn't really have a clean interpretation" 因此,这个表达式与O(1)+ O(2)+ ... + O(n)不同,它实际上没有一个干净的解释“

Actually, in your formula, the constants behind the "O" notation may be all different, and their growth may change the asymptotic behaviour of the whole sum. 实际上,在你的公式中,“O”符号背后的常数可能都是不同的,它们的增长可能会改变整个总和的渐近行为。 Don't write this! 别写这个!


To answer your question more completely, in sum(O(i), i=1..n), you can use the fact that (see GKP p. 450 for the following) 为了更完整地回答你的问题,总和(O(i),i = 1..n),你可以使用这一事实(参见GKP第450页,以下内容)

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

Thus, O(i) = i O(1), this time with the same O(1) in your formula. 因此,O(i)= i O(1),这次在公式中使用相同的O(1)。 Therefore, 因此,

sum(O(i), i=1..n) = sum(i, i=1, n) O(1) sum(O(i),i = 1..n)= sum(i,i = 1,n)O(1)

=n(n+1)/2 O(1) = O(n^2) = n(n + 1)/ 2 O(1)= O(n ^ 2)

This way you can eliminate your sum without trouble. 这样你就可以毫无困难地消除你的金额。

Go by the definition of the Big-Oh notation. 按照Big-Oh表示法的定义。

You have n functions f_i , each of which satisfies 你有n函数f_i ,每个函数都满足

a_i * i <= f_i(x) <= b_i * i; x > X_i

for some positive constants a_i, b_i and sufficiently large X_i . 对于一些正常数a_i, b_i和足够大的X_i let X = max_i ( X_i ) and sum over the n inequalities to get X = max_i ( X_i )并求n不等式得到

sum_i=1..n ( a_i * i ) <= sum_i=1..n ( f_i(x) ) <= sum_i=1..n ( b_i * i )

noting that 注意到这一点

sum_i=1..n ( min(a_i) * i ) <= sum_i=1..n ( a_i * i )
sum_i=1..n ( b_i * i )      <= sum_i=1..n ( max(b_i) * i )

arriving at 到达

    min(a_i) * 0.5*(n(n-1))  <= sum_i=1..n ( f_i(x) ) <= max(b_i) * 0.5*(n(n-1))
<=>       A       * (n(n-1)) <= sum_i=1..n ( f_i(x) ) <=        B      *(n(n-1))

thus the sum of the functions you started with is indeed O(n^2) . 因此,您开始使用的函数之和实际上是O(n^2)

sure, O(1) and O(2) is a constant, so we can forget about them. 确定,O(1)和O(2)是一个常数,所以我们可以忘记它们。 But is O(n/2) constant, I guess no. 但是O(n / 2)是常数,我猜不是。 So try (for your homework) count just second half of the elements: 所以尝试(为你的功课)只计算后半部分:

O(n/2) + O(n/2+1) + ... O(n) = ??

you will come up with O(n^2) :) 你会想出O(n^2) :)

There is a trick to find the formula, really not that hard. 有一个技巧找到公式,真的不那么难。

You have: 1 + 2 + 3 + 4 + .... + n 你有:1 + 2 + 3 + 4 + .... + n

If you count the list twice (one time ordered from low to high one time from high to low you get twice the result) 如果您对列表进行两次计数(一次从低到高从高到低排序一次,则得到两倍的结果)

((1 + 2 + 3 + 4 + ... + n) + (n + (n - 1) + (n - 2) ... + 2 + 1)) / 2 ((1 + 2 + 3 + 4 + ... + n)+(n +(n - 1)+(n - 2)... + + 2 + 1))/ 2

This is the same as 这是一样的

((1 + n) + (2 + n - 1) + (3 + n - 2) + ... + (n + 1)) / 2 ((1 + n)+(2 + n - 1)+(3 + n - 2)+ ... +(n + 1))/ 2

And this is: 这是:

((n + 1) * n) / 2 ((n + 1)* n)/ 2

or as we right in o notion O(n²). 或者我们正确的观念O(n²)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM