6

I want to calculate the theta complexity of this nested for loop:

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < i; j++) {
            for (int k = 0; k < j; k++) {
                // statement

I'd say it's n^3, but I don't think this is correct, because each for loop does not go from 1 to n. I did some tests:

n = 5 -> 10

10 -> 120

30 -> 4060

50 -> 19600

So it must be between n^2 and n^3. I tried the summation formula and such, but my results are way too high. Though of n^2 log(n), but that's also wrong...

Aaron
  • 826
  • 10
  • 22

2 Answers2

4

Using Sigma Notation is an efficient step by step methodology:

enter image description here

3

It is O(N^3). The exact formula is (N*(N+1)*(N+2))/6

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • @Aaron I asked Wolfram Alpha (see the link in the answer), it's good at these sorts of calculations. – Sergey Kalinichenko Feb 24 '13 at 14:27
  • Yes, I saw that, but I want to understand why it is that formula. – Aaron Feb 24 '13 at 14:32
  • 1
    I think the real formula is `n * (n-1) * (n-2) / 6`. Please see [this running sample](http://ideone.com/547ovq). Anyway, it doesn't change the fact that it's `O(N^3)` – Cristian Lupascu Feb 24 '13 at 14:33
  • also, +1 for the Wolfram Alpha link; awesome website – Cristian Lupascu Feb 24 '13 at 14:36
  • @w0lf It's funny I just discovered the same thing using a similar sort of a program :) I'm trying to figure out what gives... – Sergey Kalinichenko Feb 24 '13 at 14:36
  • Thanks w0lf, completely the same as my tests! Want to explain how you got it? – Aaron Feb 24 '13 at 14:38
  • @Aaron I just took dasblinkenlight's formula and tried variations until I found the working one. I don't have an explanation for this result. – Cristian Lupascu Feb 24 '13 at 14:48
  • 1
    @w0lf I think the reason behind the discrepancy is the same as what's behind [this test based on your program](http://ideone.com/8rKHrK), where the sum of arithmetic sequence comes out as `(N*(N-1))/2` rather than the well-known `(N*(N+1))/2`. The problem is that the loops do not start counting until `i` gets to `2` (or to `1` in case of two nested loops), meaning that the "mathematical" `N` is `N-2` in your program (or `N-1` in my modification of your program). Once you "remap" `N->N-2`, `N+1` becomes `N-1`, and `N+2` becomes `N` for your final formula. – Sergey Kalinichenko Feb 24 '13 at 14:50
  • 1
    @Aaron The arithmetic series formula for `N*(N+1)/2` is easy to prove, but the formula for `SUM(x*(x+1)/2, x from 0 to N)` is not that easy. Once you know the answer, you can probably prove it by induction. I'm sure if you ask on the [math Q&A site](http://math.stackexchange.com/) you'd get a much better answer as to how to come up with this formula. – Sergey Kalinichenko Feb 24 '13 at 14:55
  • 2
    That's right; I noticed that changing the `<` conditions to `<=` in the inner loops yields the correct count, based on the well known formula. I've changed the program and made versions for [two](http://ideone.com/Eb4G0k) and [three](http://ideone.com/547ovq) loops. – Cristian Lupascu Feb 24 '13 at 14:57
  • @dasblinkenlight, w0lf It is sort of funny because you guys modified the code to suit the run-time... instead of finding the run-time of the code :). The reason why that code doesn't work is that the bounds on the iterators are wrong for the traditional summation. `n*(n+1)/2` is the sum `1 + 2 + ... + n`, not `0 + 1 + ... + n-1` (which is what the code in OP and in the comments do). If it required special casing to write a simple mathematical summation in a language, I'd be incredulous at the person who designed it haha (someone who hates math, but likes designing programming languages??) – rliu Sep 10 '13 at 07:00