vanderson Posted December 26, 2008 Share Posted December 26, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/138473-need-algorithm-for-all-possible-combinations-please-help/ Share on other sites More sharing options...
corbin Posted December 26, 2008 Share Posted December 26, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/138473-need-algorithm-for-all-possible-combinations-please-help/#findComment-723990 Share on other sites More sharing options...
DarkWater Posted December 26, 2008 Share Posted December 26, 2008 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/138473-need-algorithm-for-all-possible-combinations-please-help/#findComment-724021 Share on other sites More sharing options...
Mark Baker Posted December 26, 2008 Share Posted December 26, 2008 If my math skills aren't failing me, that should work. It'll work perfectly to get the number of permutations, but I think the OP wants the actual list of permutations Quote Link to comment https://forums.phpfreaks.com/topic/138473-need-algorithm-for-all-possible-combinations-please-help/#findComment-724037 Share on other sites More sharing options...
vanderson Posted December 26, 2008 Author Share Posted December 26, 2008 Thanks, but i want the actual list of permutations for example. I have the following array: 1,2,3,4,5,6,7,8,9,10,11,12 I want all possible combinations of 6 elements.. Like this: 1,2,3,4,5,6 1,2,7,10,11,12 7,8,9,10,11,12 and so on Quote Link to comment https://forums.phpfreaks.com/topic/138473-need-algorithm-for-all-possible-combinations-please-help/#findComment-724186 Share on other sites More sharing options...
sasa Posted January 1, 2009 Share Posted January 1, 2009 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138473-need-algorithm-for-all-possible-combinations-please-help/#findComment-727574 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.