This problem is simply a variation on the form
for( int i = 1; i <= N; i *= 2 ) for( int j = 0; j < i; j++ ) { some computation using O(1) time }
from the worksheet, which we have discussed in class and in the final review session. It is also very similar to the fast_power using multiplication which takes time proportional to the products of the lengths of the arguments. This form has a time bound of O(N). Adding the third loop is equivalent to making the inner loop of the above program be O(i2) instead of O(i). By the same rationale as before, the overall computation is O(N2), since the final iteration of the outer loop takes longer than all the rest put together.
In more detail for the problem:
for( int i = 1; i <= N; i *= 2 ) for( int j = 0; j < i; j++ ) for( int k = 0; k < j; k += 2 ) { some computation using O(1) time }
It is easy to see that a tight upper bound on the combination of the inner two loops, as a function of i, is ci2 where c is an appropriate constant. The outer loop runs I through values 1, 2, 4, 8, ., N/2, N. So an overall bound is
c (12 + 22 + 42 + 82 + . + (N/2) 2 + N2 )
The last term dominates the sum of the other terms, which are all smaller powers of 2. Therefore, the overall time bound is
O(N2 ).
To see further why the inner two loops are bounded by ci2, note that the number of steps of the inner loop as a function of j is j/2 + 1 if j is odd or j/2 if j is even. So as a function of i, we have as the number of steps for the inner two loops:
1 + 2 + 3 + . i/2 + 1 if i is odd
1 + 2 + 3 + . i/2 if i is even
In the present context, i is always even, except for the case i = 1. The sum for i = 1 is 1 and for i > 1 is the usual sum of an arithmetic series:
((1+i/2)2 - (i/2))/2
which is at most i2/8. We can just as well use c = to get our bound of O(N2 ).
For final confirmation (no, I did not expect you to do this in the exam), here are results from an actual run:N T(N) T(N)/(N^2) T(N)/(N^3) 1 0 0.000000e+00 0.000000e+00 2 1 2.500000e-01 1.250000e-01 4 5 3.125000e-01 7.812500e-02 8 21 3.281250e-01 4.101562e-02 16 85 3.320312e-01 2.075195e-02 32 341 3.330078e-01 1.040649e-02 64 1365 3.332520e-01 5.207062e-03 128 5461 3.333130e-01 2.604008e-03 256 21845 3.333282e-01 1.302063e-03 512 87381 3.333321e-01 6.510392e-04 1024 349525 3.333330e-01 3.255205e-04 2048 1398101 3.333333e-01 1.627604e-04 4096 5592405 3.333333e-01 8.138020e-05 8192 22369621 3.333333e-01 4.069010e-05 16384 89478485 3.333333e-01 2.034505e-05 32768 357913941 3.333333e-01 1.017253e-05 65536 1431655765 3.333333e-01 5.086263e-06As one can see, the n2 bound is well supported.