two loops but Theta(n)?

心已入冬 提交于 2019-12-24 01:43:28

问题


i = n
while (i >= 1)
{
    for j = 1 to i 
    {
    Function() <- (O(1))
    }
i = i/2
}

The answer is Theta(n) but I do not understand why this is Theta(n).

From my understanding, the inner loop will execute n + n/2 + n/4 +...+1 so the total will be O(n) and the outer loop will be executed logn time so the answer should be nlogn. But why this is Theta(n) instead of Theta(nlogn)?


回答1:


Your calculations are partially correct. You are simply missing out on a small detail in which you have already calculated the outer loop's complexity. In your case, you chose to calculate time complexity by simply calculating the time each iteration in the outer loop takes. And that is:

for i=n : O(n)

for i=n/2: O(n/2)

.
.
.

for i=1: O(1)

Now, the calculations for each iteration where made based on the time complexity of the inner loop. Therefore, the total time complexity is how much the outer loop takes (which includes the time the inner loop takes) which is the sum of how much it takes to run each iteration which is: n+n/2+n/4+...+1.

The multiplication you were referring to is a technique used when you know how many times the outer loop runs and how many times the inner loop runs and you multiply them to get the overall time complexity. You could have alternatively, added the number of times the inner loop runs to itself n times whereas n is the number of times the outer loop runs (simple mathematics: k*n = k+k+...+k n times).

So in your case, you're simply using the second method, since n+n/2+n/4+...+1 is not the number of times the inner loop runs, it's the sum of how many times it runs in each iteration of the outer loop.




回答2:


As you correctly noted, the inner loop will be executed n + n/2 + n/4 + ... + 1 ≈ 2*n times.

Let's see how many times each line of code will be executed.

i = n                  // Executed 1 time
while (i >= 1)         // Executed approximately log(n) times
{
    for j = 1 to i     // Executed approximately 2*n times
    {
        Function()     // Executed approximately 2*n times
    }
    i = i/2            // Executed approximately log(n) times
}

So the total time complexity of the algorithm is:

Θ(1) + Θ(log(n)) + Θ(n) + Θ(n) + Θ(log(n))

Which is equivalent to Θ(n).




回答3:


From my understanding, the inner loop will execute n + n/2 + n/4 +...+1 so the total will be O(n)

That's all you need. You've already taken into account the multiplying effect that the outer loop has on the number of times the inner loop runs. The fact that the outer loop's complexity itself is O(n) can be added to the inner loop's total complexity. The total is not n * log n, it's n + log n, which is O(n).




回答4:


You're right about the total inner loop executing n + n/2 + n/4 + ... + 1 which is O(n). You don't have to multiply the inner loop's runtime by O(log(n)), since while the outer loop has a runtime of O(log(n)), the inner loop's runtime also decreases by log(n) as you divide i by 2, thus the runtime is simply just O(n)




回答5:


The sum of n + n/2 + n/4 ... + 1 is always smaller or equal than 2*n, because the geometric series for q = 0.5 converges to this.

Therefore, the function will never be called more than 2n times so the complexity is Θ(n).



来源:https://stackoverflow.com/questions/41708311/two-loops-but-thetan

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!