I know the big O complexity of this algorithm is O(n^2), but I cannot understand why.
int b=0;
for(int i=n; i>0; i--)
for(int j=0; j<i; j++)
b=b+5;
I know that the outer loop is O(n). I know that the inner loop will run n + (n-1) + (n-2) + ... + 1 times. That is as far as I can get. I am not sure where to go from there.
My question is, can somebody explain why that algorithm is O(n^2) ?
So, the total number of times the whole block of code would run
= n + (n-1) + ...+ 1
= n * (n+1) / 2
= O(n^2).
The other statements would take O(1), so their's would have no effect(not much role) on complexity(their's being a constant).
outer loop | inner loop
i=n | inner loop executes n times
i=n-1 | inner loop executes n-1 times
i=n-2 | inner loop executes n-2 times
.
.
.
i=1 | inner loop executes 1 time and exits
now summing up total no of times inner loop executes : n + (n-1) +(n-2)+.....+1= n*(n+1)/2 = O(n2)
Basically because there are n^2 more operations than in a single loop.
Related
Given:
for (int i = 1; i <= n; i *= 2) {
for (int j = 0; j < i; j++) {
// Statement(s) that take(s) constant time
}
}
Running Time Complexity = O(n)
Explanation was:
I understand that the outer loop is log(n) and the inner loop is O(n).
But why time complexity is not O(n log n)? why it's O(n + log n)?
The partial sums calculation is correct. However, the total execution time explanation is unclear, although the result is indeed O(n).
The actual time is consumed by executing the inner statements and the number of these statements executions is defined by the loops. The method of partial sums already takes into consideration both loops (1 time unit for i=1, 2 time units for i=2, 4 time units for i=4, etc.) and in total the internal statements are executed O(n) times as shown by the partial sums calculation. Therefore, total time is O(n)*O(constant for the internal statements)=O(n). I don't see any reason to include the O(logn) in calculations here.
The outer loop runs log(n) times. But, in each time, the inner loop does not run in O(n). As shown in the explanation, if i = 1, the inner loop runs 1 time. If i = 2, the inner loop runs 2 times. If i = 4, the inner loop runs 4 times. Hence, the total number of running time is T(n) = 1 + 2 + 4 + ... + 2^log(n).
Now, simplify the T(n):
T(n) = 2^(log(n)+1) - 1 = 2^log(n) * 2 - 1 = 2n - 1 = Theta(n)
I was asked if what time complexity if this:
What is the time complexity (with respect of n) of this algorithm:
k=0
for(i = n / 2 ; i < n ; i++ ) {
for( j=0 ; j < i ; j++)
k = k + n / 2
}
choices was : a. O(n) b. O(n/2) c. O(n log(n) and d. O(n^2)
can have a multiple answers.
i know the algorithm above is d. O(n^2) but i came with with a. O(n) since it is looking for complexity of n only?.
if you are to have this question. how would you answer it.?? im so curious about the answer.
The answer is O(n²).
This is easy to understand. I will try to make you understand it.
See, the outer for loop block is executed n - n/2 = n/2 times.
Of course it depends whether the number n is even or odd. If it's even then the outer loop is executed n/2 times. If it's odd then it's executed for (n-1)/2 times.
But for time complexity, we don't consider this. We just assume that the outer for loop is executed n/2 times where i starts from n/2 and ends at n - 1 (because the terminating condition is i < n and not i <= n).
For each iteration of the outer loop, the inner loop executes i times.
For example, for every iteration, inner loop starts with j = 0 to j = i - 1. This means that it executes i times (not i - 1 times because j starts from 0 and not from 1).
Therefore, for 1st iteration the inner loop is executed i = n / 2 times. i = n / 2 + 1 for 2nd iteration and so on upto i = n - 1 times.
Now, the total no. of times the inner loop executes is n/2 + (n/2 + 1) + (n/2 + 2) + ... + (n - 2) + (n - 1). It's simple math that this sums up to (3n² - n)/2 times.
So, the time complexity becomes O((3n² - n)/2).
But we ignore the n term because n² > n and the constant terms because for every n they will remain the same.
Therefore, the final time complexity is O(n²).
Hope this helps you understand.
I've been searching for a while on how to calculate complexity of algorithms.. and while there are some great explanations, I just don't seem to understand exactly how it works.. so I thought maybe on this example, someone can clarify it for me
void test(int n){
for ( int i = 1; i <= n; i++, n=n/2)
for(int j = 1; j <= n; j++)
..O(1)..
}
The first time the outer loop runs, the inner loop will run n times.
The second time the outer loop runs, the inner loop will run n/2 times.
The third time the outer loop runs, the inner loop will run n/4 times.
The fourth time the outer loop runs, the inner loop will run n/8 times.
. . .
The last time the outer loop runs, the inner loop will run 1 time.
When you sum the series you get
n + n/2 + n/4 + n/8 + ... = 2n
The body of the inner loop executes a total of 2n times, so the complexity of the algorithm is O(n).
Need help figuring this hw example out guys.
the example states 0(n) running time.
I see that the outter loop is O(logn)
I cant figure out how to describe the inner loop in relation to n.
Help is much appreciated.
for (int i = 1; i <= N; i = i*2) // log n
for (int j = 0; j < i; j++) // less than n i don't know how to describe the growth
sum++;
Answer:: 0(n)
It might be easiest to analyze the runtime of the piece of code in the aggregate. On the first iteration of the outer loop, the inner loop will run 1 time. On the second iteration of the outer loop, the inner loop will run 2 times. On the third iteration of the outer loop, the inner loop will run 4 times. More generally, on the kth iteration of the outer loop, the inner loop will run 2k times. The outer loop stops as soon as i becomes greater than N, which happens after log2 N iterations.
If we sum up the total work done, we'll see that it's
1 + 2 + 4 + 8 + ... + 2log2 N = 2log2 N + 1 - 1 = 2n - 1
(This uses the fact that 1 + 2 + 4 + 8 + ... + 2k = 2k+1 - 1). Therefore, the total work done for the entire piece of code (that is, including both loops) is O(n).
The inner loop is O(n) time because it will go through each element once. If i is 1000, 1000 times, if i is 10000, 10000 times, etc.
I have a contradiction by analyzing the running time of a program. For example, consider the following piece of code:
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
.....
}
}
Here, the computational complexity of 1st for loop is O(n2), and for the second loop it is O(n). However, the second loop is executed n2 times whereas first loop is executed n times. For example, if we put a cout statement inside the inner loop, it outputs n2 times, but if we put a cout somewhere inside the 1st loop but outside the inner loop, it outputs n times. So why do we say the complexity of inner loop is O(n), but for outer loop it is O(n2). We say the complexity of outer loop is O(n2) but it executes n times, why is this the case? Am i doing something wrong? Thanks.
The inner loop executes n times, which takes O(n). The outer loop executes the inner loop n times, but you have to account for the cost of the inner loop for each of those n outer loop executions. This makes it O(n * O(n)) = O(n^2).
Outer loop will run for n times and inner loop will run for n times for every iteranion of outer loop making inner loop to run for n^2 times. Thus statements in inner loop will get executed for n^2 times.
Let say, n = 7 ; then
for(int i=0 ; i< 7 ; i++) //time complexity is : (1) + (7+1) + (7)
{
for(int j=0;j<7;j++) // time complexity is: (1) + [(7+1)+(7+1)+(7+1)+(7+1)+(7+1)+(7+1)+(7+1)] + (7)
{
.....
}
}
Total time complexity = [2+2(7)] + [1 + (7+1)^2 + 7]
Now replace 7 = n ; We will get
Total time complexity = [2+2n] + [1 + (n+1)^2 + n] = 2 + 2n + 1 + (n^2 + 2n + 1) + n = n^2 + 5n + 4
Here the dominant term is n^2. So the worst case will be O(n^2)