Iterating over all subsets of a given size

前端 未结 2 693
耶瑟儿~
耶瑟儿~ 2021-01-13 20:46

I know that iterating over all subsets of a set of size n is a performance nightmare and will take O(2^n) time.

How about iterating over all subsets of size k (for (

2条回答
  •  抹茶落季
    2021-01-13 21:17

    It's easy to show that for a fixed n, (n, k) has a maximum at k = n/2. If I haven't misapplied Sterling's approximation, the asymptotic behavior for (n, n/2) is exponential.

    For constant k, (n, k) is O(n^k). Keep in mind that the combinatorial function is symmetric, so it's the same for (n, n-k). It's polynomial, so it's way smaller than O(n!).

提交回复
热议问题