I have an array that looks like this: $arr_in = [0, 1, 2, 3, 4, 5, ...]

I need a function that will give me an array like this:

[
[[0, 1, 2, 3, 4, 5]],   
[[0, 4, 2, 3, 1, 5]],  
...
[[0, 1, 2, 3, 4], [ 5 ]], 
[[0, 1, 2, 3], [ 4, 5 ]], 
[[0, 1, 2], [ 3, 4, 5 ]],
[[0, 4, 2, 3, 1], [ 5 ]], 
[[0, 4, 2, 3], [ 1, 5 ]], 
[[0, 4, 2], [ 3, 1, 5 ]], 
...
[[0, 1, 2], [ 3, 4 ], [ 5 ]], 
...
[[0, 4, 2], [ 3, 1 ], [ 5 ]],
...
[[ 0 ], [ 1 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ]]
]

The order of the elements inside the subsets counts. (permutation)
[ [ 0, 1, 2, 3 ] [ 4, 5 ] ] and [ [ 1, 2, 3, 0 ] [ 4, 5 ] ] are 2 different sets.

The order of the subsets does not count. (combination)
[ [ 0, 1, 2, 3 ] [ 4, 5 ] ] and [ [ 4, 5 ] [ 0, 1, 2, 3 ] ] are the same set.

In the last line every element should be alone in its own sub-array.

Also the function needs to have a $level_min and $level_max where I can set the minimum and maximum of subsets inside the sets.

my_function( $arr_in, 2, 3 ) should only include sets of 2 and 3 subsets, like so:

[ 
[[0, 1, 2, 3, 4], [ 5 ]], 
[[0, 1, 2, 3], [ 4, 5 ]], 
[[0, 1, 2], [ 3, 4, 5 ]],
[[0, 4, 2, 3, 1], [ 5 ]], 
[[0, 4, 2, 3], [ 1, 5 ]], 
[[0, 4, 2], [ 3, 1, 5 ]], 
...
[[0, 1, 2], [ 3, 4 ], [ 5 ]], 
...
[[0, 4, 2], [ 3, 1 ], [ 5 ]],
...
]

Since this has been flagged as duplicate of another question that's not even close to this one, here is a complete output array where input array is [ 0, 1, 2 ]

[
[ [ 0, 1, 2 ] ],
[ [ 0, 2, 1 ] ],
[ [ 1, 0, 2 ] ],
[ [ 1, 2, 0 ] ],
[ [ 2, 1, 0 ] ],
[ [ 2, 0, 1 ] ],
[ [ 0 ], [ 1, 2 ] ],
[ [ 0 ], [ 2, 1 ] ],
[ [ 1 ], [ 0, 2 ] ],
[ [ 1 ], [ 2, 0 ] ],
[ [ 2 ], [ 0, 1 ] ],
[ [ 2 ], [ 1, 0 ] ],
[ [ 0 ], [ 1 ], [ 2 ] ]
]  
  • For this array of 3 elements there are 13 sets of subsets.
  • A set can contain 1 to 3 subsets.
  • Inside every set, regardless of the number of subsets there are always 3 elements.
  • Any element appears only once in a subset and only once in a set.
  • Elements do permutate inside a set. [ [ 0 ], [ 1, 2 ] ] is not the same as [ [ 0 ], [ 2, 1 ] ].
  • Subsets do not permutate. It's a combination. [ [ 0 ], [ 1 ], [ 2 ] ] and [ [ 2 ], [ 1 ], [ 0 ] ] are the same set.

I am trying to construct a routing algorithm where all elements inside the subsets are way points while the subsets represent routes.

Here is what I have so far:

function my_function($in,$min,$max){
    $out = array();
    for($i=$min; $i<$max+1; $i++){   // $i is the number of subsets in each set

        $sets = ??                       // all I know so far is that if $i=1 $i=P(count($in),count($in))
                                        // since for $i=1 all the sets contain all permutations of the elements inside $in
        for($j=0; $j<$sets; $j++){
            array_push($out,array());
            //construct sets of subsets
        }
    }
    return $out;
}

These are a few functions to use:

function fact($x){                          // !X
    return gmp_strval(gmp_fact($x));
}
function C($n,$r){                          // nCr
    return fact($n)/fact($r)/fact($n-$r);
}
function P($n,$k){                          // nPk
    return fact($n)/fact($n-$k);
}
function pc_permute($items, $perms = array( )) {    //samples permutations
    if (empty($items)) {
        $return = array($perms);
    }  else {
        $return = array();
        for ($i = count($items) - 1; $i >= 0; --$i) {
             $newitems = $items;
             $newperms = $perms;
         list($foo) = array_splice($newitems, $i, 1);
             array_unshift($newperms, $foo);
             $return = array_merge($return, pc_permute($newitems, $newperms));
         }
    }
    return $return;
}

If you can help with as little as a small suggestion please do. If you don't understand, don't just flag my question to be a duplicate of another that's not even close to be this complex. Thank you!

0

There are 0 answers