Time Complexity of finding all partitions of a set

99封情书 提交于 2021-02-11 12:46:41

问题


We know that this problem is np-complete, hence, no polynomial algorithm can be found for it. Also, we know that number of all partitions of a set is equal to the bell's number. I see there are few algorithms to generate all partitions of a set but couldn't find what is the time complexity to solve this problem.

For example, this python code generates all partitions of a set recursively. What is the time complexity of this algorithm? Could this problem be solved with better time complexity?

def partition(collection):
    global counter
    if len(collection) == 1:
        yield [collection]
        return
    first = collection[0]
    for smaller in partition(collection[1:]):
        for n, subset in enumerate(smaller):
            yield smaller[:n] + [[first] + subset] + smaller[n + 1:]
        yield [[first]] + smaller

回答1:


First off, note that this problem is not NP-complete. NP is a class of decision problems, which this problem is not. Often the distinction is useless semantics, but in this case there is no clear roughly equivalent decision problem. Furthermore, for a problem to be in NP the answer has to be verifiable in polynomial time, and to be NP-hard an O(1) oracle for the problem must allow other problems in NP to be solved in polynomial time. Neither of these is the case.

That having been said, you rightly say that the number of partitions of a set of size n is equal to the n'th Bell number. Now, because your code requires no search for partitions, it gives a partition "every step", and thus runs in time proportional to the Bell number. Technically there is also a polynomial term as larger partitions take longer to generate, but this term will be dominated.

What, then, is the big-o for the Bell numbers? This question turns out to be somewhat difficult to answer. Wikipedia mentions some upper bounds for Bell numbers, one of them found in 2010 ((0.792n / ln(n+1))^n), but there does not appear to be a tight bound proven.



来源:https://stackoverflow.com/questions/65845727/time-complexity-of-finding-all-partitions-of-a-set

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