Jump to content

need algorithm for all possible combinations.. please help


vanderson

Recommended Posts

i need algorithm that will generate all possible combinations of 6 elements from a given array of 12 elements... the algorithm i have been using was good only for up to 10 element array.. since the number of combinations is rapidly increasing i need more efficient algorithm because there is not enough memory to run mine.. here is mine algorithm:

<?
ini_set('memory_limit', '1000M');
$y=0;
$group=array(49,21,32,31,45,22,34,23,43,20,24,25); //12 element array
foreach($group as $k =>$v1){
    foreach($group as $k => $v2){
        foreach($group as $k => $v3){
            foreach($group as $k => $v4){
                foreach($group as $k => $v5){
                    foreach($group as $k => $v6) {
                        
                    $combo[$y][0]=$v1;
                    $combo[$y][1]=$v2;
                    $combo[$y][2]=$v3;
                    $combo[$y][3]=$v4;
                    $combo[$y][4]=$v5;
                    $combo[$y][5]=$v6;
            $y++;
                    }
            }
            }
             }
             }
} ?>

there should be more efficient way to do this.. any help is appreciated :)

Link to comment
Share on other sites

I'm confused about your goal, and don't feel like wrapping my head around 10 foreach statements....  So...

 

 

Let's say you had the dataset 1,2,3,4,5,6,7,8,9,10,11,12.  You would want something like:

 

1 7

1 8

1 9

1 10

1 11

1 12

2 7

2 8

2 9

2 10

 

So on?

Link to comment
Share on other sites

<?php
function factorial($num) {
    if ($num == 1) { 
        return 1; 
    }
    return $num * factorial($num - 1);
}

function permutations($array, $num) {
    return factorial(count($array)) / (factorial(count($array) - $num));
}

$group=array(49,21,32,31,45,22,34,23,43,20,24,25);
echo permutations($group, 6);
?>

 

If my math skills aren't failing me, that should work.

Link to comment
Share on other sites

try

<?php
function comb($a, $len){
if ($len > count($a))return 'error';
$out = array();
if ($len==1) {
	foreach ($a as $v) $out[] = array($v);
	return $out;
}
$len--;
while (count($a) > $len) {
	$b = array_shift($a);
	$c = comb($a, $len);
	foreach ($c as $v){
		array_unshift($v, $b);
		$out[] = $v;
	}
}
return $out;
}
$test = array(1,2,3,4,5,6,7,8,9,10,11,12);
$a = comb($test,6);
print_r($a);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.