8

I saw this algorithm that will take numbers or words and find all possible combinations

And I'm using it, but it does NOT return all "real" combinations.

PHP:

<?php
    require_once 'Math/Combinatorics.php';
    $words = array('cat', 'dog', 'fish');
    $combinatorics = new Math_Combinatorics;
    foreach($combinatorics->permutations($words, 2) as $p) {
        echo join(' ', $p), "\n"; 
    }
?>

And it returns:

cat dog
dog cat
cat fish
fish cat
dog fish
fish dog

But these are not all real combinations, all real combinations includes these too:

cat cat
dog dog
fish fish

And that is what I need, the method to get all real combinations:

cat dog
dog cat
cat fish
fish cat
dog fish
fish dog
cat cat
dog dog
fish fish
Community
  • 1
  • 1
Minion
  • 2,497
  • 4
  • 27
  • 29
  • Why don't you add these combinations yourself? Seems easy enough to loop through your data and add the pairs manually. – laurent Mar 20 '12 at 12:49
  • Isn't this the same question as before? You're just having doubts on the answer it seems. why not continue there? – Nanne Mar 20 '12 at 12:52
  • 5
    Math_Combinatorics - "A package that returns all the combinations and permutations, without repitition, of a given set and subset size. Associative arrays are preserved.". The key here is "without repetition". – strkol Mar 20 '12 at 12:54
  • If this is just for fun, it wouldn't be hard to write your own function that would add the duplicates. If it isn't just for fun, there is probably a better way to do what you want. – jasonlfunk Mar 20 '12 at 12:57
  • Have a look at my code... :-) – Dr.Kameleon Mar 20 '12 at 13:12
  • thats because you are using a code for permutations not combinations – Juan Antonio Orozco Jul 10 '13 at 01:20

2 Answers2

12

OK, here's your code (and btw, thanks for posting such an interesting and challenging problem - at least for me... :-)) - using recursion for all possible permutations (by N) given an array of elements)

Code :

<?php

function permutations($arr,$n)
{
     $res = array();

     foreach ($arr as $w)
     {
           if ($n==1) $res[] = $w;
           else
           {
                 $perms = permutations($arr,$n-1);

                 foreach ($perms as $p)
                 {
                      $res[] = $w." ".$p;
                 } 
           }
     }

     return $res;
}

// Your array
$words = array('cat','dog','fish');

// Get permutation by groups of 3 elements
$pe = permutations($words,3);

// Print it out
print_r($pe);

?>

Output :

Array
(
    [0] => cat cat cat
    [1] => cat cat dog
    [2] => cat cat fish
    [3] => cat dog cat
    [4] => cat dog dog
    [5] => cat dog fish
    [6] => cat fish cat
    [7] => cat fish dog
    [8] => cat fish fish
    [9] => dog cat cat
    [10] => dog cat dog
    [11] => dog cat fish
    [12] => dog dog cat
    [13] => dog dog dog
    [14] => dog dog fish
    [15] => dog fish cat
    [16] => dog fish dog
    [17] => dog fish fish
    [18] => fish cat cat
    [19] => fish cat dog
    [20] => fish cat fish
    [21] => fish dog cat
    [22] => fish dog dog
    [23] => fish dog fish
    [24] => fish fish cat
    [25] => fish fish dog
    [26] => fish fish fish
)

HINT : By permutations($words,2), you'll be able to get exactly what you wanted...

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
  • 1
    I'm trying to convert this to return an array of arrays instead of an array of strings. So each inner array would have $n elements in it. It's proving more difficult than it seems it should be... – still_dreaming_1 Oct 13 '16 at 23:03
  • Using the Combinatorics pear class for that now – still_dreaming_1 Oct 18 '16 at 18:11
  • 2
    Actually the Combinatorics class does help with some things, but it is different from this because it would not produce subsets with duplicate values like this solution does. I still need a version of this that works with arrays instead of strings. – still_dreaming_1 Oct 18 '16 at 20:27
0

Your current code gives you permutations, simply add duplicates:

foreach($words as $w) {
    echo "$w $w\n";    
}

What's the problem?

William R
  • 185
  • 1
  • 10
  • The problem is that that is non-generic, and will not work if you are working with more then 2 dimensions: in 3 dimensions he would need to add cat cat fish, cat fish cat, fish cat cat. etc... again combinatorics. – Ellert van Koperen Jan 16 '16 at 17:54