Is this algorithm in N^2? - algorithm

int sum = 0;
for (int i = 1; i <= N; i++)
for (int j = 1; j <= i*i; j++)
sum++;
Is this complexity 1 + 2^2 + 3^2 + 4^2 ... + N^2? How to represent it in big-o notation?

The sum is N(N+1)(2N+1)/6 so your algorithm is O(N^3).
If you expand the sum you have 1/3 * N^3 + ...
You can see it simply by plotting the values of sum after running the algorithm for different values of N:

Straightforwardly, using Sigma notation:

Another way to look at it is to use the "worst case" analysis.
Originally, we have:
The outer for loop will run N times
The inner for loop will run i*i times
This might be confusing since i seems to be changing based on the outer for loop.
However, in the "worst case" scenario, the largest value that i ever attains is N.
Therefore, just substitute N instead of i in our analysis.
Now we have:
The outer for loop will run N times
The inner for loop will run N*N times
And the time complexity of this situation is of course O(N³).

you can always use the intuitive way.
for i = 2 second loop 1 time, first loop 1 time. 1*1
for i = 3 second loop 9 times, first loop 2 times. 9*2
for i = 4 second loop 16 times, first loop 3 times 16*3
for i = 5 second loop 25 times, first loop 4 times. 25*4
.
.
.
you can see the pattern easily. it is (nˆ2)*(n-1) therefore its big o should be O(nˆ3)

Related

Determining the big-O of three nested for loops with if statment

What is the big-O for the following code :
y=1;
x=3;
for(int i =1 ; i < =n ; i*=2)
for(int j =1; j<= i * i; j++)
if (i % j == 0)
for(int k = 1; k<=j; k++)
y=y*x;
My Thoughts :
Looking at another similar questions I think the inner most loop is O(n) and the first loop is O(log (n))..as for the middle its O(n^2)
so the overall result would be O(log(n)*n^3)
Is my answer and way of thinking right ? I'm new to this so i hope i can get some help explaning how this loops work.
the most inner loop will run j time if i % j == 0. As the middle loop will run i^2 times, only when j < i it will be possible to satisfy the specified condition. Hence, among i^2 iteration of the middle loop, at least i^2 - i times, the condition will not be satisfied.
Suppose we denote the number of divisors of i with tau(i), among j < i only tau(i) times the condition will satisfy that means the total complexity of the most inner loop is equal to the sum of divisions of i which is at most 77/16 i (see this post for the proof).
Hence, the total complexity of the middle loop with the inner loop is at most (i^2 - i) + (i - tau(i)) + 77/16 i = i^2 + 77/16 i - tau(i).
We also know that the tau(i) is in O(i^(1/loglog(i))) (see the proof here). Now, to find the complexity of the whole loop, we need to sum the last expression for i = 1, 2, 4, ..., n. As we desire to find the asymptotic complexity and we have a sum here, we can ignore the lower powers of i. Therefore, the time complexity of the whole loop is 1 + 2^2 + (2^2)^2 + ... + (2^2)^log(n) = ((2^2)^(log(n)+1)-1)/(2^2-1) = Theta(n^2) (a geometric sum with factor of 2^2 and log(n) items).
In sum, the higher time complexity analysis for the specified code is Theta(n^2) which is also in O(n^2) as well.

confused about the time complexity of the follwing func. A good explanation would be helpful

