0

So I am learning how to calculate time complexities of algorithms from Introduction to Algorithms by Cormen. The example given in the book is insertion sort:

1. for i from 2 to length[A]
2.    value := A[i] 
3.    j := i-1
4.    while j > 0 and A[j] > value
5.       A[j+1] := A[j]
6.       j := j-1
7.    A[j+1] = value  

Line 1. executes n times.
Line 4., according to the book, executes enter image description here times.

So, as a general rule, is all inner loops' execution time represented by summation?

An SO User
  • 24,612
  • 35
  • 133
  • 221
  • What would you expect, a product? – n. m. could be an AI Dec 14 '14 at 18:43
  • @n.m. Sorry for sounding dumb, but yes. That is what my instructor told me. I believe he is wrong. :) – An SO User Dec 14 '14 at 18:58
  • Well a product's one particular case of a sum, where all summands are equal. But really. Total time spent in the inner loop = (time spent in the inner loop while doing the first iteration of the outer loop) + (time spent in the inner loop while doing the second iteration of the outer loop) + .... + (time spent in the inner loop while doing the last iteration of the outer loop). That's what the formula says. Hard to argue with that, isn't it? – n. m. could be an AI Dec 14 '14 at 19:43
  • @n.m. That's about it. Summation it is. End of story. :) – An SO User Dec 14 '14 at 20:15

2 Answers2

1

Anecdotally, most loops can be represented by summation. In general this isn't true, for example I could create the loop

for(int i = n; i > 1; i = ((i mod 2 == 0) ? i / 2 : 3 * i + 1)))

i.e. initialize i to n, on each iteration if i is even then divide it by two, else multiply i by three and add one, halt when i <= 1. What's the big-oh for this? Nobody knows.

Obviously I've never seen a real-life program using such a weird loop, but I have seen while loops that either increase and decrease the counter depending on some arbitrary condition that might change from iteration to iteration.

Zim-Zam O'Pootertoot
  • 17,888
  • 4
  • 41
  • 69
  • I really wasn't talking about corner cases :p So in general, it will be summation. – An SO User Dec 14 '14 at 19:04
  • @LittleChild The answer is also dependent on the domain. In an intro to algorithms book, most inner loops are going to run in linear or log time, because a lot of introductory algorithms run in `log`/`n`/`log*n`/`n*n` time; however in a domain like reactive systems it's common to see loops that don't terminate. – Zim-Zam O'Pootertoot Dec 14 '14 at 19:15
  • Agreed. I am still in the "academic" domain, if I may say :-D – An SO User Dec 14 '14 at 19:18
  • The question asks whether inner loop execution time can be represented as a particular kind of sum. You don't have an inner loop => this is not an answer. – n. m. could be an AI Dec 14 '14 at 19:40
  • @n.m. I could add a trivial loop body and a trivial outer loop to turn the loop header I provided into a well formatted inner loop, but it appears by the fact that the answer was accepted that Little Child was able to do this on his own. – Zim-Zam O'Pootertoot Dec 14 '14 at 19:53
  • You seem to totally misunderstand the question. It is immaterial whether the answer was accepted or not, it is still not an answer. Of course total time spent in the inner loop is trivially the sum of times spent there while doing each separate iteration of the outer loop. This is true whether your loops are trivial or express unsolved millennium prize problems. – n. m. could be an AI Dec 14 '14 at 20:04
  • @n.m. So you're upset that I answered the intended question rather than a pedantic interpretation of the stated question? Yes, all loops can be represented as summations. The intended question was whether all loops can be represented as trivial summations from `i = C to n`, and the answer to that question is "no." If this hadn't been the intended question then I gave the wrong answer, but it appears that my interpretation of what was being asked was the correct one. – Zim-Zam O'Pootertoot Dec 14 '14 at 20:11
  • I respectfully disagree. I intend to spend no more time on this, sorry. – n. m. could be an AI Dec 14 '14 at 20:18
1

When the inner loop has a condition, the times the program will iterate through it(ti) will differ in each iteration of the outer loop. That's why it's equal to the summation of all the (ti)s.

However, if the inner loop was a for loop, the times the program will iterate through it is constant. So you just multiply the times of the outer loop by the times of the inner loop. like this:

 for i=1 to n
       for j=1 to m
         s++ 

the complexity here is n*m

An SO User
  • 24,612
  • 35
  • 133
  • 221
Huda
  • 21
  • 2
  • In your context, yes it is intuitive. However, I was talking about how to represent the times of the inner loop when calculating the complexity of, say, bubble sort. :) – An SO User Dec 14 '14 at 19:06