4

I am trying to figure out the complexity of a for loop using Big O notation. I have done this before in my other classes, but this one is more rigorous than the others because it is on the actual algorithm. The code is as follows:

for(i=n ; i>1 ; i/=2) //for any size n
{
    for(j = 1; j < i; j++)
    {
      x+=a
    }
}

and

for(i=1 ; i<=n;i++,x=1) //for any size n
{
    for(j = 1; j <= i; j++)
    {
      for(k = 1; k <= j; x+=a,k*=a)
      {

      }
    }
}

I have arrived that the first loop is of O(n) complexity because it is going through the list n times. As for the second loop I am a little lost! Thank you for the help in the analysis. Each loop is in its own space, they are not together.

4
  • Is your comment in the last paragraph describing your thoughts on the first problem or the second one? Commented May 24, 2013 at 20:58
  • last paragraph describing the first problem Commented May 24, 2013 at 21:02
  • There is no list in your code, so I assume by "going through the list" you simply mean that x += a is executed n times, right? If so, how did you come to that conclusion? Commented May 24, 2013 at 21:31
  • This may help cs.stackexchange.com/questions/4590/… Commented May 24, 2013 at 22:01

3 Answers 3

2

Consider the first code fragment,

for(i=n ; i>1 ; i/=2) //for any size n
{
    for(j = 1; j < i; j++)
    {
      x+=a
    }
}

The instruction x+=a is executed for a total of n + n/2 + n/4 + ... + 1 times.

Sum of the first log2n terms of a G.P. with starting term n and common ratio 1/2 is, (n (1-(1/2)log2n))/(1/2). Thus the complexity of the first code fragment is O(n).

Now consider the second code fragment,

for(i=1 ; i<=n; i++,x=1)
{
    for(j = 1; j <= i; j++)
    {
      for(k = 1; k <= j; x+=a,k*=a)
      {

      }
    }
}

The two outer loops together call the innermost loop a total of n(n+1)/2 times. The innermost loop is executed at most log<sub>a</sub>n times. Thus the total time complexity of the second code fragment is O(n2logan).

Sign up to request clarification or add additional context in comments.

Comments

1

You may formally proceed like the following:

Fragment 1:

enter image description here

Fragment 2 (Pochhammer, G-Function, and Stirling's Approximation):

enter image description here

With log(G(n)).

[UPDATE of Fragment 2]:

With some enhancements from "DISCRETE LOOPS AND WORST CASE PERFORMANCE" publication, by Dr. Johann Blieberger (All cases verified for a = 2):

enter image description here

Where:

enter image description here

enter image description here

enter image description here

Therefore,

enter image description here

Comments

0

EDIT: I agree the first code block is O( n )

You decrement the outer loop i by diving by 2, and in the inner loop you run i times, so the number of iterations will be a sum over all the powers of two less than or equal to N but greater than 0, which is nlog(n)+1 - 1, so O(n).

The second code block is O(loga(n)n2) assuming a is a constant.

The two outermost loops equate to a sum of all the numbers less than or equal to n, which is n(n-1)/2, so O(n2). Finally the inner loop is the powers of a less than an upper bound of n, which is O(logan).

2 Comments

can you please more expling why O(2log(n))
2^log_2(n) is just n, but I don't think you're right here. I'm pretty sure it actually is O(n).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.