简体   繁体   English

Big-O表示法的计算复杂度

[英]Computational complexity in Big-O notation

How can I specify computational complexity of an algorithm in Big-O notation whose execution follows the following pattern, according to input sizes? 如何根据输入大小以大O表示法指定算法的计算复杂度,其执行遵循以下模式?

Input size: 4
Number of execution steps: 4 + 3 + 2 + 1 = 10

Input size: 5
Number of execution steps: 5 + 4 + 3 + 2 + 1 = 15

Input size: 6
Number of execution steps: 6 + 5 + 4 + 3 + 2 + 1 = 21

Technically, you have not given enough information to determine the complexity. 从技术上讲,您没有提供足够的信息来确定复杂性。 On the information so far, it could take 21 steps for all input sizes greater than 5. In that case, it would be O(1) regardless of the behavior for sizes 4 and 5. Complexity is about limiting behavior for large input sizes, not about how something behaves for three extremely small sizes. 根据到目前为止的信息,对于大于5的所有输入大小,可能要花21个步骤。在那种情况下,无论大小4和5的行为如何,它都将是O(1)。复杂性在于限制大输入大小的行为,与三种最小尺寸的物体的行为无关。

If the number of steps for size n is, in general, the sum of the numbers from 1 through n then the formula for the number of steps is indeed n(n+1)/2 and it is O(n^2). 如果大小为n的步数通常为1到n的总和,则步数公式的确为n(n + 1)/ 2,则为O(n ^ 2)。

For any series which follows the pattern 1 + 2 + 3 + 4 + ....+n , the sum of the series is given as n(n+1)/2, which is O(n^2).. 对于遵循模式1 + 2 + 3 + 4 + .... + n的任何序列,该序列的和为n(n + 1)/ 2,即O(n ^ 2)。

In your case , the number of steps will never be n^2, because at each level we are doing one step less(n + n-1 + n-2),however the big Oh notation is used to specify the upper bound on the growth rate of a function thus your big O will be O(n^2) because it is more than n number of steps but less than n^2 number of steps. 在您的情况下,步数将永远不会是n ^ 2,因为在每个级别上,我们都少做一步(n + n-1 + n-2),但是使用大Oh表示法指定了上界函数的增长率,因此您的大O将为O(n ^ 2),因为它的步数大于n,但步数小于n ^ 2。

I think it should be O(N) , because it's just like : Given an array with N size, and iterate it (if we don't care the plus computation time). 我认为应该是O(N) ,因为它就像:给定一个具有N个大小的数组,并对其进行迭代(如果我们不在乎加算时间的话)。

By the way, I think the (n + 1)*n/2 is just the result of sum, not the algorithm complexity. 顺便说一句,我认为(n + 1)*n/2只是求和的结果,而不是算法的复杂性。

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

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