简体   繁体   中英

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. 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. 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)?

You iterate n times in the loop. 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). Therefore, you have 1+2+...+(n-1)+n=(n*(n+1))/2 time steps. This is equal to (n^2+n)/2, or n^2+n after eliminating constants. The order of this term is 2, therefore your running time is O(n^2) (always take the highest power).

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.

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)

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)

You may ask why did we skip the n=len(S) , and A=[0] ?

Because those variables take 0(1) time to complete the action.

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.

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 j in range(n):
    for q in range(S)
        A[j] = ....

Therefore, the complexity is O(n^2)

The For Loop (for j in range(n)) has n iterations:

Iteration(Operation)

1st iteration( 1 operation for summing first 1 element)

2nd iteration( 2 operations for summing first 2 elements)

3rd iteration( 3 operations for summing first 3 elements)

.

.

.

(n-1)th iteration( n-1 operations for summing first n-1 elements)

nth iteration( n operations for summing first n elements)

So, the total number of operation is the summation of (1 + 2 + 3 +......(n-1) + n)...

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

So the time complexity is O(n^2) as we have to (n (n+1))//2 operations. *

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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