0

Hello I have an algorthm in C++ and I want to find the instructions executed. The code is below

cin >> n;   
for(i=1;i<=n;i++)
     for (j = 1; j <= n; j ++) 
     A[i][j] = 0;

for(i=1;i<=n;i++) 
A[i][i] = 1;

now, after my calculation, I got this T(n) = n^2+8n-5. I just need someone else to verify if I am correct. Thanks

helpdesk
  • 1,984
  • 6
  • 32
  • 50

1 Answers1

0

Ok, let's do an analysis step by step.

The first instruction

cin >> n

counts as one operation: 1.

Then the loop

for(i=1;i<=n;i++)
     for (j = 1; j <= n; j ++) 
     A[i][j] = 0;

Let's go from inside out. The j loop performs n array assignments (A[i][j] = 0), (n + 1) j <= n comparisons and n j ++ assignments. It also performs once the assignment j = 1. So in total this gives: n + (n +1) + n + 1 = 3n + 2.

Then the outer i loop performs (n + 1) i <= n comparisons, n i ++ assignments and executes n times the j loop. It also performs one i = 1 assignment. This results in: n + (n + 1) + n * (3n + 2) + 1 = 3n^2 + 4n + 2.

Finally the last for loop performs n array assignments, (n + 1) i <= n comparisons and n i ++ assignments. It also performs one assignment i = 1. This results in: n + (n + 1) + n + 1 = 3n + 2.

Now, adding up three operations we get:

(1) + (3n^2 + 4n + 2) + (3n + 2) = 3n^2 + 7n + 5 = T(n) total operations.

The time function is equivalent, assuming that assignments, comparisons, additions and cin are all done in constant time. That would yield an algorithm of complexity O(n^2).

This is of curse assuming that n >= 1.

cgledezma
  • 612
  • 6
  • 11
  • Hi, What if I say 1 for(j=1) n+1 for(j<=n) and n for(j++) giving 2n+1 is that wrong? you got 3n+1. could you explain the different from what I just did? – helpdesk Jun 27 '13 at 09:28
  • @henryjoseph you're right, I miscalculated the number of comparisons. I edited and recalculated. The only error in your reasoning is that you also need to put into the count of operations the actual action done inside the loop, not only the checks on the bounds – cgledezma Jun 27 '13 at 09:41
  • so you mean I should have added an extra n+1 to the 2n+1? what would the extra n+1 be for? Please, could you re-edit your answer if you miscalculated something? – helpdesk Jun 27 '13 at 09:45
  • @henryjoseph I'm not sure what you mean by an extra (n+1). I did edit to correct my miscalculations, on the answer I explain every aspect I considered to get to the result, which part in particular is not clear? To the 2n + 1 you actually need to add the (1) of the `j=1` and the n array assignments :) – cgledezma Jun 27 '13 at 10:07
  • take alook at this link which I am following to do mine : http://stackoverflow.com/questions/12851857/what-is-the-time-complexity-of-the-following-algorithm?rq=1 what could have been different because the fellow also used 2n+1 instead of 3n+2. – helpdesk Jun 27 '13 at 10:09
  • @henryjoseph I believe the guy that posted that question has some errors in his formulas, some wrong parenthization and reasoning that render his proof incorrect. In particular he's counting the `Simple statement` as if it were outside the inner loop, ergo he gets (2n + 2) instead of (3n + 2) when he does the product. And the product is also incorrectly formulated. – cgledezma Jun 27 '13 at 11:37
  • how about the statements : A[i][j] = 0;? I don't see them added in your calculation? I will try your analysis on another solution and see if it matched up. – helpdesk Jun 27 '13 at 12:02
  • @henryjoseph "The `j` loop performs n array assignments". That's the `A[i][j] = 0` statements. Edited the answer so it is clearer. – cgledezma Jun 27 '13 at 12:13
  • Thanks alot for your time. one final question.you said this: "and executes n times the j" and that means n*(3n+2) does that means that the j function(3n+2) is multiplied by the number of times the j runs which is n? – helpdesk Jun 27 '13 at 12:18
  • @henryjoseph Yes, it is just as if you were counting them one by one. Executing the full `j` loop costs (3n + 2) operations. And you are executing n full `j` loops, so the total number of operations is n * (3n + 2) – cgledezma Jun 27 '13 at 12:22