PHP算法从单个集合生成特定大小的所有组合

asi*_*haq 33 php algorithm statistics combinations

我试图推断出一种算法,它生成特定大小的所有可能组合,就像一个接受一个字符数组和大小作为参数的函数,并返回一个组合数组.

示例:假设我们有一组字符:设置A = {A,B,C}

a)尺寸2的所有可能组合:(3 ^ 2 = 9)

AA, AB, AC
BA, BB, BC
CA, CB, CC
Run Code Online (Sandbox Code Playgroud)

b)尺寸3的所有可能组合:(3 ^ 3 = 27)

AAA, AAB, AAC,
ABA, ABB, ACC,
CAA, BAA, BAC,
.... ad so on total combinations = 27
Run Code Online (Sandbox Code Playgroud)

请注意,货币对的大小可能大于pouplation的总大小.防爆.如果set包含3个字符,那么我们也可以创建大小为4的组合.

编辑:另请注意,这与排列不同.在排列中我们不能有重复的字符,例如,如果我们使用置换算法,AA就不会出现.在统计中,它被称为抽样.

Joe*_*inz 50

我会使用递归函数.这是一个带有注释的(工作)示例.希望这对你有用!

function sampling($chars, $size, $combinations = array()) {

    # if it's the first iteration, the first set 
    # of combinations is the same as the set of characters
    if (empty($combinations)) {
        $combinations = $chars;
    }

    # we're done if we're at size 1
    if ($size == 1) {
        return $combinations;
    }

    # initialise array to put new values in
    $new_combinations = array();

    # loop through existing combinations and character set to create strings
    foreach ($combinations as $combination) {
        foreach ($chars as $char) {
            $new_combinations[] = $combination . $char;
        }
    }

    # call same function again for the next iteration
    return sampling($chars, $size - 1, $new_combinations);

}

// example
$chars = array('a', 'b', 'c');
$output = sampling($chars, 2);
var_dump($output);
/*
array(9) {
  [0]=>
  string(2) "aa"
  [1]=>
  string(2) "ab"
  [2]=>
  string(2) "ac"
  [3]=>
  string(2) "ba"
  [4]=>
  string(2) "bb"
  [5]=>
  string(2) "bc"
  [6]=>
  string(2) "ca"
  [7]=>
  string(2) "cb"
  [8]=>
  string(2) "cc"
}
*/
Run Code Online (Sandbox Code Playgroud)

  • 对于不想在每个组合中多次存在一个字符的用户,请将最后一个foreach循环更改为:if(strpos($ combination,$ char)=== false){$ new_combinations [] = $ combination。$ char;} (2认同)

San*_*dri 6

一种可能的算法是:

$array_elems_to_combine = array('A', 'B', 'C');
$size = 4;
$current_set = array('');

for ($i = 0; $i < $size; $i++) {
    $tmp_set = array();
    foreach ($current_set as $curr_elem) {
        foreach ($array_elems_to_combine as $new_elem) {
            $tmp_set[] = $curr_elem . $new_elem;
        }
    }
    $current_set = $tmp_set;
}

return $current_set;
Run Code Online (Sandbox Code Playgroud)

基本上,您要做的是获取当前集合的每个元素并附加元素数组的所有元素。

在第一步:你将得到结果('a', 'b', 'c'),在第二步之后:('aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc')等等。


cyo*_*yon 5

您可以递归执行此操作。请注意,按照您的定义,长度的“组合” n+1可以n通过采用长度的每种组合n并在集合中附加一个字母来从长度的组合中生成。如果您愿意,可以通过数学归纳法证明这一点。

因此,例如,一{A,B,C}组长度为1的组合为:

A, B, C
Run Code Online (Sandbox Code Playgroud)

因此,长度2的组合为

(A, B, C) + A = AA, BA, CA
(A, B, C) + B = AB, BB, BC
(A, B, C) + C = AC, CB, CC
Run Code Online (Sandbox Code Playgroud)

这将是代码,并在ideone上

function comb ($n, $elems) {
    if ($n > 0) {
      $tmp_set = array();
      $res = comb($n-1, $elems);
      foreach ($res as $ce) {
          foreach ($elems as $e) {
             array_push($tmp_set, $ce . $e);
          }
       }
       return $tmp_set;
    }
    else {
        return array('');
    }
}
$elems = array('A','B','C');
$v = comb(4, $elems);
Run Code Online (Sandbox Code Playgroud)