Time complexity of iterating over a k-layer deep loop nest always Θ(nᵏ)?

一笑奈何 提交于 2019-12-24 01:44:44

问题


Many algorithms have loops in them that look like this:

for a from 1 to n
  for b from 1 to a
    for c from 1 to b
      for d from 1 to c
        for e from 1 to d
           ...
           // Do O(1) work

In other words, the loop nest is k layers deep, the outer layer loops from 1 to n, and each inner layer loops up from 1 to the index above it. This shows up, for example, in code to iterate over all k-tuples of positions inside an array.

Assuming that k is fixed, is the runtime of this code always Θ(nk)? For the special case where n = 1, the work is Θ(n) because it's just a standard loop over an array, and for the case where n = 2 the work is Θ(n2) because the work done by the inner loop is given by

0 + 1 + 2 + ... + n-1 = n(n-1)/2 = Θ(n2)

Does this pattern continue when k gets large? Or is it just a coincidence?


回答1:


Yes, the time complexity will be Θ(nk). One way to measure the complexity of this code is to look at what values it generates. One particularly useful observation is that these loops will iterate over all possible k-element subsets of the array {1, 2, 3, ..., n} and will spend O(1) time producing each one of them. Therefore, we can say that the runtime is given by the number of such subsets. Given an n-element set, the number of k-element subsets is n choose k, which is equal to

n! / k!(n - k)!

This is given by

n (n-1)(n-2) ... (n - k + 1) / k!

This value is certainly no greater than this one:

n · n · n · ... · n / k! (with k copies of n)

= nk / k!

This expression is O(nk), since the 1/k! term is a fixed constant.

Similarly, when n - k + 1 ≥ n / 2, this expression is greater than or equal to

(n / 2) · (n / 2) · ... · (n / 2) / k! (with k copies of n/2)

= nk / k! 2k

This is Ω(nk), since 1 / k! 2k is a fixed constant.

Since the runtime is O(nk) and Ω(nk), the runtime is Θ(nk).

Hope this helps!




回答2:


You may consume the following equation:

Where c is the number of constant time operations inside the innermost loop, n is the number of elements, and r is the number of nested loops.



来源:https://stackoverflow.com/questions/19719441/time-complexity-of-iterating-over-a-k-layer-deep-loop-nest-always-%ce%98n%e1%b5%8f

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