Big O complexity of for nested loop - complexity-theory

I must find the Big O complexity for this loop:
for(i=0; i<n; i++)
for(j=0; j<n-i; j++)
print(i)
I think it's O(n^2), but I'm not really sure. Can anyone help me?

You are correct, the complexity is O(n^2).
The first loop (for(i=0; i<n; i++)) is pretty easy. That is O(n).
The second loop (for(j=0; j<n-i; j++)) is trickier: It is (theoretically) O(n - i).
When you combine those two, you will end up with:
O = n^2 - i*n
Since the Bit O notation only takes the largest factor, you simply remove the - i*n and end up with:
O(n^2)

Related

Big O complexity of three nested loops with the last loop in an if statement

Suppose we have the following block of code:
sum = 0;
for(i = 0; i < n; i++)
for(j = 0; j < n; j++)
if(i == j)
for(k = 0; k < n; k++)
sum++;
In the book I'm reading it's stated that the complexity is O(n^2), thetha (n^2) to be precise, but I'm not sure why. When trying to calculate it myself I get O(n^3):
Outer loop complexity is O(n)
By the second loop the complexity gets to O(nˆ2) by following the nested loops rule
The if statement would be true n times, so by the third loop the complexity would be O(n*nˆ2)
The sum++ statement takes constant time, so complexity stays at O(nˆ3)
That's my logic, but it's flawed it seems. What is the reasoning for the O(n^2) complexity here?
The second loop runs n steps. Each step takes only constant time (evaluating the if statement) except for one step. The i-th step takes O(n) because the third loop is executed. So the second loop takes 2n = O(n) in total. So in total you get O(n^2).

Big(o) notation logn or n?

for(int i = 1; i < N; i = 2*i){
for(j=0; j<i; j++){
}
}
so I just learned that a logN for loop is one that either divides or multiplies in a statement, which the outerloop is doing. However, because the inner loop is incrementing with addition, and that linear time is a higher complexity than logN, would this for loop be considered O(n)?
The inner function is O(n) because it runs in linear time, and the outer function is O(log n) because i is multiplied by 2 every iteration. So to answer you question, yes the inner loop would be considered O(n) because j++ runs in linear time. But since O(n) is higher complexity than O(log n), then O(n) takes more precedence and the overall run time will be O(n).

What is the time complexity and space complexity of following code

I want to know the time complexity of following piece of code
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
printf("hi")
}
}
Time complexity in loop
http://www.geeksforgeeks.org/analysis-of-algorithms-set-4-analysis-of-loops/
http://www.geeksforgeeks.org/analysis-of-algorithms-set-4-analysis-of-loops/
i think following link is used full for this problem :)
Time complexity is nothing but the number of instructions executed in your program. Now, in your program you have two loops. Outer loop will iterate i=0 to i=N-1, which is total of N instructions that is O(N). Since you also have a inner loop which will again iterate from j=i+1 to j=N-1 for each i.
Hence, time complexity will be O(N^2).
At each iteration of the inner loop work done is a follows
n
n-1
n-2
....
...
1
So total work done will be n+(n-1)+(n-2)+...+...+1, which turn out to be the sum of n natural numbers i.e n*(n+1)/2 so time complexity is O(n^2). As no auxiliary space is used so space complexity is O(1)

Big O Run Time for nested loops?

How do you calculate the big O run-time efficiency for this code? My gut tells me it is O(n^3) but I'm not sure, as I'm not really sure if the loops are independent or dependent.
for (i=1; i<=n; i++)
for (j=1; j<=n; j++)
for (k=1; k<=n; k++)
print ("%d %d %d/n", i, j, k);
Your gut feeling is right. You have three nested for loops iterating over n, so for each of the first n loops you make another n loops, each of which in turn make n more loops. Thus O(n^3).
Edit: think about how this will play out-
i is first 1, j 1 as well, and then k loops 1 through n. Only after k has undergone that whole loop, will j increment to 2, then k undergoes the loop once again and so on.
Yes, this function would be O(N^3) because inside each loop you are running N iterations.
The loops are not independent, because they are nested.
N * N * N = N^3

How is the time complexity of these simple loops calculated?

I understand how:
for (int i=0; i<n; i++)
This time complexity is O(n).
for (int i=0; i<n; i++)
for (int j=0; j<n; j++)
for (k=0; k<n; k++)
this is O(n^3) right?
i=1
do
//......
i++
while (i*2 <n)
Is this O(n)? Or is it exactly O(n/2)?
O(n/2) is O(n) only with a constant coefficient of 1/2. The coefficient can be 10 billion, it would still be O(n), and not e.g. O(n^(1.0001)) which is a different complexity class.
The first one complexity O(n^3), correct.
The second one, O(cn), c constant. No matter how huge c is, according to the definition of big-O, the complexity is still O(n).
However, O-notation is considered harmful. See here.
The first one of O(n3), you're right.
Your second algorithm is O(n/2) = O(Cn) = O(n). 1/2 is a constant so we can safety discard it.
This fragment of code:
i=1
do
//......
i++
while (i*2 < n);
is equivalent to that one:
for ( i = 1; i < n / 2 ; ++ i );
Superficially, this is O(n).

Resources