简体   繁体   English

如何计算该算法的时间复杂度

[英]How to calculate Time Complexity of this algorithm

I am new to the concept of asymptotic analysis.我是渐近分析概念的新手。 I am reading "Data Structures and Algorithms in Python" by Goodrich.我正在阅读 Goodrich 的“Python 中的数据结构和算法”。 In that book it has an implementation as follows:在那本书中,它的实现如下:

 def prefix average2(S):
 ”””Return list such that, for all j, A[j] equals average of S[0], ..., S[j].”””
 n = len(S)
 A = [0] n # create new list of n zeros
 for j in range(n):
      A[j] = sum(S[0:j+1]) / (j+1) # record the average
 return A

The book says that this code runs in O(n^2) but I don't see how.这本书说这段代码在 O(n^2) 中运行,但我不明白如何。 S[0:j+1] runs in O(j+1) time but how do we know what time the 'sum()' runs in and how do we get the running time to be O(n^2)? S[0:j+1] 在 O(j+1) 时间内运行,但我们如何知道“sum()”在什么时间运行以及如何让运行时间为 O(n^2)?

You iterate n times in the loop.您在循环中迭代 n 次。 In the first iteration, you sum 1 number (1 time step), then 2 (2 time steps), and so on, until you reach n (n time steps in this iteration, you have to visit each element once).在第一次迭代中,对 1 个数字(1 个时间步)求和,然后对 2 个(2 个时间步)求和,以此类推,直到达到 n(本次迭代的 n 个时间步,您必须访问每个元素一次)。 Therefore, you have 1+2+...+(n-1)+n=(n*(n+1))/2 time steps.因此,您有 1+2+...+(n-1)+n=(n*(n+1))/2 个时间步。 This is equal to (n^2+n)/2, or n^2+n after eliminating constants.这等于 (n^2+n)/2,或消除常数后的 n^2+n。 The order of this term is 2, therefore your running time is O(n^2) (always take the highest power).该术语的阶数为 2,因此您的运行时间为 O(n^2)(始终取最高功率)。

for j in range(n): # This loop runs n times.
      A[j] = sum(S[0:j+1]) # now lets extend this sum function's implementation.

I'm not sure about the implementation of sum(iterable) function but it must be something like this.我不确定sum(iterable) function 的实现,但它必须是这样的。

def sum(iterable):
    result=0

    for item in iterable: # worse time complexity: n
          result+=item
    return result

so, finally, your prefix_average2 function will run n*n=n^2 time in worse case (When j+1=n)所以,最后,您的prefix_average2 function 将在最坏的情况下运行 n*n=n^2 次(当 j+1=n 时)

First of all, I am not an expert on this topic, but I would like to share my opinion with you.首先,我不是这个话题的专家,但我想和大家分享一下我的看法。

If the code is similar to the below:如果代码类似于以下:

for j in range(n):
    A[j] += 5 

Then we can say the complexity is O(n)那么我们可以说复杂度是O(n)

You may ask why did we skip the n=len(S) , and A=[0] ?你可能会问为什么我们跳过了n=len(S)A=[0]

Because those variables take 0(1) time to complete the action.因为这些变量需要 0(1) 时间来完成操作。

If we return our case:如果我们返回我们的案例:

for j in range(n):
    A[j] = sum(S[0:j+1]) ....

Here, sum(S[0:j+1]) there is also a loop of summation is calculated.这里, sum(S[0:j+1])还有一个循环求和计算。

You can think this as:你可以这样想:

for q in S:
    S[q] += q  # This is partially right

The important thing is two-for loop calculation is handling in that code.重要的是在该代码中处理两个for循环计算。

for j in range(n):
    for q in range(S)
        A[j] = ....

Therefore, the complexity is O(n^2)因此,复杂度为 O(n^2)

The For Loop (for j in range(n)) has n iterations: For 循环(for j in range(n))有 n 次迭代:

Iteration(Operation)迭代(操作)

1st iteration( 1 operation for summing first 1 element)第一次迭代(对前 1 个元素求和的 1 次操作)

2nd iteration( 2 operations for summing first 2 elements)第二次迭代(对前 2 个元素求和的 2 次操作)

3rd iteration( 3 operations for summing first 3 elements)第三次迭代(对前 3 个元素求和的 3 次操作)

. .

. .

. .

(n-1)th iteration( n-1 operations for summing first n-1 elements) (n-1) 次迭代(n-1 次操作,用于对前 n-1 个元素求和)

nth iteration( n operations for summing first n elements)第 n 次迭代(对前 n 个元素求和的 n 次操作)

So, the total number of operation is the summation of (1 + 2 + 3 +......(n-1) + n)...所以,操作的总数是(1 + 2 + 3 +......(n-1) + n)......的总和

which is (n*(n+1))//2.即 (n*(n+1))//2。

So the time complexity is O(n^2) as we have to (n (n+1))//2 operations.所以时间复杂度是 O(n^2),因为我们必须 (n (n+1))//2 次操作。 * *

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

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