Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time complexity for dependant nested for loop?

Can you explain me how to find time complexity for this?

sum=0;
for(k=1;k<=n;k*=2)
  for(j=1;j<=k;j++)
     sum++;

So, i know the outer loop has time complexity of O(logn), but since the iterations of the inner loop depends on the value of the outer loop, the complexity of this algorithm is not O(nlogn).

The book says it is O(n).

I really dont understand how it is O(n)...Can someone please explain it... I'll be really grateful if u could go into the details btw :D

A mathematical solution would help me understand better...

like image 828
user2228135 Avatar asked Feb 06 '14 11:02

user2228135


People also ask

What is time complexity of nested for loop?

Every time the outer loop executes, the inner loop executes M times. As a result, the statements in the inner loop execute a total of N * M times. Thus, the total complexity for the two loops is O(N2).

What is the time complexity of 3 nested for loop?

+n iterations n*(n+1)/2 which is O(n2) .

What is the time complexity for for loop?

The loop executes N times, so the sequence of statements also executes N times. Since we assume the statements are O(1), the total time for the for loop is N * O(1), which is O(N) overall.


2 Answers

Just see how many times the inner loop runs:

1 + 2 + 4 + 8 + 16 +...+ n

Note that if n = 32, then this sum = 31 + 32. ~ 2n.
This is because the sum of all the terms except the last term is almost equal to the last term.

Hence the overall complexity = O(n).

EDIT:

The geometric series sum (http://www.mathsisfun.com/algebra/sequences-sums-geometric.html) is of the order of:

(2^(logn) - 1)/(2-1) = n-1.
like image 179
Abhishek Bansal Avatar answered Oct 13 '22 21:10

Abhishek Bansal


The outer loop executed log(Base2)n times.so it is O(log(Base2)n).

the inner loop executed k times for each iteration of the outer loop.now in each iteration of the outer loop, k gets incremented to k*2.

so total number of inner loop iterations=1+2+4+8+...+2^(log(Base2)n)
=2^0+2^1+2^2+...+2^log(Base2)n (geometric series)
=2^((log(base2)n+1)-1/(2-1)
=2n-1.
=O(n)
so the inner loop is O(n). So total time complexity=O(n), as O(n+log(base2)n)=O(n).

UPDATE:It is also O(nlogn) because nlogn>>n for large value of n , but it is not asymptotically tight. you can say it is o(nlogn)[Small o] .

like image 33
tanmoy Avatar answered Oct 13 '22 21:10

tanmoy