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.
Related
int a = 0, i = N;
while (i > 0)
{
a += i;
i /= 2;
}
How will I calculate the time complexity of the code? Can anyone Explain?
Time complexity is basically the number of times a loop will run. Big O is the worst case complexity that a particular loop can have. For example, if linear search were being used to find K, which is, say the (n-1)th element of an array(0 indexed, starts with 0), the program would have to loop through the entire array to find the element. This would mean that the loop has to run n times in the worst case, giving linear search a time complexity of O(n).
In the case of your problem, i is initally equal to N and decrements by half per iteration. This would mean that when (N/pow(2, m) > 0 the loop terminates. So the loop runs at most m times which log(n).
log(N) = log(pow(2,m)) ==> log(N) = m
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).
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))
How to calculate the time complexity of the following algorithm?
for(i=1;i<=n;i++)
for(k=i;k*k<=n;k++)
{
Statements;
}
From what I know, time complexity for nested for loops is equal to the number of times the innermost loop is executed. So here innermost loop is executed n*n times, hence it's O(n^2).
Could it be O(n) depending upon the condition k*k<=n given in the second loop?
Thank you!
Time complexity of an algorithm is always measured in terms of a certain type of operation. For example, if your Statements; have an un unknown time complexity which depends on n, then it would be misleading to describe the time complexity in the first place.
But what you are probably after is to know the time complexity in terms of Statements; operations. If Statements; is a constant-time operation, this becomes especially meaningful. And in this case, what we are looking for is simply to count how many times Statements; are executed. If this number is, say, 3*n, then the time complexity would be O(n).
To answer this question, let us break your nested loop apart. The outer loop iterates from (and including) 1 to n, so it will run exactly n times, regardless of anything.
For each iteration of the outer loop, the inner loop will execute once. It starts from k=i and iterates until k*k > n, or k > sqrt(n). Notice that whenever i > sqrt(n), it will not run at all. We can see that on average, it will run for
O(sqrt(n) + sqrt(n)-1 + sqrt(n)-2 + ... + 0) / n
iterations. By the summation formula you can find here, this equals
O( sqrt(n) * (sqrt(n) + 1) / 2 ) = O( (n + sqrt(n))/2 ) = O( n + sqrt(n) ) = O(n).
So yes, the time complexity in this case is O(n) as you suggested.
You can see this in action by writing a simple script which simulates your algorithm and counts the number of Statements;. Below in JavaScript, so it can be run as a snippet:
// Simulation
function f(n) {
let res = 0;
for(let i=1;i<=n;i++)
for(let k=i;k*k<=n;k++)
++res;
return res;
}
// Estimation
function g(n) {
return ~~((n + Math.sqrt(n))/2);
}
console.log(
f(10),
f(100),
f(1000),
f(10000),
);
console.log(
g(10),
g(100),
g(1000),
g(10000),
);
I hope you found this useful.
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).