简体   繁体   English

下一个置换算法中的最佳情况和最坏情况

[英]best case and worst case in next permutation algorithm

Can someone explain why the in next permutation algorithm , we have O(n) as worst case and O(1) as best case?有人可以解释为什么在下一个置换算法中,我们将 O(n) 作为最坏情况,O(1) 作为最好情况吗? An explanation with an example would be appreciated...一个例子的解释将不胜感激......

The algorithm is:算法是:

  1. Find the largest index k such that a[k] < a[k + 1].找到最大的索引 k,使得 a[k] < a[k + 1]。 If no such index exists, the permutation is the last permutation.如果不存在这样的索引,则排列是最后排列。

  2. Find the largest index l greater than k such that a[k] < a[l].找到大于 k 的最大索引 l,使得 a[k] < a[l]。

  3. Swap the value of a[k] with that of a[l].将 a[k] 的值与 a[l] 的值交换。

  4. Reverse the sequence from a[k + 1] up to and including the final element a[n].反转从 a[k + 1] 到并包括最终元素 a[n] 的序列。

I'm new to learning algorithms.我是学习算法的新手。 So, it's a bit difficult for me to "visualize" the worst and best case of this algorithm in my head.因此,在我的脑海中“可视化”这个算法的最坏和最好情况对我来说有点困难。 Thanks.谢谢。

The reason it is in O(n) is that you must find two specific characters.它在 O(n) 中的原因是您必须找到两个特定的字符。 In general, if an algorithm relies on you finding a specific thing out of a list of n things the algorithm will be in O(n) but will be constant time in the best case.通常,如果算法依赖于您从 n 个事物的列表中找到特定事物,则该算法将在 O(n) 中,但在最佳情况下将是恒定时间。 To help with visualizing it let's trace the algorithm on a couple strings.为了帮助可视化它,让我们在几个字符串上跟踪算法。

Worst Case最差的情况

Consider finding the next permutation given the string考虑找到给定字符串的下一个排列

a = 6 7 5 4 3 2 1 a = 6 7 5 4 3 2 1

We must first find the rightmost character that is less than the character right after it.我们必须首先找到最右边的字符,它小于紧随其后的字符。 To do this we can search backwards through the string:为此,我们可以通过字符串向后搜索:

6 7 5 4 3 2 1 6 7 5 4 3 2 1

6 7 5 4 3 2 1 6 7 5 4 3 2 1

6 7 5 4 3 2 1 6 7 5 4 3 2 1

6 7 5 4 3 2 1 6 7 5 4 3 2 1

6 7 5 4 3 2 1 6 7 5 4 3 2 1

6 7 5 4 3 2 1 Found it! 6 7 5 4 3 2 1 找到了!

That took 6 comparisons.这需要进行 6 次比较。 Let's call the left index k, giving us k=0.让我们称左索引为 k,给我们 k=0。 Now you have to find the rightmost character that is greater than k.现在你必须找到最右边的大于 k 的字符。 We can again do this by looking backwards through the list.我们可以通过向后查看列表来再次做到这一点。

6 7 5 4 3 2 1 6 7 5 4 3 2 1

6 7 5 4 3 2 1 6 7 5 4 3 2 1

6 7 5 4 3 2 1 6 7 5 4 3 2 1

6 7 5 4 3 2 1 6 7 5 4 3 2 1

6 7 5 4 3 2 1 6 7 5 4 3 2 1

6 7 5 4 3 2 1 Found it! 6 7 5 4 3 2 1 找到了!

That took 6 comparisons.这需要进行 6 次比较。 Let's call that index l, giving us k=0 and l=1.我们称该指数为 l,即 k=0 和 l=1。 Now, swap the two values.现在,交换两个值。

6 7 5 4 3 2 1 --> 7 6 5 4 3 2 1 6 7 5 4 3 2 1 --> 7 6 5 4 3 2 1

And now reverse the sequence from k+1 up to the end of the list.现在反转从 k+1 到列表末尾的序列。 I am not going to write this out because I do not think it is particularly important to understanding the algorithm, but not that this is not a constant time operation.我不打算写出来,因为我认为理解算法并不是特别重要,但并不是说这不是一个常数时间操作。 If you are using an array, reversing the elements would be linear.如果您使用的是数组,则反转元素将是线性的。 This does not change the efficiency class.这不会改变效率等级。

7 6 5 4 3 2 1 --> 7 1 2 3 4 5 6 7 6 5 4 3 2 1 --> 7 1 2 3 4 5 6

And you have the next permutation!你有下一个排列! I don't think it is hard to see that this is the worst case scenario.我不认为很难看出这是最坏的情况。 This took 6 + 6 = 12 comparisons, which is equal to 2 * (length(a) - 1).这需要 6 + 6 = 12 次比较,等于 2 * (length(a) - 1)。 If we expand this we get 2 * length(a) - 2. Both of the 2's can be dropped because we don't care about constant addition or multiplication in big-O and we are left length(a), putting the algorithm in O(n).如果我们扩展它,我们会得到 2 * length(a) - 2。这两个 2 都可以去掉,因为我们不关心 big-O 中的常量加法或乘法,我们剩下的是 length(a),把算法放在在)。


Best Case最佳案例

Now let's look at the (much shorter) best case.现在让我们看一下(更短的)最佳情况。

a = 1 2 3 4 5 6 7 a = 1 2 3 4 5 6 7

Find k求k

1 2 3 4 5 6 7 Found it! 1 2 3 4 5 6 7找到了!

Find l查找 l

1 2 3 4 5 6 7 Found it! 1 2 3 4 5 6 7找到了!

Swap k and l交换 k 和 l

1 2 3 4 5 6 7 --> 1 2 3 4 5 7 6 1 2 3 4 5 6 7 --> 1 2 3 4 5 7 6

And reverse all of the characters after k.并反转k后的所有字符。

1 2 3 4 5 7 6 --> 1 2 3 4 5 7 6 1 2 3 4 5 7 6 --> 1 2 3 4 5 7 6

In this case we only needed to do a single comparison to find k and a single comparison to find l.在这种情况下,我们只需要做一次比较就可以找到 k 和一次比较来找到 l。 Reversing the elements after k was also a constant time operation because there was only a single element to reverse.反转 k 之后的元素也是一个恒定时间操作,因为只有一个元素需要反转。 This would be true whether the string was 7 characters long or if it was 100,000,000 characters long.无论字符串长度为 7 个字符还是长度为 100,000,000 个字符,这都是正确的。 Finding this permutation had nothing to do with the length of the string, making the best case O(1).找到这个排列与字符串的长度无关,最好的情况是 O(1)。

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

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