6

I had this question for my assignment the other day, but I was still unsure if I'm right.

for(int i =1; i <n; i++)   //n is some size
{             
    for(j=1; j<i; j++)
    {
        int k=1;

        while (k<n)
        {
           k=k+C;   //where C is a constant and >=2
        }
    }
}

I know the nested for loops is O(n^2) but I wasn't sure with the while loop. I assumed that the whole code will be O(n^3).

spaghettifunk
  • 1,936
  • 4
  • 24
  • 46
user977151
  • 495
  • 5
  • 9
  • 18

4 Answers4

2
    int k=1;

    while (k<n){
       k=k+C                    //where C is a constant and >=2
    }

This will take (n-1)/C steps: write u = (k-1)/C. Then, k = Cu + 1 and the statement becomes

u=0;
while(u < (n-1)/C) {
    u=u+1
}

Hence the while loop is O(n) (since C is constant)

EDIT: let me try to explain it the other way around.

Start with a dummy variable u. The loop

u=0;
while(u < MAX) {
    u = u+1
}

runs MAX times.

When you let MAX = (n-1) / C, the loop is

u=0;
while(u < (n-1)/C) {
    u=u+1
}

And that runs (n-1)/C times.

Now, the check u < (n-1)/C is equivalent to C*u < n-1 or C*u + 1 < n, so the loop is equivalent to

u=0;
while(C*u + 1 < n) {
    u=u+1
}

Now, suppose that we rewrote this loop in terms of a variable k = C * u + 1. Then,

u=0;
k=1; // C * 0 + 1 = 1

The loop looks like

while(C*u + 1 < n) {
while(k < n) {

and the inner condition is

    u=u+1
    k=k+C //C * (u+1) + 1 = C * u + 1 + C = old_k + C 

Putting it all together:

    int k=1;

    while (k<n){
       k=k+C
    }

takes (n-1)/C steps.

Foo Bah
  • 25,660
  • 5
  • 55
  • 79
2

The inner loop is literally O(n/C)=O(n), so yes, overall it's O(n^3) (the second loop has an upper bound of O(n))

Blindy
  • 65,249
  • 10
  • 91
  • 131
1

Formally, you may proceed using the following methodology (Sigma Notation):

enter image description here

Where a symbolizes the number of constant operations inside the innermost loop (a = 1 if you want to count the exact number of iterations).

  • Sad the imgur.com is completely blocked by my companies proxy :| – wonko realtime Mar 31 '14 at 15:26
  • Can you [interpret LaTeX code](http://www.codecogs.com/latex/eqneditor.php)? I can share the script with you. – Mohamed Ennahdi El Idrissi Mar 31 '14 at 15:31
  • Can't find a quote from SO about it atm. I personally think it would be cool (and Latex could even be indexed which a pic can't), but I'm not sure if others would appreciate it because it might confuse readers of your posting, depending on how you'd add it, so I'm going to watch it at home. [ Maybe someone should ask SO to integrate some sort of Latex to html renderer (http://stackoverflow.com/questions/116054/what-is-the-best-way-to-embed-latex-in-a-webpage) ] – wonko realtime Mar 31 '14 at 15:41
  • Can you see what is generated by this site: [http://www.codecogs.com/latex/eqneditor.php](http://www.codecogs.com/latex/eqneditor.php)? – Mohamed Ennahdi El Idrissi Mar 31 '14 at 15:43
  • The size of the message transcends the allowed number of characters in the comments. Do you have an email address? – Mohamed Ennahdi El Idrissi Mar 31 '14 at 15:47
  • Never posting emails :) You could post to http://pastebin.com/ and post the link here – wonko realtime Mar 31 '14 at 15:50
0

Well, you would need to look at how many times the while loop body is run for a given value of n and C. For example n is 10 and C is 3. The body would run 3 times: k = 1, k = 4, k = 7. For n = 100 and C = 2, the body would run 50 times: k = 1,3,5,7,9,...,91,93,95,97,99. It is a matter of counting by C until n. You should be able to calculate the Big-O complexity from that clue.

  • 2
    I don't like your counting approach to infinite sequences (that's what asymptotic means btw)... A simple "the number of loops grows linearly with `n` at a rate of 1/2" would be easier to understand and far more accurate. – Blindy Oct 03 '11 at 18:28