0

As you can see I'm still pretty new with all these run time analyses and want to make sure each step I'm calculating is right.. Also I hate writing in pseudocode form so I did this in Python instead.. here goes

def mean(n):
    sum = 0               #cost = 1
    for i in n:           #cost = n
        sum += i          #cost = n
    return sum/len(n)     #cost = 1

Therefore overall running time for mean (correct me if im wrong) = O(1) + O(n) + O(n) + O(1) = O(n)

def variance(n):
    var = 0                    #cost = 1
    for i in n:                #cost = n
        var += (i-mean(n))**2  #cost = n*n or n+n ??
    return var / len(n)        #cost = 1

Question is what is the overall running time for variance? Can you show all workings please?

compski
  • 709
  • 3
  • 13
  • 28

1 Answers1

1

O(N^2), since you're performing an O(N) operation N times.

In general, loops are multiplicative when determining runtime; had your variance loop been "for i in lg(n)" then your runtime would be O(N * lg(N)) since you'd be performing an O(N) operation lg(N) times; likewise had your inner operation been O(2^N) with an outer loop of "for i in n" then your runtime would be O(N * 2^N)

Another common loop format is

for(int i = 0; i < N; i++) {
    for(int j = i; j < N; j++) {
        // O(1) operation
    }
}

This is still O(N^2), but the analysis is a bit trickier: you need to take the sum of the arithmetic series "1 + 2 + 3 + ... + n-1 + n" which is n * (n - 1) / 2, or O(N^2)

Zim-Zam O'Pootertoot
  • 17,888
  • 4
  • 41
  • 69
  • hmm even if the mean value is always the same it would do that? O(n) times n times – compski Jun 29 '13 at 14:28
  • @user1186038 If you're recomputing the mean each time then it would be O(N^2), because the loop's inner operation would be O(N); if you're only computing the mean once, storing it in a local variable, and retrieving the local variable inside the loop, then the loop's inner operation would be O(1) and therefore the entire loop would be O(N) – Zim-Zam O'Pootertoot Jun 29 '13 at 14:30
  • I get it =) so regardless what the code is it will still be O(n^2) ? i.e. it can't get anymore efficient than O(n^2)? – compski Jun 29 '13 at 14:35
  • @user1186038 No, you can improve your code's runtime to O(N) by caching the `(i-mean(n))**2` statement. In fact, you can improve your code's runtime to O(1) by replacing the entire loop with `var = n * (i-mean(n))**2`. However, I don't know enough statistics to know if your variance implementation is correct; but, given the implementation in your question, you can improve it to O(1). – Zim-Zam O'Pootertoot Jun 29 '13 at 14:40