具有互斥性的二维阵列的所有组合


All combinations of 2D array with mutual exclusivity

我有一个像这样的数组:

$i[0] = ['a', 'b', 'c'];
$i[1] = ['d', 'e'];
$i[2] = ['a', 'b', 'c'];
$i[3] = ['d', 'e'];
$i[4] = ['f', 'g', 'h'];

我想获得这个数组的所有可能的排列或组合,但不从两个或多个子数组中使用相同的值两次。例如,结果a d b e f是可能的,但a d a d f是不可能的。

我尝试过基本的排列算法,但我不知道如何修改它来做我想要的。

这是我目前得到的:

function array_permutation(array $a){
    $count = array_map('count', $a);
    $finalSize = 1;
    foreach ($count as $val) {
        $finalSize *= $val;
    }
    $output = [];
    for ($i = 0; $i < $finalSize; $i++) {
        $output[$i] = [];
        for ($c = 0; $c < count($a); $c++) {
            $index = ($i + $finalSize) % $count[$c];
            array_push($output[$i], $a[$c][$index]);
        }
    }
    return $output;
}

一个非常简单的方法是:

function decartProductExclusive($one, $two)
{
   $result = [];
   for($i=0; $i<count($one); $i++)
   {
      for($j=0; $j<count($two); $j++)
      {
         if(!count(array_intersect((array)$one[$i], (array)$two[$j])))
         {
            $result[]=array_merge((array)$one[$i], (array)$two[$j]);
         }
      }
   }
   return $result;
}
function createAssociation()
{
   $args   = func_get_args();
   if(!count($args))
   {
      return [];
   }
   $result = array_shift($args);
   while($array=array_shift($args))
   {
      $result=decartProductExclusive($result, $array);
   }
   return $result;
}
$i[0] = ['a', 'b', 'c'];
$i[1] = ['d', 'e'];
$i[2] = ['a', 'b', 'c'];
$i[3] = ['d', 'e'];
$i[4] = ['f', 'g', 'h'];
$result = call_user_func_array('createAssociation', $i);

(检查小提琴)你的问题是关于计算笛卡尔积,但有条件,元组不能包含重复的元素。然而,这个条件可以在不计算每次迭代中的交集的情况下被满足(这将是一种过度的做法)。相反,您可以使用array_unique()过滤结果数组,就像在这个小提琴。