Array permutation
Ladies and gentlemen, I would like to generate an anagram of the possible combinations. Ex: "123456" would generate: 1234 2341 6531 5431 1243 1342 or 12 43 56 23 14 16, with repetition or not, and so on, I know how to get the number of combinations by the factorial, but I do not know how to start the algorithm to generate the combinations, I have read quite a lot about combinatorics analysis, permutation etc, but I still could not implement the algorithm. If anyone can just give me a light to start that would be great! it can be in pseudocode even.
Would pass a String and the size. Former: where size would be ten hundred or thousand.
class Permutacao
{
public $resul;
private $cont;
function __construct()
{
$this->resul = array();
}
public function permuta($array, $indice)
{
if($indice == count($array)){
$this->cont++;
array_push($this->resul, $this->arraytemp);
}
else{
for ($i=0; $i < count($array); $i++) {
$valida = false;
for ($j=0; $j < $indice; $j++) {
if($this->arraytemp[$j] == $array[$i]) $valida = true;
}
if (!$valida) {
echo $indice." ";
$this->arraytemp[$indice] = $array[$i];
$this->permuta($array, $indice + 1);
}
}
}
}
}
echo "<pre>";
$permuta = new Permutacao();
$permuta->permuta(array(1,2,3,4), 0);
print_r($permuta->resul);
UPDATE: researching I found this simplest way to implement, my biggest difficulty is to understand the algorithm, as I pass the "indice index = 0"it reaches 3 and then returns to 1 etc., and if you pass the array with repeated values it returns nothing! 1,1,2,3 , he would have to return
1,1,3
1,1,2
2,1,1 etc.
2 answers
I solved as follows:
private function get_permutations(array $arr = array(), $max_length = NULL){
if(count($arr) == 1 || ($max_length !== NULL && $max_length <= 1)){
return array_values($arr);
}
$return_array = array();
foreach($arr as $key => $val){
$temp_arr = $arr;
unset($temp_arr[$key]);
$temp = call_user_func('NomeDaClasse::'.__FUNCTION__, $temp_arr, $max_length !== NULL ? $max_length - 1 : NULL);
for($x = 0; $x < count($temp); $x++){
$temp[$x] = $val.$temp[$x];
}
$return_array = array_merge($return_array, $temp);
}
return $return_array;
}
How about it?
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"
}
*/
Other examples in: