1

I am having difficulty trying to find the total number of operations in the following code block:

for(int i = 1; i <= n; i *= 2) {
   for(int j = 1; j <= i; j *= 2) {
       // What is the number of operations here, with respect to n?
   }
}

My thinking was that: there are floor(log_2(n)) operations in the outer loop, and log_2(i) operations in the inner loop. I am not sure if my thinking is right, and how I would go from here... How could this be written in terms of solely n?

great_20r
  • 25
  • 5

1 Answers1

0

As you said, there are floor(log_2(n)) operations in the outer loop. For simplicity let's say log_2(n) operations. It won't matter so much for the large number of inputs. In the inner loop, number of operations will be log_2(i) for each case.
So, from i=1 to i=n, number of inner loop operations:

A = log_2(1)+1 (i = 1) + log_2(2)+1 (i = 2) + log_2(4)+1 (i = 4) + ... + log_2(n)+1 (i = n)
A =  1 + 2 + 3 + ... log_2(n) + 1
A = (log_2(n) + 2) * (log_2(n)+1) / 2 
A = ((log_2(n))^2 + 3*log_2(n) + 2)/2

Total number of operations = (log(n)^2 + 3log(n) + 2)/2
In short, time complexity of the algorithm will be : O(log(n)^2)

Muhteva
  • 2,729
  • 2
  • 9
  • 21
  • Thanks for the help! I can see that O(nlogn) is a valid upper bound, but technically isn't the exact bound Θ((log_2(n))^2)? – great_20r Sep 15 '21 at 12:30
  • I would say exact bound is ```Θ(log_2(n!))``` rather than ```Θ((log_2(n))^2)```. Did you mean to say ```Θ(log_2(n!))```? If not, could you explain your reasoning? – Muhteva Sep 15 '21 at 14:38
  • Yup. The outer loop goes from 2^0, 2^1, ..., 2^k ≤ n -> log_2(n) operations. When i = 2^0, j = 2^0; when i = 2^1, j = 2^0, 2^1; when i = 2^2, j = 2^0, 2^1, 2^2; in general, when i = 2^k, j = 2^0, 2^1, ..., 2^k. From this, I noticed the total number of operationss was the summation, T(n) = (1 + 2, + 3 + ... + log_2(n)) = (log_2(n) * log_2(n)+1)/2. I verified this in the code by incrementing a counter variable in the inner loop and it seems to be correct. – great_20r Sep 17 '21 at 23:33
  • And then (log_2(n) * log_2(n) + 1)/2 is roughly (log_2(n))^2, making the exact bound (log_2(n))^2 – great_20r Sep 17 '21 at 23:34
  • I just realized I actually made a small error... The summation actually goes up to log_2(n) + 1, hence the number of operations is (log_2(n) + 1)(log_2(n) + 2)/2 – great_20r Sep 17 '21 at 23:49
  • Yes, you are right. Time complexity will be ```O(log(n)^2)```. I had a mistake in my calculation, now fixed it. – Muhteva Sep 18 '21 at 06:01