1

So I've been trying to find big-Oh complexity using the following algorithm:

for (i = 1; i ≤ n;i + +)
     for (j = 0; j < n; j = j + i)
          print(Array[j]);

I was told that the optimal way would be represent using summations, and I know it can be represented in some form of a series, I really just don't know where to start. I can see that the outside loop iterates n times, but the inside loop is what gets me. I was hoping I could get a push in the right direction here rather than the answer.

1
  • The inner loop can be rewritten. See my answer. Commented Oct 10, 2014 at 15:52

2 Answers 2

1

If you expand the number of summations in both the for loops. The array indices should look as follows

(0,1,2,....n-1), (0,2,4,...n-1), (0,3,6,9....n-1) .....(0,n/2),(0)

If we observe the first parenthesis has n, the second has at the worst case n/2 and so on till the last parenthesis has 1.
So total number of summations can be written as

Summations = Sum(from i = 1 to n) [n/i]

Try solving the summation and you will get the total number of summations

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

3 Comments

You may wish to double-check that. It seems that the loop bounds for j do not depend on the loop bounds for i. So, this should just look like: (0,1, 2, ...n-1),(0,1,2, ... n-1) ... (0,1,2, ... n-1) repeated n times.
I think my summation is right since the innerloop is incremented as j = j+i. It would've been the case you are mentioning had it been j = j+1
I stand corrected. I misread the i as a 1 in the inner loop.
0

Let's use summations as you suggested, for a more analytical/algebraic approach.

First, consider only:

for (i = 1; i <= n; i++)
    //some O(1) operation

This is simple, because the var i takes all the range of integer values from 1 to n. We can express this loop with the following summation:

sum 1

Now, consider only:

for (j = 0; j < n; j = j + k)
    //some O(1) operation

where k is a constant positive integer term (like 1, 2, 3, ... or etc.), and k <= n

In this case var i doesn't take all the range of integers values from 0 to n-1. For example, let k=2, then we can represent what is going on in the following image:

range k2

What you see in black is the interval of possible integers, and what is in red represent the actual integer values var i takes. As you see, var i is "jumping" over odd numbers to reach n-1, so in this case (when k=2) only even numbers are taken.

As result, when k=2, the complexity is given by the summation:

sum 2

In general, a similar approach can be done for any k. In particular, note that as k tends to n, the complexity will tend to decrease.

For our purposes we can rewrite the loop:

for (j = 0; j < n; j = j + k)

As:

for (j = 0; j < n/k-1; j++) 

Both have the same complexity.

Finally, consider:

for (i = 1; i <= n; i++)
     for (j = 0; j < n; j = j + i)
          //some O(1) operation

In the inner loop, we have i playing the same role our k before. This means that the inner loop is going to be executed with every possible value of i in the range 1 to n.

Rewrite as:

for (i = 1; i <= n; i++)
     for (j = 0; j < n/k-1; j++)
          //some O(1) operation

Therefore, the complexity is given by the following two nested summations:

enter image description here

First, start evaluating with the inner summation (the right one):

enter image description here

NOTE that i changed the var name from i to x accidentally, but that is not really important.

We have here exactly the same result given by the other poster.

Now if you just expand the last summation the result is:

enter image description here

Now what you have in brackets is the n-th harmonic number, not to confuse with harmonic series. This are interesting numbers in discrete mathematics (and other fields).

This can be denoted as:

enter image description here

Note that from this analysis you can obtain an asymptotic tight bound.

BTW: you can check the last equation in this link

The syntax is:

Sum[Sum[1, {j, 0, (n/i)-1}], {i, 1, n}]=Sum[n/x, {x, 1, n}]=n*Sum[1/x, {x, 1, n}]=n*(1+1/2+1/3+...+1/n)=n*HarmonicNumber[n]

I hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.