So, I'm slightly confused by this question on my homework.
for ( int j = 0; j < 2*n; j++){
for ( int k = 0; k < n * n * n; k += 3)
sum++;
}
So I am at this conclusion after a bit of confusion
for( 1, 2n, n)
for( 1/3( 1, 3n, 1)
I have it as 1/3 because it's going up by 3. I'm just not sure if I'm right, we were just introduced to this so I'm sorta lost.
I'm not completely sure that I understand what you are asking... Assuming that the question is what the Big-O notation for this nested loop would be (and assuming that the addition operation is the base operation)
The outer loop is executed 2n times
The inner loop is executed n^3/3 times for each iteration of the outer loop
That means that the inner statement is executed 2n * n^3/3 = (2/3)*n^4. For Big O notation, we ignore the constants, so this nested loop is O(n^4).
Related
My question is about finding the complexity of this algorithm. J value is related to n, so I'm confused about this.
What is the asymptotic complexity of this pseudocode?
for i=1 to n
do
j = 1;
while (j < n)
do
j = j * 2;
Thanks.
I believe it is O(n log2n)
The outer loop is called n times and the inner loop is called log2n times, since in every iteration, j is doubled. For first iteration, i.e., k=0; j is equal to 1 and goes on like 2, 4, 8, ... until 2k>=n
If I add a print at the end of the inner loop, I see:
(1,2,5)
(1,4,5)
(1,8,5)
(2,2,5)
(2,4,5)
(2,8,5)
(3,2,5)
(3,4,5)
(3,8,5)
(4,2,5)
(4,4,5)
(4,8,5)
(5,2,5)
(5,4,5)
(5,8,5)
So it appears O(n^2) but the inner loop appears constant so probably O(n) -- If the (j < n) part is switched to (j < i), that would be closer to O(n log(n)):
(2,2,5)
(3,2,5)
(3,4,5)
(4,2,5)
(4,4,5)
(5,2,5)
(5,4,5)
(5,8,5)
Can't figure out what the big O for this block of code is. Outer loop is log(n), but how bout inner loop that depends on outer loop?
for (int i = 1; i < A.length; i = i * 3) {
for (int j = i; j < A.length; j++) {
// Simple computation
}
}
Any help greatly appreciated :)
If A.Length == n, the inner loop runs in n-i each time. Hence, the total complextiy of the program is sum(n - i) for i = 1, 3, 9, ..., n = 3^log_3(n). As the number of i is log_3(n), the complexity is T(n) = n * log_3(n) - (3^(log_3(n)+1)-1) = n * log_3(n) - 3(n-1) = O(nlog(n))
As is your code will not complete because i = i * 3 means that i will always be 0.
From your comment it is supposed to be i = 1. So, on each iteration of the outer loop, the problem size is reduced by a constant factor of 3. This means that the outer loop takes O(log n) steps.
The inner loop takes O(n) in the worst case and is run O(log n) times. Therefore, the entire running time is O(n log n).
Be sure to check your post for typos and do some research in the future :).
Below code is actually bounded by O(n^2), could anyone please explain why?
for (int i = 1; i < n; i++) {
int j = i;
while (j < n) {
do operation with cost O(j)
j = j * 3;
}
}
It's not that tricky.
Your inner loop's complexity forms a geometric progression with total omplexity O(n).
Without filling the details all the way (this seems like a HW problem), note that the formula for a geometric sequence is
a_0 (q^k - 1) / q - 1, (a_0 = first element, q = multiplication factor, k = num elements).
and your q^k here is O(n).
Your outer loop is O(n).
Since it's a nested loop, and the inner term does not depend on the outer index, you can multiply.
The proof is geometric progression :)
For i = n, the inner loop doesn't execute more than once if i > n/3 (because in next iteration j is >= n).
So for 2n/3 iterations of outer loop the inner loop iterates only once and does O(j) operation. So for 2n/3 iterations the complexity is O(n^2).
To calculate complexity of remaining iterations set n = n/3, now apply steps 1, 2 and 3
Methodically, using Sigma notation, you may do the following:
I'm trying to find the Big-O complexity of the following algorithm:
int i, j;
for (i = 0; i < n; i += 5)
{
for (j = 1; j < n; j *= 3)
{
// O(1) code here
}
}
n is the size of an array passed into the method. Struggling with this due to the i += 5 and j *= 3. I know this is probably wrong but I tried the following...
Outer loop iterates n/5 times. Is that just O(n)?
Inner loop iterates log3(n) times. Must be just log(n).
Since they're nested, multiply the complexities together.
So the Big O complexity is just O(n log(n))?
You can proceed like the following:
Yes you are right. the time complexity is n(log n) -- base 3.
Try taking a very large input value for n and you will understand that the graph for [(n/5)*(log3n)]works identical. Hope this helps.
Basically I am struggling to come to grips with operation counting and Big-O notation. I understand it is possibly one of the harder parts of computer science to understand and I have to admit I am struggling with it. Could anyone give me some help with these examples, and possibly any further help/links with Big-O?
for (i = 0; i < N; i++)
{ for (j = i; j < N; j++)
{ sequence of statements }
}
Here I would say the complexity is O(N²) - Quadratic
int m = -9
for (j = 0; j < n; j+=5)
{
if (j<m)
{
for (k = 1; k <n; k*=3)
{some code}
}
}
Here I would also say is O(N²). The first loop takes N and the second loop takes N so I would say the answer is O(N*N) which is equal to O(N²).
Any help and advice for further understanding would be great!!
The first is indeed O(n^2), as you suspected, assuming the 'sequence of statements' is O(1).
However, the second part of code is O(n), since the condition j < m is never met - and thus, the outer loop only iterates itself without actually doing nothing. The inner loop is not even reachable.
As side note, some compilers may actually optimize the second part of code to run in O(1) by just setting the end values of variables, but this is not the point of the question.
The second example is complexity O(N).
int m = -9
for (j = 0; j < n; j+=5)
{
if (j<m)
{
// this never executes; m is negative and j is positive
}
}
First example:
The inner loop executes N times when i = 0, N-1 times when i = 1, and so on...
You can just calculate the number of steps the for loops execute
(N) + (N - 1) + (N - 2) + ... + 2 + 1
steps = N(N+1)/2 = (N^2 + N) / 2
N <=> 1 |add the left to the right| => N+1
(N - 1) <=> 2 |add the left to the right| => N+1
(N - 2) <=> 3 |add the left to the right| => N+1 . . . N
What does the big-O nation means?
F(N) = O(G(N)) means that |F(N)|<= c*|G(N)| where c > 0
It means that the G(N) function is an upper bound on the grothw rate of the function. F(N) could not grow faster than G(N).
Just to throw this out here: if we assume that the j<-9 clause is a mistake and ignore it, then your second example has two nested loops. However the inner loop is actually multiplying k times 3. So this makes this inner loop O(log n). Which makes the pair of loops O(n log n). I'm not sure this is the answer, but you asked for further understanding, so... you know... maybe that's further.
Ok, also on a further note I would really suggest you to go through a single lecture in the series of introduction of algorithms. Believe me you won't need to look any further.
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-046j-introduction-to-algorithms-sma-5503-fall-2005/video-lectures/