O(n!) 的示例?
O(n!)
函数的示例(代码)是什么?参考 n
应该需要适当数量的操作来运行;也就是说,我问的是时间复杂度。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
O(n!)
函数的示例(代码)是什么?参考 n
应该需要适当数量的操作来运行;也就是说,我问的是时间复杂度。
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(16)
就这样吧。这可能是在
O(n!)
时间内运行的函数的最简单的示例(其中n
是函数的参数):There you go. This is probably the most trivial example of a function that runs in
O(n!)
time (wheren
is the argument to the function):一个典型的例子是通过暴力搜索解决旅行推销员问题。
如果有
N
个城市,暴力法将尝试这N
个城市的每一个排列,以找出哪一个最便宜。现在,N
个城市的排列数量为N!
,使其复杂性阶乘 (O(N!)
)。One classic example is the traveling salesman problem through brute-force search.
If there are
N
cities, the brute force method will try each and every permutation of theseN
cities to find which one is cheapest. Now the number of permutations withN
cities isN!
making it's complexity factorial (O(N!)
).请参阅常用函数的顺序部分.wikipedia.org/wiki/Big_O_notation" rel="noreferrer">Big O 维基百科文章。
根据该文章,通过暴力搜索解决旅行推销员问题并找到行列式 与 未成年人的扩展都是 O(n!)。
See the Orders of common functions section of the Big O Wikipedia article.
According to the article, solving the traveling salesman problem via brute-force search and finding the determinant with expansion by minors are both O(n!).
任何计算给定数组的所有排列的算法都是
O(N!)
。Any algorithm that calculates all permutation of a given array is
O(N!)
.有一些问题,
NP-complete
(可在非确定性多项式时间内验证)。这意味着如果输入规模扩大,那么解决问题所需的计算量就会增加很多。一些
NP-hard
问题是:哈密尔顿路径问题(打开图片), 旅行推销员问题( open图片)一些
NP完全
问题是:布尔可满足性问题(周六)(打开图片),N-puzzle(打开img),背包问题(打开img ),子图同构问题(打开img), 子集和问题( 打开 img ), 派系问题(< a href="https://i.sstatic.net/SrtfF.png" rel="nofollow noreferrer"> 打开 img ), 顶点覆盖问题(打开img ) , 独立集问题( 打开 img ), 支配集问题(打开图片),图形着色问题( open img < /a>),来源:链接 1,链接 2
来源:链接
There are problems, that are
NP-complete
(verifiable in nondeterministic polynomial time). Meaning if input scales, then your computation needed to solve the problem increases more then a lot.Some
NP-hard
problems are: Hamiltonian path problem( open img ), Travelling salesman problem( open img )Some
NP-complete
problems are: Boolean satisfiability problem (Sat.)( open img ), N-puzzle( open img ), Knapsack problem( open img ), Subgraph isomorphism problem( open img ), Subset sum problem( open img ), Clique problem( open img ), Vertex cover problem( open img ), Independent set problem( open img ), Dominating set problem( open img ), Graph coloring problem( open img ),Source: link 1, link 2
Source: link
我想我有点晚了,但我发现 snailsort 是O(n!) 确定性算法的最佳示例。它基本上找到数组的下一个排列,直到对其进行排序。
它看起来像这样:
I think I'm a bit late, but I find snailsort to be the best example of O(n!) deterministic algorithm. It basically finds the next permutation of an array until it sorts it.
It looks like this:
寻找未成年人扩张的行列式。
这里有很好的解释。
代码来自此处。您还可以在那里找到必要的
.hpp
文件 。Finding the determinant with expansion by minors.
Very good explanation here.
Code from here. You will also find the necessary
.hpp
files there.最简单的例子 :)
伪代码:
就这样 :)
作为一个真实的例子 - 生成一组项目的所有排列怎么样?
the simplest example :)
pseudocode:
there you go :)
As a real example - what about generating all the permutations of a set of items?
在维基百科中
通过暴力搜索解决旅行推销员问题;寻找未成年人扩张的行列式。
http://en.wikipedia.org/wiki/Big_O_notation#Orders_of_common_functions
In Wikipedia
Solving the traveling salesman problem via brute-force search; finding the determinant with expansion by minors.
http://en.wikipedia.org/wiki/Big_O_notation#Orders_of_common_functions
printf("Hello World");
是的,这是 O(n!)。如果你认为不是,我建议你阅读BigOh的定义。
我添加这个答案只是因为人们必须始终使用 BigOh 的恼人习惯,而不管它们的实际含义是什么。
例如,我很确定这个问题是想问 Theta(n!),至少是 cn!步数不超过Cn!对于一些常数c,C>的步骤0,但选择使用 O(n!) 代替。
另一个例子:
快速排序在最坏情况下是 O(n^2)
,虽然技术上是正确的(即使是堆排序在最坏情况下也是 O(n^2)!),但它们实际上的意思是在最坏的情况下,快速排序是 Omega(n^2)
。printf("Hello World");
Yes, this is O(n!). If you think it is not, I suggest you read the definition of BigOh.
I only added this answer because of the annoying habit people have to always use BigOh irrespective of what they actually mean.
For instance, I am pretty sure the question intended to ask Theta(n!), at least cn! steps and no more than Cn! steps for some constants c, C > 0, but chose to use O(n!) instead.
Another instance:
Quicksort is O(n^2) in the worst case
, while technically correct (Even heapsort is O(n^2) in the worst case!), what they actually mean isQuicksort is Omega(n^2) in the worst case
.在 C# 中,
空间复杂度不是 O(N!) 吗?因为,C# 中的字符串是不可变的。
In C#
Wouldn't this be O(N!) in space complexity? because, string in C# is immutable.
你是对的,递归调用应该恰好n!时间。这是一个类似测试 n 个不同值的阶乘时间的代码。内循环运行 n!不同的 j 值的时间,因此内循环的复杂度是 Big O(n!)
以下是 n = 5 的测试结果,它迭代精确的阶乘时间。
时间复杂度为 n! 的精确函数
You are right the recursive calls should take exactly n! time. here is a code like to test factorial time for n different values. Inner loop runs for n! time for different values of j, so the complexity of inner loop is Big O(n!)
Here are the test result for n = 5, it iterate exactly factorial time.
Exact function with time complexity n!
Bogosort 是我遇到的唯一一个冒险进入 O(n!)区域。但它并不能保证 O(n!),因为它本质上是随机的。
Bogosort is the only "official" one I've encountered that ventures into the O(n!) area. But it's not a guaranteed O(n!) as it's random in nature.
您可能学到的用于求矩阵行列式的递归方法(如果您采用线性代数)需要 O(n!) 时间。尽管我不太想把这些全部编码出来。
The recursive method you probably learned for taking the determinant of a matrix (if you took linear algebra) takes O(n!) time. Though I dont particularly feel like coding that all up.
Add to up k 函数
这是一个复杂度为 O(n!) 的函数的简单示例,给定参数中的 int 数组和整数 k。如果数组 x+y = k 中有两项,则返回 true,例如:如果 tab 为 [1, 2, 3, 4] 且 k=6,则返回值将为 true,因为 2+4=6
作为额外的,这是一个使用 jUnit 的单元测试,它工作得很好
Add to up k function
This is a simple example of a function with complexity O(n!) given an array of int in parameter and an integer k. it returns true if there are two items from the array x+y = k , For example : if tab was [1, 2, 3, 4] and k=6 the returned value would be true because 2+4=6
As a bonus this is a unit test with jUnit, it works fine
在 JavaScript 中:
对于 Node 8.5+,您需要首先包含 perf_hooks 模块的性能。谢谢。
In JavaScript:
for node 8.5+, you need to first include performance from the perf_hooks module. Thank you.