【问题标题】:Computing all n-sized permutations without repetitions and without "classic" ordering计算所有不重复且不使用“经典”排序的 n 大小排列
【发布时间】:2011-12-26 22:56:22
【问题描述】:

好的,伙计们,我需要为摄影比赛实现这个...我有一组主要的 N 张图片,我需要生成这些图片的大小为 2 的排列而不重复,例如:

foo.png VS bar.png

等于

bar.png VS foo.png

另一件事,我不能每次都预先生成所有排列,所以我需要一个函数,在给定前一个排列的情况下,将返回下一个排列(如果可能的唯一排列结束,则返回 NULL)。

我用下面的 PHP 函数解决了这个问题:

function getNextPermutation( $aPermutableItems, $iPermutationSize, $aPreviousPermutation = NULL )
{
  $aNextPermutation = $aPreviousPermutation;
  $iLastIndex       = $iPermutationSize - 1;
  $iPermutableItems = count($aPermutableItems);

  // Any previous permutation ?
  if( $aPreviousPermutation )
  {
    // Loop the elements backwards
    for( $i = $iLastIndex; $i >= 0; $i-- )
    {
      // Can the current element be incremented without reaching the limit ?
      if( ++$aNextPermutation[ $i ] >= $iPermutableItems )
      {
        // Increment the previous element
        $iPrevValue = ++$aNextPermutation[ $i - 1 ];
        // Reset the current element with the value of the previous plus one
        $iNextValue = $aNextPermutation[ $i ] = $iPrevValue + 1;
        // Skip the previous element because it was just incremented
        $i--;
        // If one of the two elements reached the limit, we are in the exit condition
        if( $iPrevValue >= $iPermutableItems || $iNextValue >= $iPermutableItems )
          return FALSE;
      }
      // Limit still to be reached for the i-th element, skip previous ones
      else
        break;
    }
  }
  // Am i able to generate the first permutation ?
  else if( $iPermutationSize <= $iPermutableItems )
    $aNextPermutation = range( 0, $iLastIndex );
  // Permutation impossible to generate because we don't have enough elements in the main set
  else
    $aNextPermutation = FALSE;

  return $aNextPermutation;
}

这样:

$iPerm  = 0;
$aPrev  = NULL;
$aItems = array( 0, 1, 2, 3, 4 );

while( ($aPrev = getNextPermutation( $aItems, 2, $aPrev )) != FALSE )
{
  echo sprintf( "%2d : %s\n", $iPerm++, implode( ', ',  $aPrev ) ); 
}

将输出:

0 : 0, 1
1 : 0, 2
2 : 0, 3
3 : 0, 4
4 : 1, 2
5 : 1, 3
6 : 1, 4
7 : 2, 3
8 : 2, 4
9 : 3, 4

现在,我真的很想给它添加一些熵...我的意思是,如您所见,组合中的第一项经常重复( 0,1 0,2 0,3 ),并且就我而言,这并不好,因为我会看到 4 个连续排列的相同图片。

有没有办法修改(或重新实现)我的算法以具有类似(例如)的东西:

0 : 0, 1
1 : 1, 2
2 : 0, 3
3 : 3, 4
4 : 0, 2
5 : 1, 3
6 : 0, 4
7 : 2, 3
8 : 2, 4
9 : 1, 4

显然,我不能只打乱排列数组,因为正如我所写,我没有整个排列数组,而只有前一个排列,一旦应用我的算法就会给我下一个排列。

PS:每个挑战我有大约 500 张照片,因此存储位掩码或类似的东西是不可接受的。

谢谢。

【问题讨论】:

    标签: php algorithm unique permutation


    【解决方案1】:

    如果您想以未排序的方式对大小 k 进行一些排列,您可以拥有一个以 2 为底的 n 位计数器,并每次打印下一个值(具有 k1),例如计数器大小为 3(并且 k=2):

    000
    001
    010
    011
    100
    101
    110
    

    因此,您的输出将是 011、101 和 110(实际上将转换为 (1,2),(3,1),(3,2)),这不是您想要的,而是当计数器大小时增长,它会更明智,但使用这样的计数器很耗时,但如果你的图片尺寸小于 20,它就足够快了(因为 2^20 = 100 万并不大)。此外,通过获得像 100 这样的数字,您可以简单地用这个来启动您的计数器并获得下一个值。这也可以简单地扩展以生成大小为 k 的排列。

    【讨论】:

    • 不幸的是,我需要超过 2 个元素(或者至少可以使用 $iPermutationSize 参数配置算法),在您的示例中,第一个数字 (0) 在多个连续排列中重复([0 ]00, [0]01, [0]10, ...) ... 无论如何,感谢您的努力:)
    • @Simone Margaritelli,正如我提到的,它可以简单地配置为k,只接受带有k1 的数字,而且我从来没有说过000,001,010,100对于k=2 是可接受的排列
    • 那我不明白,011、101 和 110 将是索引(一旦转换为基数 10)还是什么?
    • @Simone Margaritelli,它们是 2 个数字,最后你应该得到它们的相关索引,我编辑了我的答案以表明这一点。
    • 这是将 011,101,110 转换为 (1,2),(3,1),(3,2) 的方法?我在这里遗漏了一些东西 XD
    【解决方案2】:

    创建一个包含 k 个 1 和 N-k 个 0 的数组。 然后使用 C++ std::next_permutation 之类的算法,其工作原理如下:

    1. 从左边开始,找到最右边的前一个零。
    2. 用一代替零并对数组的其余部分进行排序。

    例如, 从 0 0 0 0 1 1 1 开始

    在位置 5 找到最右边的一个,将其移动到位置 4: 0 0 0 1 0 1 1

    对其余部分进行排序: 0 0 0 1 0 1 1

    在第 6 位找到最右边的一个,将其移动到第 5 位: 0 0 0 1 1 0 1

    对其余部分进行排序: 0 0 0 1 1 0 1

    在第 7 位找到最右边的一个,将其移动到第 6 位: 0 0 0 1 1 1 0

    对其余部分进行排序: 0 0 0 1 1 1 0

    在位置 4 找到最右边的一个,将其移动到位置 3: 0 0 1 0 1 1 0

    对其余部分进行排序: 0 0 1 0 0 1 1

    等等……

    【讨论】:

    • 是的,但我应该有一个大约 500 位的数组,我不能这样做
    • 你可以用它们的坐标替换它们,然后做同样的事情。实际上,如果 k == 2 您可以执行以下操作: a[0] = N-1;一[1] = N;如果 (a[0] + 1 == a[1]) { a[0]--;一[1] = N; } 其他 {a[1]--; }
    • 这将产生相邻的结果,就像我认为的算法一样......尝试使用实数:)
    • 是的。您想从序列中排除相同的组合吗?
    • 不仅如此,我不希望相同的数字在彼此附近重复...例如,01和21(或12)连续重复1,我不希望这样
    猜你喜欢
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多