If the the first loop runs for n+1 times.
second loop runs for n(n+1) times.
third loop will run for ??? it has n^2+1 one relation with with with the second loop i guess but how about with the first one
somefunction(n) {
c = 0
for (i = 1 to n*n)
for (j = 1 to n)
for (k = 1 to 2*j)
c = c+1
return c
}
The first loop has O(n**2) iterations.
The second loop has O(n) iterations.
The third loop has O(n) iterations as well, since j is steadily increasing towards n.
(It's a little easier to see if you sum up the number of times c = c + 1 executes for the two inner loops combined. The inner loop runs 2 times for j = 1, 4 for j = 2, ..., and 2*n times for j = n. 2 + 4 + .. + 2*n = O(n**2).)
You can then (loosely speaking) multiply the three values together to get a total bound of O(n**4).

Nested Loops Asymptotic analysis

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.

Loop Analysis - Analysis of Algorithms

This question is based off of this resource http://algs4.cs.princeton.edu/14analysis.
Can someone break down why Exercise 6 letter b is linear? The outer loop seems to be increasing i by a factor of 2 each time, so I would assume it was logarithmic...
From the link:
int sum = 0;
for (int n = N; n > 0; n /= 2)
for (int i = 0; i < n; i++)
sum++;
This is a geometric series.
The inner loops runs i iterations per iteration of the outer loop, and the outer loop decreases by half each time.
So, summing it up gives you:
n + n/2 + n/4 + ... + 1
This is geometric series, with r=1/2 and a=n - that converges to a/(1-r)=n/(1/2)=2n, so:
T(n) <= 2n
And since 2n is in O(n) - the algorithm runs in linear time.
This is a perfect example to see that complexity is NOT achieved by multiplying the complexity of each nested loop (that would have got you O(nlogn)), but by actually analyzing how many iterations are needed.
Yes its simple
See the value of n is decreasing by half each time and I runs n times.
So for the first time i goes from 1 to n
next time 0 to n/2
and hence 0 to n/k on kth term.
Now total time inner loop would run = Log(n)
So its a GP the number of times i is running.
with terms
n,n/2,n/4,n/8....0
so we can find the sum of the GP
2^(long(n) +1)-1 / (2-1)
2^(long(n)+1) = n
hence n-1/(1) = >O(n)

Basic Algorithm Analysis and Summation Notation

So for a homework we had to count the number of steps in a piece of code. Here it is:
int sum = 0;
for (int i = 1; i <= n*n; i++)
for (int j = 1; j <= i; j++)
for (int k = 1; k <= 6; k++)
sum++;
My prof (i think) explained that the number of operations in the 2nd line could be found using summation notation, like so:
n^2
Σ x 4 + 3
i=1
which would be 1/2(n^4 + n^2) x 4 + 3 = 2n^4 + 2n^2 + 3
but from just looking the line, I would think it would be something like 4n^4 + 2 (my prof said 4n^4 + 3, I'm not sure where the third operation is though...)
Am I doing the summation notation wrong here? It made sense to me to do summation notation for nested for loops, but I don't know why it would work for a for loop by itself.
Thanks.
Actually even your prof result is wrong. The exact result is 3n^4+3n^2.
To obtain that result simply consider:
All passages are pretty simple (the passage from step 4 to step 5 is immediate if you consider the formula for the sum of the firsts n natural numbers).
I guess both you and your professor are wrong. According to my calculation (I might be wrong too) it should be 3n^4+3n^2.
The outer most loop will run n^2 times. Taken this into consideration the inner loop will run 1 time for the first iteration and so on till n^2. i.e. from j=1 to j=1,2,3,4 ... n^2. If we sum the series (1+2+3...n^2) this becomes (n^2(n^2+1))/2.
So for n^2 iterations of outer loop the inner loop will execute (n^2(n^2+1))/2 times. The most inner loop executes six times for every iteration of the second loop. So by just multiplying (n^2(n^2+1))/2 with 6 it evaluates to 3n^4+3n^2.
To check the answer let's take an example. Say n=5, run your algorithm and print the sum this will give 1950. Now substitute this value in the evaluated expression, this will be like 3(5^4)+3(5^2) and again this evaluates to 1950.
What you need to calculate is this:
S = sum(i in 1..n^2) sum(j in 1..i) sum(k in 1..6) 1
Now, the innermost sum is obviously 6, hence we have
S = sum(i in 1..n^2) sum(j in 1..i) 6
= 6 sum(i in 1..n^2) sum(j in 1..i) 1
The innermost sum is just the sum of the first i numbers, which you should know is i(i + 1)/2, giving
S = 6 sum(i in 1..n^2) i(i + 1)/2
= 3 sum(i in 1..n^2) i(i + 1)
= 3 sum(i in 1..n^2) (i^2 + i)
We can separate this into two sums:
S = 3 [ (sum(i in 1..n^2) i^2) + (sum(i in 1..n^2) i) ]
The second sum there is just our old friend, the sum of the first n^2 numbers, so expanding that is easy.
The first sum there is a new friend, the sum of the first n^2 squares. You can google for that if you don't know it off hand.
Drop in the formulae, expand a little, tidy with a broom, and you should get your answer.
Cheers!

Resources