3

I am not sure at all if I understand calculations of the time complexity. I am given these loops, and these are my calculations. However, I am not sure at all what to say about the big O's here.

Loop 1:

lst = []                   
i=1                        
while i<n:
  lst = list(range(i))       
  i *= 2 

I assume that each operation takes O(1) time. In this loop, the 1st line and the second line are executed 1 time each. The first line of the while loop has 3 operations - range, list, and assignment of that value to lst. Since we are dealing with range, I assume that it runs n+1 times.

The last line of the loop has 2 operations: multiplication by 2 and assignment of that value to i, and it runs n times.

From this, I think that the total would be: 1+1+3(n+1)+2n = 5n+5.

Since this is a linear function, then the big O will be O(n)?

===============

Loop 2:

lst = []            
i=1                 
while i<n:
  lst = lst + [i]      
  i *= 2    

Here we have a similar case, however the first line of the while loop has 2 operations. Then,
1+1+2n+2n = 4n+2.

Since it's a linear function, it's O(n) as well?

========================== Loop 3:

lst = []          
i=1                
while i<n:
  lst += [i]         
  i *= 2  

I think that lst +=[i] would have 2 operations executed 2n times, since this is an in-place calculation? I am not sure about this. If this is right, then the total would be 6n+2

The questions are: am I calculating these right or am I completely wrong? How do I write the big O's for each of these loops?

nanachan
  • 1,051
  • 1
  • 15
  • 26
  • 1
    The second one was posted earlier today [here](http://stackoverflow.com/q/20164008/645270). It'll probably help you understand all of them. – keyser Nov 23 '13 at 18:25
  • 1
    Perhaps this would be more appropriate on [Computer Science Stack Exchange](http://cs.stackexchange.com/). – KobeJohn Nov 23 '13 at 18:26
  • You need to think about how many times the `while` loop is executed. – rlms Nov 23 '13 at 18:27
  • First think about how many times it loops like sweeneyrod said, then think about what's done at each iteration. When thinking about the number of iterations, don't use actual numbers, just think about how it relates to `n`. Is `i` doubled each time? Then it will reach `n` in `log2(n)` increments. – keyser Nov 23 '13 at 18:32
  • @KEYSER I understand that each loop will be iterating for log2n times, since in each case i is doubled. What confuses me are the other operations besides doubling of i, especially the difference between += and a regular addition. – nanachan Nov 23 '13 at 18:38

1 Answers1

0

LOOP 1: O(n log n)

Loop runs log2(n) times, its mean O(log n). each iteration do (in worst case) n actions. so the complexity is O(n log n).

LOOP 2: O(log n)

Loop runs log2(n) times, its mean O(log n). I suppose the assignment lst = lst + [i] is just adding node (and not makes new list). its mean O(1), so the complexity is O(log n). If I'm wrong, the assignment creates new list, so each iteration do (in worst case) n actions. so the complexity is O(n log n)

LOOP 3: O(log n)

As in loop 2. Here, the assignment is O(1) for sure, not assumption...

MeNa
  • 1,467
  • 9
  • 23