|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Combinations and Permutations question
I'm seeking advice for a combinations.
I'm familiar with a small amount of statistical math, and I wanted to create a combination listing. I understand that you use nCk() function to get the number of unique combinations... n! --------- = number of combinations k!(n-k)! but I want to get a list of all unique combinations. I.E. - If I used CAT and wanted 2 letter combinations of it's letters (CAT) 3 C 2 List - CA AT CT I want to create an array that contains all the unique combinations that can be derived from the components that will be combined. Any suggestions / solutions? Thanks. |
|
#2
|
|||
|
|||
|
Re: Combinations and Permutations question
Quote:
Just call the Comb. function and get all combinations to the length of the word. nC0, nC1, nC2, ... nCword.length.
__________________
~mgb |
|
#3
|
|||
|
|||
|
Not really using a word. That was just an example. It's going to be a database of components that can be matched in combinations.
|
|
#4
|
|||
|
|||
|
Can you explain more? How many elements does each combo. have? 2Cn or 3Cn or all possible subsets?
|
|
#5
|
|||
|
|||
|
this (c#) function does what you're looking for (hopefully)
Code:
public static String[] getCombinations(String[] sourceArray,int size)
{int i,j;
int sourceLength=sourceArray.Length;
if (sourceLength<size) {size=sourceLength;}
int[] indices=new int[size];
int count=1;
for (i=0;i<size;)
{count*=(sourceLength-i);
count/=++i;
}
String[] items=new String[count];
int indexCap=1+sourceLength-size;
for (i=0;i<count;++i)
{for (j=0;j<size;++j) {items[i]+=sourceArray[j+indices[j]]+" ";}
items[i]=items[i].Substring(0,items[i].Length-1);
while (0<j)
{indices[--j]++;
if (indices[j]<indexCap) {break;}
}
if (indices[0]<indexCap)
{for (j=0;j<size;++j) {if (indices[j]==indexCap) {indices[j]=indices[j-1];}}}
}
return items;
}
Let me know if you need it explained... What's the comb() function? Last edited by epl : July 16th, 2003 at 11:54 AM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Software Design > Combinations and Permutations question |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|