-1
$\begingroup$

I was asked to solve this code on piece of paper while having a debate in the class. We are all new to DataStrct/Algo. I came up with the answer $O(n^{48})$ which was proved wrong. I am not convinced with the answer they gave me which was $O(n^6)$.

void main()
{
    int i;

    for(i=1, i<= n^2; i++)
    for(i=1, i<= n^4; i++)
    for(i=1, i<= n^6; i++)
    printf("0");
}

What is the no. of times '0' is printed?

Following is my computation for calculating complexity

  1. for loop: $O(n^2)$ then
  2. for loop: $O(n^4)$ then
  3. for loop: $O(n^6)$

making total of $$O(n^2) \cdot{} O(n^4) \cdot{} O(n^6) = O(n^{48})$$

$\endgroup$
  • 5
    $\begingroup$ This is literally the same "trick" as in your last question. I don't know who gives you those pieces of code for analysis, but they seem to be enjoy abusing language semantics more than they care about algorithm analysis. $\endgroup$ – Raphael Jan 4 '18 at 6:41
  • $\begingroup$ Also, you did not "compute" anything (nor give an argument); you applied pattern matching that is fooled here (which may be the point of the problem). Analyse properly (e.g. as explained in the referenced question linked to you before) and you'll get the right answer. $\endgroup$ – Raphael Jan 4 '18 at 6:42
  • 2
    $\begingroup$ Technically, O(N^48) isn't wrong --something bound by O(N^6) is also bound by O(N^48)-- it's just not the optimal solution. $\endgroup$ – ikegami Jan 4 '18 at 7:33
  • $\begingroup$ Comments are not for extended discussion; this conversation has been moved to chat. $\endgroup$ – Raphael Jan 4 '18 at 8:51
  • $\begingroup$ note the same index variable i. Once the inner-most for-loop ends, they all end. $\endgroup$ – JimN Jan 4 '18 at 18:03
-1
$\begingroup$

Sorry to say you that you are wrong!

I am no expert but I have tried to break it down for you.

This question not only test your algorithm analysis skills but also test your language skills.

You can learn in detail about algorithmic complexity and how to translate your program to mathematics here Is there a system behind the magic of algorithm analysis?

Save this link or Bookmark it, it will be helpful to you later.

Here is your program

IMP: You need to pay attention to variable 'i' and how it plays its role in all three loops.

This problem checks your syntax knowledge, scope of variable, and complexity knowledge all three in one go.

void main()
{
    int i;..............................statement 1 [executed once]

    for(i=1, i<= n^2 ; i++)..............statement 2 [executed twice]
    for(i=1, i<= n^4; i++)..............statement 3 [executed twice]
    for(i=1, i<= n^6; i++)..............statement 4 [executed N^6]
    printf("0");
}

statement 1: Scope of 'i' is permeating throughout all three loops that is, once 'i' is incremented, its value is stored and fetched from same memory location by all three loops.

statement 2: 'i' is initialized to 1, checked for the condition (i<= $n^2$) and control moves to 3rd statement.

statement 3: again 'i' is assigned value= 1, checked for condition (i<= $n^4$) and control moves to 4th statement.

statement 4: again 'i' is assigned value= 1, checked for condition (i<= $n^6$) and looped till (i<= $n^6$) fails to match [i.e. i> $n^6$ and condition fails.]

Execution control is transferred to statement 3 where 'i' is incremented (i++) and checked for the condition again. As soon as it checks the condition, condition fails to meet because now value of 'i' = last value of 'i' + 1. So, it exits the middle loop.

So, execution control moves to statement 2 where again 'i' is incremented (i++) and checked for the condition again. But here again value of i is greater than $n^2$. Control exits the loop.

You can deduce that Big-Oh for your piece of code is $O\left(n^{6}\right)$ and not $O\left(n^{48}\right)$ because your loop actually iterated $n^6$ + 2 + 2 + 1 = $n^6$ + 5 = $O\left(n^{6}\right)$.

So, your printf statement will run $O\left(n^{6}\right)$ times.

$\endgroup$
  • $\begingroup$ Please stop raising straw men. I never said It wasn't C. I said if it's C and not pseudocode as you claim, then you are wrong about what the code does. ^ is not exponentiation in C; ^ is the bitwise operator in C. C doesn't have an exponentiation operator; one would use the pow function. $\endgroup$ – ikegami Jan 4 '18 at 7:39
  • $\begingroup$ Comments are not for extended discussion; this conversation has been moved to chat. $\endgroup$ – D.W. Jan 4 '18 at 8:06
2
$\begingroup$

tl;dr- It's $O\left(n^{6}\right)$ because it's basically just the inner loop. However, if it weren't for the confusing code construction, it'd have been $O\left(n^{12}\right)$ instead of $O\left(n^{48}\right)$ since nested loops multiply the complexity of their body, leading to $n^2{\times}n^4{\times}n^6=n^{\left(2+4+6\right)}=n^{12}$.


The inner-most loop, for(i=1; i<= n^6; i++), should terminate when i==n^6+1. After that, the other two loops will check for termination against i==n^6+1, ending both immediately. So, the first two for loops can basically be ignored.

However, let's say that it wasn't a trick question and the code were

                                                                 //  Called:
void foo(int n)                                                  //  1 time
{
  for(int i = 0; i < n^2; i++)                                   //  1 time
    for(int j = 0; j < n^4; j++)                                 //  n^2 times
      for(int k = 0; k < n^6; k++)                               //  n^6 times
        printf("0");                                             //  n^12 times
}

. In this case, it'd have been $O\left(n^{12}\right)$, not $O\left(n^{48}\right)$.

This can be seen by rewriting it with single-n loops:

                                                                 //  Called:
void foo(int n)                                                  //  1 time
{
  //  Two effective n loops for "for(int i = 0; i < n^2; i++)":
  for(int i_0 = 0; i_0 < n; i_0++)                               //  1 time
    for(int i_1 = 0; i_1 < n; i_1++)                             //  n times

      //  Four effective n loops for "for(int j = 0; j < n^4; j++)":
      for(int i_2 = 0; i_2 < n; i_2++)                           //  n^2 times
        for(int i_3 = 0; i_3 < n; i_3++)                         //  n^3 times
          for(int i_4 = 0; i_4 < n; i_4++)                       //  n^4 times
            for(int i_5 = 0; i_5 < n; i_5++)                     //  n^5 times

              //  Six effective n loops for "for(int k = 0; k < n^6; k++)":
              for(int i_6 = 0; i_6 < n; i_6++)                   //  n^6 times
                for(int i_7 = 0; i_7 < n; i_7++)                 //  n^7 times
                  for(int i_8 = 0; i_8 < n; i_8++)               //  n^8 times
                    for(int i_9 = 0; i_9 < n; i_9++)             //  n^9 times
                      for(int i_10 = 0; i_10 < n; i_10++)        //  n^10 times
                        for(int i_11 = 0; i_11 < n; i_11++)      //  n^11 times

                          //  The inner-most loop body:
                          printf("0");                           //  n^12 times
}
$\endgroup$
0
$\begingroup$

The most effective loop in the given problem $O(n^6)$. Variable '$i$' change the way all the three loops work in tandem. The first and second (middle) loops will not execute the way they normally they do and that is because of variable '$i$'. Also, the code is neither pseudo code or working program.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy