Will this code have Time Complexity O(n^5) or O(n^3) or anything else? - time-complexity

I am having problem in identifying the time complexity of this nested loop code. Some of my friends are saying O(n^3) and O(n^5).
sum = 0;
for(int i=0; i<n; i++)
for(int j=0; j<i*i; j++)
for(int k=0; k<j; k++)
sum++;

WolframAlpha gives the total count of increments to sum as
sum_(i=0)^(n-1)( sum_(j=0)^(i^2 - 1)( sum_(k=0)^(j-1) 1))
= 1/20 (n - 2) (n - 1) n (n + 1) (2 n - 1)
= n^5/10 - n^4/4 + n^2/4 - n/10
which is in θ(n^5).

I would say time complexity is about N * (N*N)/2 * N/2. Combined it would be O(N^4).
Edit: it's O(N^5)because the inner loop is squared by the middle loop!
But don't take my word for it. For these kind of questions, why don't you run a few examples of your code with different N and compare the sums, you will figure out what the time-complexity is soon enough.

Related

Time Complexity for nested loops with half-size

void function(int n)
{
int count = 0;
// outer loop
for (int i=n/2; i<=n; i++)
// middle loop
for (int j=1; j+n/2<=n; j = j++)
// inner loop executes log n times
for (int k=1; k<=n; k = k * 2)
count++;
}
I am doing some exercise, and can someone please help me to figure out the Big-Oh of the above algorithm? I understand that the inner most loop executes for log n times. What about the outermost loop and middle loop ? Would that also be log n or n/2 ?
Assuming your code with full indentation is this:
void function(int n)
{
int count = 0;
// outer loop
for (int i=n/2; i<=n; i++){
// middle loop
for (int j=1; j+n/2<=n; j++){
// inner loop executes log n times
for (int k=1; k<=n; k = k * 2){
count++;
}
}
}
}
The time complexity can be calculated as follows:
The innermost loop executes (log n) times, so its complexity is O(log n).
The middle loop with j as the loop variable executes n / 2 times, with the innermost loop executing, each time in its iteration. Therefore, the time complexity of the middle loop is (n / 2) * O(log n) = O(n * log n).
Similarly, the outermost loop also executes (n / 2) times, with the middle loop executing completely in it each iteration. So, its time complexity will be (n / 2) * O(n * log n) = O(n * n * log n).
Hence, the overall time complexity will be O(n^2 * log n).

What is the complexity of this for loop, for (int j = i; j < n; j++)?

what is the complexity of the second for loop? would it be n-i? from my understanding a the first for loop will go n times, but the index in the second for loop is set to i instead.
//where n is the number elements in an array
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
// Some Constant time task
}
}
In all, the inner loop iterates sum(1..n) times, which is n * (n + 1) / 2, which is O(n2)
If you try to visualise this as a matrix where lines represents i and each columns represents j you'll see that this forms a triangle with the sides n
Example with n being 4
0 1 2 3
1 2 3
2 3
3
The inner loop has (on average) complexity n/2 which is O(n).
The total complexity is n*(n+1)/2 or O(n^2)
The number of steps this takes is a Triangle Number. Here's a bit of code I put together in LINQpad (yeah, sorry about answering in C#, but hopefully this is still readable):
void Main()
{
long k = 0;
// Whatever you want
const int n = 13;
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
k++;
}
}
k.Dump();
triangleNumber(n).Dump();
(((n * n) + n) / 2).Dump();
}
int triangleNumber(int number)
{
if (number == 0) return 0;
else return number + triangleNumber(number - 1);
}
All 3 print statements (.Dump() in LINQpad) produce the same answer (91 for the value of n I selected, but again you can choose whatever you want).
As others indicated, this is O(n^2). (You can also see this Q&A for more details on that).
We can see that the total iteration of the loop is n*(n+1)/2. I am assuming that you are clear with that from the above explanations.
Now let's find the asymptotic time complexity in an easy logical way.
Big Oh, comes to play when the value of n is a large number, in such cases we need not consider the dividing by 2 ( 2 is a constant) because (large number / 2) is also a large number.
This leaves us with n*(n+1).
As explained above, since n is a large number, (n+1) can be approximated to (n).
thus leaving us with (n*n).
hence the time complexity O(n^2).

Big O of Nested Loop (int j = 0; j < i * i; ++j)

