简体   繁体   中英

How do I find worst-case complexity of the following code?

Need to know the worst-case complexity of the following code, would be grateful if you could provide the steps of how to solve it. I was thinking of the answer n^3logn, but not sure.

int complexity(int n)
  int i,j,c
  for(i=1; i < n; i=i*3)
    for(j=n; j < n*n; j++)
      c++
  for(i=c; i > 0; i--)
    if(random(0...999) > 0)
      for(j=0; j < 3*n; j++)
        c++
    else
      for(j=2*n*n; j > 0; j--)
        c++
  return c

Let's look at the first nested loop

for(i=1; i < n; i=i*3)
    for(j=n; j < n*n; j++)
        c++

The outer loop runs log(n) times [log base 3, but base changes is multiplying by a constant which does not effect the asymptotic complexity] and the inner loop n^2 times, thus after this loop c = n^2 * log(n) .

For the second loop:

for(i=c; i > 0; i--)
    if(random(0...999) > 0)
       for(j=0; j < 3*n; j++)
          c++
   else
      for(j=2*n*n; j > 0; j--)
        c++

In the worst case the else case always happens, so we can modify it to

for(i=c; i > 0; i--)
  for(j=2*n*n; j > 0; j--)
    c++

The outer loop happens c times, which is O(n^2 * log(n)) and the inner loop increments c by 2*n^2 , so c is incremented by 2 * n^2 * n^2 * log(n) , adding the initial value we get that c (and thus the overall complexity) is in O(2*n^4*log(n) + n^2 * log(n)) = O(n^4 * log(n))

I hope I'm not just doing your homework for you. In future I recommend showing your thought processes thus far, rather than just your final answer.

Let's look at this code one section at a time.

int complexity(int n)
    int i,j,c
    for(i=1; i < n; i=i*3)
        for(j=n; j < n*n; j++)
            c++

So in the outer loop, i goes from 1 to n, but each time i is tripled. This means it will finish after log_3(n) loops. Changing base of log is just a constant factor, which doesn't matter in computational complexity, so we can just say log(n).

The inner loop has j going from n to n^2. O(n^2 - n) = O(n^2) since lower powers are dwarfed by higher ones (ie quadratic dwarfs linear).

So putting this all together, the first section has computational complexity O(n^2 logn). Now let's look at the second section.

for(i=c; i > 0; i--)
    if(random(0...999) > 0)
        for(j=0; j < 3*n; j++)
            c++
    else
        for(j=2*n*n; j > 0; j--)
            c++
return c

Now because the outer loop's initialization depends on c, we need to know what c is. c was incremented every time in the first two nested loops, so c's value is proportional to n^2 logn. Thus the outer loop will run O(n^2 logn) times.

For the inner loops, remember we are always considering the worst-case scenario. So the random number generator is a bit misleading: compute the computational complexity of both j loops, and assume the worst case always happens.

The first j loop goes from 0 to 3n, which is simply O(n). The second j loop goes from 2n^2 to 0 which is simply O(n^2). Since the second case is worse, we assume it always happens (even though this is highly improbable). So the inner loop is O(n^2).

Multiplying the two nested loops together, we get O(n^2 logn xn^2) = O(n^4 logn).

Finally, remember we have to see which of the two sections dominated. The first section was O(n^2 logn) and the second was O(n^4 logn), so we want O(n^2 logn + n^4 logn). The latter term obviously dominates, so the final answer is O(n^4 logn).

Hope this helps. Please ask if some part was unclear.

ps The current top answer states that c will be ~n^3/3. Because i is tripling every time, this is incorrect; it will be n^2 log_3(n). Otherwise their work is correct.

The way to solve this is to work out a formula that gives c as a function of n . (We can use the number of c++ operations as a proxy for the overall number of operations.)

In this case, the random function means that you can't get an exact formula. But you can work out two formulae, one for the case where random always returns zero, and the other for the case where random always returns > zero.

How do you work out the formula / formulae? Maths!

Hint: the worst-case will be one of the two cases that I mentioned above. (Which one? The one whose complexity is worst, of course!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM