Friday, 13 September 2013

Our prof says for a double loop, T(n) is a*(n^2) + b*n + c. I think it's just a*(n^2). What's the exact answer?

Our prof says for a double loop, T(n) is a*(n^2) + b*n + c. I think it's
just a*(n^2). What's the exact answer?

This is the slide by our prof.
Example 4: Consider this simple program: 1: s = 0 2: for i = 1 to n do 3:
for j = 1 to n do 4: s= s+i+j 5:end for 6:end for` T(n) =? It's hard to
get the exact expression of T(n) even for this very simple program. We can
see: the loop iterates n2 times, and loop body takes constant number of
instructions. So T(n) = a*(n^2) + bn + c for some constants a, b, c.
Now here's what I think. Let's assume that the body loop takes constant
time 'a'. Then that itself will be looped over for a*(n^2) times. So, I
don't understand from where b*n + c comes! What's the actual answer?

No comments:

Post a Comment