Big O complexity of inner loops - time-complexity

The Big O of Outer loop is O(n),and I think second inner loop big O is log(n) but I cant't figure the big O of the third inner loop
for(i=0;i<n;i+=2)
{
for(j=1;j<i*i;j*=3)
{
for(k=2;k*k<=n;k++){}
}
}

The outermost loop gets executed n-2 times making the complexity O(n).
In the second loop the value of j increases in powers of 3 up to i*i that is equal to log3(i*i). so for both the loops combined, it is ∑ i=1n-2 log3(i) which is equal to log3(n!) or simply n*log(n) as you mentioned. The innermost loop gets executed sqrt(n) times making the total complexity O(n*log(n)*sqrt(n))

Related

Time complexity with arbitrary number of nested loops

I am just learning the concept of time complexity and for the following nested loop what will be the time complexity?
i:1-n loop
j:N(i) loop
stmt;
end
k:M(i) loop
stmt;
end
end
In the above pseudocode, the main i-loop is executed for n times. The nested loops j and k run for an arbitrary number of times which is stored in an array N and M. N(i) and M(i) are bounded by n-1.
For example, let n =3 and N=[2,1,3] and M=[2,3,3].
Then the inner loops will be executed for 2+1+3 times and 2+3+3 times. How do I formulate time complexity for this? Will saying O(n^2) be correct?
Since you know that N(i) and M(i) are bounded by n-1 for any i, you can draw the conclusion that your inner loops will be bounded by O(n) iterations.
So you get a total time complexity of:
O(n * 2n) = O(n2)
of stmt calls.

Time complexity of these two while loops

x = 1;
while(x<n)
{
x = x + n / 10;
m = n*n
y = n;
while(m>y)
{
m=m-100;
y=y+20;
}
}
The way I solved it: we are adding to x 1/10 of n every time, so no matter how big n will be, the number of repitation we are doing is always 10.
The inner loop is from n to n^2, and each variable in it is increasing linearly, so the inner loop should be O(n)
and because the outer loop is O(1), we get O(n) for all of the function.
but the optional answers for the question are: O(n^2), O(n^3), O(n^4), O(nlogn)
What am I missing? thanks.
You are correct that the outer loop runs a constant number of times (10), but your reasoning about the inner loop isn't all the way there. You say that there is a "linear increase" with each iteration of the inner loop , and if that were the case you would be correct that the whole function runs in O(n), but it's not. Try and figure it out from here, but if you're still stuck:
The inner loop has to make up the difference between n^2 and n in terms of the difference between m and y. That difference between m and y is reduced by a constant amount with each iteration: 120, not a linear amount. Therefore the inner loop runs (n^2 - n)/120 times. Multiplying the number of times the outer loop runs by the times the inner loop runs, we get:
O(10) * O((n^2 - n)/120)
= O(1) * O(n^2)
= O(n^2)

Determine time complexity when inner loop has logarithmic frequency of outer loop

I am novice in analysising time complexity.some one can help me with the time complexity of below algorithm?
public void test(int n)
{
int i=1;
while(i<n)
{
int j=1;
while (j<i)
{
j=j*2;
}
i=i*2;
}
}
outer loop will run log(n) times.How many times the inner loop will run. How can we calulate the frequency of inner
loop in terms of "n" because here it depends on variable "i" and will run log(i) times.
Can someone help to find time complexity of above code.
The time complexity of the given function is O(log n log n) = O(log^2 n).
The outer loop has time complexity O(log n).
Similarly, the inner loop also has time complexity O(log n) because the value of i is bounded above by n. Hence log i = O(log n).
Outer loop will run for (log n ) times.
Since inner loop is bound by i. So it will run for
log1+log2+log3...log (n-1) times for different values of i.
Solving it above can be deduced to
= log(1*2*3...(n-l). (Because log a* log b=log(a*b)
= log((n-1)!). (This is (n-1) factorial)
=(n-1)log(n-1). (because logn!= nlogn)
SO inner loop will have complexity O(nlogn).
So the time complexity of above algo is
logn + nlogn
which is O(nlogn).

Why is the complexity O(log(n^2)*(log(n))

The complexity of this code is O(log(n^2)*log(n), and i don't understand how we arrive to this result.
According to me, the nested while's big O should be just log(n) since its a while loop and we divide j by 4 everytime we enter the loop, and same for the initial while loop with i divided by 2. I especially dont understand which while loop has the O(log^2(n)) complexity
c = 0
i = n * n
while i > 0:
j = n
while j > 0:
c += 1
j = j//4
i = i//2
print c
I seem to be coming up with O(log_4(n)*log_2(n)) as the complexity. First, appreciate that the outer and inner while loops are not correlated. That is to say, the outer loop in i is independent of the inner loop in j. Here are the complexities of the outer and inner loop, in terms of n:
outer loop: O(log_2(n)). This is because the loop starts with n^2, and then decrements the counter by factors of 2, which is therefore log_2 behavior. As #chepner has commented:
O(log_2(n^2)) == 2*O(log_2(n)) = O(log_2(n))
inner loop: O(log_4(n)). This loop begins at n, and decrements the counter by factors of 4, which is log_4 behavior.
Your current guess is almost right, except that you might have missed the bases of the logarithms.

time complexity for loop justification

Hi could anyone explain why the first one is True and second one is False?
First loop , number of times the loop gets executed is k times,
Where for a given n, i takes values 1,2,4,......less than n.
2 ^ k <= n
Or, k <= log(n).
Which implies , k the number of times the first loop gets executed is log(n), that is time complexity here is O(log(n)).
Second loop does not get executed based on p as p is not used in the decision statement of for loop. p does take different values inside the loop, but doesn't influence the decision statement, number of times the p*p gets executed, its time complexity is O(n).
O(logn):
for(i=0;i<n;i=i*c){// Any O(1) expression}
Here, time complexity is O(logn) when the index i is multiplied/divided by a constant value.
In the second case,
for(p=2,i=1,i<n;i++){ p=p*p }
The incremental increase is constant i.e i=i+1, the loop will run n times irrespective of the value of p. Hence the loop alone has a complexity of O(n). Considering naive multiplication p = p*p is an O(n) expression where n is the size of p. Hence the complexity should be O(n^2)
Let me summarize with an example, suppose the value of n is 8 then the possible values of i are 1,2,4,8 as soon as 8 comes look will break. You can see loop run for 3 times i.e. log(n) times as the value of i keeps on increasing by 2X. Hence, True.
For the second part, its is a normal loop which runs for all values of i from 1 to n. And the value of p is increasing be the factor p^2n. So it should be O(p^2n). Thats why it is wrong.
In order to understand why some algorithm is O(log n) it is enough to check what happens when n = 2^k (i.e., we can restrict ourselves to the case where log n happens to be an integer k).
If we inject this into the expression
for(i=1; i<2^k; i=i*2) s+=i;
we see that i will adopt the values 2, 4, 8, 16,..., i.e., 2^1, 2^2, 2^3, 2^4,... until reaching the last one 2^k. In other words, the body of the loop will be evaluated k times. Therefore, if we assume that the body is O(1), we see that the complexity is k*O(1) = O(k) = O(log n).