Question 1
for (i = 0; i < n; i++) {
for (j = 0; j < i * i ; j++){
}
}
Answer: O(n^3)
At first glance, O(n^3) made sense to me, but I remember a previous problem I did:
Question 2
for (int i = n; i > 0; i /= 2) {
for (int j = 0; j < i; j++) {
//statement
}
}
Answer: O(n)
For Question 2, the outer loop is O(log n) and the inner loop is O(2n / log n) resulting in O(n). The inner loop is O(2n / log n) because - see explanation here: Big O of Nested Loop (int j = 0; j < i; j++)
Why we don't do Question 1 like Question 2 since in Question 1, j also depends on i which means we should really be taking the average of how many iterations will occur in the inner loop (as we do in Question 2).
My answer would be: O(n) for the outer loop and O(n^2 / n) for the inner loop which results in O(n^2) for Question 1.
Your answer is wrong. The code is Θ(n³).
To see that note that the inner loop takes i² steps which is at most n² but for half of the outer loop iterations is at least (n/2)² = n²/4.
Therefore the number of total inner iterations is at most n * n² = n³ but at least n/2 * n²/4 = n³/8.
Your consideration is wrong in that the inner loop takes on average proportional to n² many iterations, not n² / n.
What your inner for loop is doing, in combination with the outer for loop, is calculating the sum of i^2. If you write it out you are adding the following terms:
1 + 4 + 9 + 16 + ...
The result of that is (2n^3+3n^2+n)/6. If you want to calculate the average of the number of iterations of the inner for loop, you divide it by n as this is the number of iterations of the outer for loop. So you get (2n^2+3n+1)/6, in terms of Big O notation this will be O(n^2). And having that gives you... nothing. You have not gain any new information as you already knew the complexity of the inner for loop is O(n^2). Having O(n^2) running n times gives you O(n^3) of total complexity, that you already knew...
So, you can calculate the average number of iterations of the inner for loop, but you will not gain any new information. There were no cuts in the number of iteration steps as there were in your previous question (the i /= 2 stuff).
void fun(int n, int k)
{
for (int i=1; i<=n; i++)
{
int p = pow(i, k);
for (int j=1; j<=p; j++)
{
// Some O(1) work
}
}
}
Time complexity of above function can be written as 1k + 2k + 3k + … n1k.
In your case k = 2
Sum = 12 + 22 + 32 + ... n12.
= n(n+1)(2n+1)/6
= n3/3 + n2/2 + n/6

Am I Oversimplifying Calculating Complexity

I'm just trying to calculate complexity on some program fragments, but I'm, worried I'm making things too simple. If I put my fragments and answers down, can you tell me if I'm doing anything wrong with it?
(a)
sum = 0;
for (i = 0;i < n;i++)
sum++;
ANSWER: n, only one for loop
(b)
sum = 0;
for (i = 0;i < n;i++)
for (k = 0;k < n*n;k++)
sum++;
ANSWER: n^2 because of the nested loop, although I wonder if the n*n in the nested loop makes it n^3
(c)
sum = 0;
for (i = 0;i < n;i++)
for (k = 0;k < i;k++)
sum++;
ANSWER: n^2
(d)
sum = 0;
for (i = 0;i < n;i++)
for (k = 0;k < i*i;k++)
sum++;
ANSWER: n^2, but I have the same concern as b
(e)
sum= 0;
for (i = 0;i < n;i++)
for (k = i;k < n;k++)
sum++;
ANSWER: n^2
Since in all your examples the main operation is sum++, we are bound to count the number of times this basic operation is performed.
Also, in all cases, there is the i++ operation, that also counts, as well as the k++. Finally, these counters have to be compared with their limits at every step, and we should also take these comparisons into account. Now, these additional operations don't change the number of iterations; they simply make each iteration more expensive. For instance,
(a)
sum = 0;
for (i = 0;i < n;i++)
sum++;
repeats n times: i++, sum++ and i<n, all of which gives 3n operations of similar complexity. This is why the total complexity is O(n).
Once this has been understood, it is no longer necessary to analyze the complexity in as much detail, because the big-O notation will take care of these additional calculations.
The second example
sum = 0;
for (i = 0;i < n;i++)
for (k = 0;k < n*n;k++)
sum++;
repeats n time the operation
for (k = 0;k < n*n;k++)
sum++;
Because of the previous case, this operation has complexity O(n*n) as here the limit is n*n rather than n. Thus the total complexity is O(n*n*n).
The third example is similar, except that this time the operation being executed n times is
for (k = 0;k < i;k++)
sum++;
which has a complexity that changes with i. Therefore, instead of multiplying by n we have to sum n different things:
O(1) + O(2) + ... + O(n)
and since the constant factor implicit in the O is always the same (= number of variables being increased or compared at every elementary step), we can rewrite it as
O(1 + 2 + ... + n) = O(n(n+1)/2) = O(n*n)
The other examples are similar in that they can be analyzed following these very same ideas.

Time Complexity: O(logN) or O(N)?

I thought the time complexity of the following code is O(log N), but the answer says it's O(N). I wonder why:
int count = 0;
for (int i = N; i > 0; i /= 2) {
for (int j = 0; j < i; j++) {
count += 1;
}
}
For the inners for-loop, it runs for this many times:
N + N/2 + N/4 ...
it seems to be logN to me. Please help me understand why here. Thanks
1, 1/2, 1/4, 1/8... 1/2 ** n is a geometric sequence with a = 1, r = 1/2 (a is the first term, and r is the common ratio).
Its sum can be calculated using the following formula:
In this case, the limit of the sum is 2, so:
n + n/2 + n/4 ... = n(1 + 1/2 + 1/4...) -> n * 2
Thus the complicity is O(N)
Proceeding step by step, based on the code fragment, we obtain: