顶点覆盖问题java_java-O(n!)的示例?

java-O(n!)的示例?

O(n!)函数的一个示例(在代码中)是什么? 参考n,应该执行适当数量的操作; 也就是说,我在问时间复杂度。

17个解决方案

81 votes

妳去 这可能是在O(n!)时间(其中n是该函数的参数)运行的函数的最简单的例子:

void nFacRuntimeFunc(int n) {

for(int i=0; i

nFacRuntimeFunc(n-1);

}

}

sepp2k answered 2019-12-24T22:09:07Z

38 votes

一个典型的例子是通过蛮力搜索的旅行商问题。

如果有N个城市,则蛮力方法将尝试使用这N个城市中的每个排列,以找出最便宜的城市。 现在,具有N个城市的排列数为N!,这使其复杂度为因子(O(N!))。

codaddict answered 2019-12-24T22:09:32Z

8 votes

请参阅Big O Wikipedia文章的通用功能顺序部分。

根据文章,通过蛮力搜索解决旅行商问题并找到未成年人扩张的行列式都是O(n!)。

Bill the Lizard answered 2019-12-24T22:09:57Z

6 votes

存在问题NP-complete(可在不确定的多项式时间内验证)。 这意味着,如果输入扩展,那么解决该问题所需的计算量就会增加很多。

一些NP-complete问题是:哈密顿路径问题(img),旅行商问题(img)

一些NP-complete问题是:布尔可满足性问题(星期六)(打开img),N难题(打开img),背包问题(打开img),子图同构问题(打开img),子集和问题(打开img),派系问题 (open img),顶点覆盖问题(open img),独立集合问题(open img),支配集合问题(open img),图形着色问题(open img),

来源:链接1,链接2

1hU5U.gif

资料来源:链接

Margus answered 2019-12-24T22:10:35Z

5 votes

寻找未成年人扩张的决定因素。

这里很好的解释。

# include

# include

bool det_by_minor()

{ bool ok = true;

// dimension of the matrix

size_t n = 3;

// construct the determinat object

CppAD::det_by_minor Det(n);

double a[] = {

1., 2., 3., // a[0] a[1] a[2]

3., 2., 1., // a[3] a[4] a[5]

2., 1., 2. // a[6] a[7] a[8]

};

CPPAD_TEST_VECTOR A(9);

size_t i;

for(i = 0; i < 9; i++)

A[i] = a[i];

// evaluate the determinant

double det = Det(A);

double check;

check = a[0]*(a[4]*a[8] - a[5]*a[7])

- a[1]*(a[3]*a[8] - a[5]*a[6])

+ a[2]*(a[3]*a[7] - a[4]*a[6]);

ok = det == check;

return ok;

}

从这里的代码。 您还会在此处找到必要的.hpp文件。

Jungle Hunter answered 2019-12-24T22:11:04Z

5 votes

我想我有点晚了,但是我发现snailsort是O(n!)确定性算法的最佳示例。 它基本上找到数组的下一个排列,直到对其进行排序。

看起来像这样:

template

void snail_sort(Iter first, Iter last)

{

while (next_permutation(first, last)) {}

}

Gabi Purcaru answered 2019-12-24T22:11:28Z

5 votes

计算给定数组的所有置换的任何算法都是O(N!)。

John answered 2019-12-24T22:11:48Z

4 votes

最简单的例子:)

伪代码:

input N

calculate N! and store the value in a vaiable NFac - this operation is o(N)

loop from 1 to NFac and output the letter 'z' - this is O(N!)

你去了:)

作为一个真实的例子-生成一组项目的所有排列怎么样?

Armen Tsirunyan answered 2019-12-24T22:12:21Z

3 votes

printf("Hello World");

是的,这是O(n!)。 如果您认为不是,建议您阅读BigOh的定义。

我之所以仅添加此答案,是因为人们不得不不管自己的实际意思而总是使用BigOh的烦人习惯。

例如,我很确定这个问题打算问Theta(n!),至少是cn! 步骤,不超过Cn! 某些常数c的步长,C> 0,但是选择使用O(n!)。

另一个实例:Quicksort is O(n^2) in the worst case,虽然在技术上是正确的(在最坏的情况下,甚至堆排序都为O(n ^ 2)!),但它们的实际含义是Quicksort is Omega(n^2) in the worst case。

Aryabhatta answered 2019-12-24T22:12:59Z

2 votes

在维基百科

通过蛮力搜索解决旅行商问题; 寻找未成年人扩张的决定因素。

[http://zh.wikipedia.org/wiki/Big_O_notation#Orders_of_common_functions]

nopole answered 2019-12-24T22:13:28Z

1 votes

在C#中

在空间复杂度上这不是O(N!)吗? 因为,C#中的字符串是不可变的。

string reverseString(string orgString) {

string reversedString = String.Empty;

for (int i = 0; i < orgString.Length; i++) {

reversedString += orgString[i];

}

return reversedString;

}

user623429 answered 2019-12-24T22:13:52Z

1 votes

您是对的,递归调用应该恰好为n! 时间。 这是一个代码,用于测试n个不同值的阶乘时间。 内循环运行n! j的不同值的时间,因此内部循环的复杂度为Big O(n!)

public static void NFactorialRuntime(int n)

{

Console.WriteLine(" N Fn N!");

for (int i = 1; i <= n; i++) // This loop is just to test n different values

{

int f = Fact(i);

for (int j = 1; j <= f; j++) // This is Factorial times

{ ++x; }

Console.WriteLine(" {0} {1} {2}", i, x, f);

x = 0;

}

}

这是n = 5的测试结果,它会精确迭代阶乘时间。

N Fn N!

1 1 1

2 2 2

3 6 6

4 24 24

5 120 120

具有时间复杂度的精确函数n!

// Big O(n!)

public static void NFactorialRuntime(int n)

{

for (int j = 1; j <= Fact(i); j++) { ++x; }

Console.WriteLine(" {0} {1} {2}", i, x, f);

}

techdips answered 2019-12-24T22:14:21Z

0 votes

Bogosort是我遇到的唯一进入O(n!)领域的“官方”人员。 但这不是保证的O(n!),因为它本质上是随机的。

MdaG answered 2019-12-24T22:14:41Z

0 votes

您可能学习的关于矩阵行列式的递归方法(如果使用线性代数)需要O(n!)时间。 虽然我不是特别想将其编码。

user477556 answered 2019-12-24T22:15:01Z

0 votes

@clocksmith你是完全正确的。 这不是在计算n!。 也不是O(n!)。 我运行它收集了下表中的数据。 请比较第二栏和第三栏。 (#nF是对nFacRuntimeFunc的调用次数)

n #nF n!

0 0 1

1 1 1

2 4 2

3 15 6

4 65 24

5 325 120

6 1956 720

7 13699 5040

显然,if的性能比O(n!)差得多。 以下是用于计算n!的示例代码! 递归地 您会注意到它的O(n)阶数。

int Factorial(int n)

{

if (n == 1)

return 1;

else

return n * Factorial(n-1);

}

user3416507 answered 2019-12-24T22:15:31Z

0 votes

加起来k功能

这是一个复杂度为O(n!)的函数的简单示例,给定参数数组int和整数k。 如果数组x + y = k中有两项,则返回true,例如:如果tab为[1、2、3、4]且k = 6,则返回值为true,因为2 + 4 = 6

public boolean addToUpK(int[] tab, int k) {

boolean response = false;

for(int i=0; i

for(int j=i+1; j

if(tab[i]+tab[j]==k) {

return true;

}

}

}

return response;

}

另外,这是使用jUnit进行的单元测试,可以正常工作

@Test

public void testAddToUpK() {

DailyCodingProblem daProblem = new DailyCodingProblemImpl();

int tab[] = {10, 15, 3, 7};

int k = 17;

boolean result = true; //expected result because 10+7=17

assertTrue("expected value is true", daProblem.addToUpK(tab, k) == result);

k = 50;

result = false; //expected value because there's any two numbers from the list add up to 50

assertTrue("expected value is false", daProblem.addToUpK(tab, k) == result);

}

Achraf Bellaali answered 2019-12-24T22:16:00Z

0 votes

最简单的例子是阶乘函数:

function factorial(n){

let fact=1;

for(var i=1; i<=n;i++){

fact=fact*i;

}

return fact;

}

上!)

Mayank Tolani answered 2019-12-24T22:16:24Z

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值