Skip to content

Instantly share code, notes, and snippets.

@tpetry
Created July 25, 2010 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tpetry/489584 to your computer and use it in GitHub Desktop.
Save tpetry/489584 to your computer and use it in GitHub Desktop.
<?php
function recursiveCombinatorics($strings, $removed , &$combinations, $recursionLevel, $debug) {
$rec = str_repeat('&nbsp;', 8*$recursionLevel);
foreach($strings as $i => $string) {
if($debug) printf("%srecursiveCombinatorics(strings=array(%s), removed=array(%s), combinations=array(%s))<br>",
$rec,
implode(', ', $strings),
implode(', ', $removed),
implode(', ', array_keys($combinations))
);
// recursive argument
$recRemoved = array_merge($removed, array($string));
$recStrings = $strings;
unset($recStrings[$i]);
// (recursive) check for combinations
sort($recRemoved);
$combination = implode('-', $recRemoved);
if(!isset($combinations[$combination])) {
$combinations[$combination] = 0;
if(count($recStrings) > 0) {
recursiveCombinatorics($recStrings, $recRemoved, $combinations, ++$recursionLevel, $debug);
$recursionLevel--;
}
}
}
}
// Kombinationen suchen
$strings = array('01', '03', '02', '04');
$combinations = array();
recursiveCombinatorics($strings, array(), $combinations, 0, false);
// Ausgeben
ksort($combinations);
foreach($combinations as $combination => $devNull)
echo "$combination<br>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment