简体   繁体   English

当时间复杂度为O(n!)和O(2 ^ n)时?

[英]When time complexity is O(n!) and O(2^n)?

I am practicing algorithm but always confused about time complexity between O(n!) and O(2^n). 我正在练习算法,但始终对O(n!)和O(2 ^ n)之间的时间复杂度感到困惑。 I know O(2^n) can be understood like select or not select an item from a set, which usually likes recursion (or backtracking?) situation. 我知道O(2 ^ n)可以理解为选择或不选择集合中的一项,通常喜欢递归(或回溯?)情况。 But backtrack problems sometimes also marked with O(n!)? 但是回溯问题有时也标记为O(n!)?

Can somebody please explain them in simple language? 有人可以用简单的语言解释一下吗?

2^N: Inclusion/exclusion of each element. 2 ^ N:包含/排除每个元素。 Order does not matter. 顺序无所谓。

N!: Iterate over each permutation of elements. N !:遍历元素的每个排列。 Order does matter. 顺序很重要。

The Big O notation does not produce the exact results but rather estimates of growth of functions by specifying some upper bound function. Big O表示法不会产生确切的结果,而是通过指定一些上限函数来估计函数的增长。 To represent the most relevant families of such functions there are commonly used terms as logarithmic, linear, polymorphic and expotentional. 为了表示此类功能的最相关的族,通常使用对数,线性,多态和指数形式的术语。 Both O(n!) and O(2^n) fall into the category of expotentional growth. O(n!)和O(2 ^ n)都属于指数增长类别。 Just O(n!) grows slightly faster. 只是O(n!)增长更快。

So as the takeaway conclusion Big O allows some sloppiness and occasionally there are functions that match closer the upper bound but different authors may use either term referring to the same algorithm. 因此,得出的结论是,Big O允许有些草率,并且偶尔会有一些函数更接近上限,但不同的作者可能会使用任一个术语来引用相同的算法。

As it comes to backtracking, the choice of the solutions in every step may be limited by the choice done in the previous step so the number of possible choices in every step is reduced by one following the factorial pattern. 关于回溯,每个步骤中解决方案的选择可能会受到前一步中所做选择的限制,因此,按照阶乘模式,每个步骤中可能的选择数量都会减少一个。 But not all cases of backtracking fall into this category and some may involve complexity O(2^n) or even O(n^n) if the choice of solutions in every step is independent from previous steps. 但是,并非所有回溯的情况都属于此类,并且如果每个步骤中解决方案的选择均与先前步骤无关,则可能涉及复杂度O(2 ^ n)甚至O(n ^ n)。

EDIT: Corrected growth relations between O(2^n), O(n!) and O(n^n) 编辑:校正了O(2 ^ n),O(n!)和O(n ^ n)之间的增长关系

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM