1

What's the big-O time complexity of the two methods listed below?

Is method 1 O(log n²) or O(log n)?

Is method 2 O(n²) or something else?

public static void method1(int n) {
    int k = 0;      
    for (int i = 1; i <= n; i = i*2)
       for (int j = n; j > 1; j = j/2)
           k++;     
}               

public static void method2(int n) {             
    for (int i = 1; i <= n; i = i + 1)
       for (int j = i; j <= n; j++)
          method1(n);
}
AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
Seth Keno
  • 679
  • 8
  • 16
  • 2
    It seems that you want us to do your homework for you. – Raedwald Mar 26 '14 at 12:57
  • See http://stackoverflow.com/questions/3255/big-o-how-do-you-calculate-approximate-it – Raedwald Mar 26 '14 at 12:59
  • @Raedwald it looks the OP wants his answers verified and explanations on why he might be wrong. It does look like homework and OP could have included reasoning for his guesses, but he tried to solve it at least. – Colin D Mar 26 '14 at 12:59
  • I'm practicing for a test...my answer was O(logn^2),O(n^2) ... so I got the second one wrong (I thought I should ignore the logn^2 in the second method because n^2 is big enough for me to ignore it) – Seth Keno Mar 26 '14 at 13:03
  • @Seth Keno: Note that you cannot ignore logn^2 here because it is a *multiplicative* term. If it was an additive term, your resoning about ignoring it would be right. – gexicide Mar 26 '14 at 15:37

4 Answers4

2

In the first example, the outer loop executes logn times, and the second loop also executes log n times. The complexity is O(log² n).

In the second example, the first loop executes n times and the second loop executes i times where i is the current index of the first loop. So, it executes 1 + 2 + 3 + ... + n times, summing up to n ⋅ (n - 1) / 2. The resulting complexity is O( (n² - n)/2 ) = O(n²). Read more here 1 + 2 + 3 ... series

So, to wrap it up, method2 executes method1 n² times, giving the total complexity of O(n² ⋅ log² n)

Neil
  • 7,227
  • 5
  • 42
  • 43
darijan
  • 9,725
  • 25
  • 38
  • the inner loop of method 1 is also logn... in method 2 after the inner loop we call the method 1 which has 2 more loops, could it possibly be O(n^2+(logn^2))? – Seth Keno Mar 26 '14 at 12:46
  • No, the inner loop in Method2 does not execute n times. It is rather a triangular number, but the resulting asymptotic complexity is the same, so O(n^2) is correct. – gexicide Mar 26 '14 at 12:46
  • I still dont know if the first method is O(logn) or O(logn^2) and for the second method, considering it calls method 1 in the inner loop, is it O(n^2) or O(n^2+(logn^2)) (or something like that). – Seth Keno Mar 26 '14 at 12:50
  • @Seth Keno: See my answer. The author of this answer may have overlooked that method1 gets called from method2. – gexicide Mar 26 '14 at 12:52
2

Well in the first method you have 2 nested loops where each one is exactly log(n) so yes that would turns them into a O((logn)^2)

For the second method, if we focus on the second loop we can see that for

  • i = 1 => runs n times
  • i = 2 => runs n-1 times
  • ...
  • i = n => runs 1 times

This is an arithmetic progression and its sum is equal to n * (n+1) / 2 which turns it into an O((nlogn)^2) since we are calling method1 about n^2 times.

Samy Arous
  • 6,794
  • 13
  • 20
1

The complexity of method1 is O((log n)²) since we have a nested loop where each loop runs O(log n) times.

In Method2 we execute Method1 a triangluar number of times, i.e., we execute it O(n²) times. Since we execute an O((log n)²) function O(n²) times, the resulting complexity for Method2 is O(n² ⋅ (log n)²).

AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
gexicide
  • 38,535
  • 21
  • 92
  • 152
0

Methodically and formally, you can use Sigma notation like this:

enter image description here