Can someone help me with finding the tight runtime bound for this loop:
for(c4=0, i=1; i<=n; i = 2*i)
for(j=1; j<= i; j++)
c4++;
I am not sure what to do with the 2*i in the outer loop, I think that the inner loop is something like O(i-1)/1 and in terms of n would be O(n-1) because of the while i<=n.
Thanks in advance.
The outer loop will run log(N) times. For each outer loop, the inner loop will run i times. And i goes from 1, , 2, 4, 8, 16, ..., log(N).
So the total is about 2^(log(N)) - 1, it is O(N)
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)
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.
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 :).
s=0
for(i=1; i<n; i = i*2){
if (i<20)
for (j=0; j<n; j++)
{
s=s+i*j;
}
s=s+1
}
I'm trying to establish the big-o complexity for the above algorithm. The outer loop starts at 1 and runs to n, counter in i doubles for each iteration, thus this is a log(n) behaviour. The inner loop runs independently from 0 to n with O(n) behaviour.?
I don't understand how if statement affects complexity. You might not want to provide me with the answer but please guide in the right direction as I don't get it at all.
The inner loop is O(N) but it will only run 5 times,
i.e. when i = 1, 2, 4, 8, 16
After the first 5 iterations, your code basically becomes
for(i=32; i<n; i = i*2){
s=s+1
}
which is O(log(N))
So the total complexity is:
O(5 * N + log(N)) = O(N)
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