
Comments and Discussions



Hi,
I have 80 C 20 combinations, and there are 3535316142212174320 combinations in this.
I need to pick up one random combination ... ElementAt use INT as input, but I need double???
So how to pick random combination from this large set?
THANK YOU





I’m not sure which code you’re looking at, because none of the classes in my zip file provide an ElementAt method. My code isn’t really amenable to random access into the set of combinations.
If you want one single random combination, why not just place all 80 elements in an array, generate a random permutation, copy out the first 20 elements, sort them if you want, and return them?





Hi,
thank You for fast answer. I've build Your code, get Combinations.dll and use it like this :
int[] Elements = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80 };
Combinations<int> C = new Combinations<int>(Elements, 20);
Random RNDGen = new Random(DateTime.Now.Millisecond);
long RNDCombination = (long)Math.Floor(RNDGen.NextDouble() * 3535316142212174320);
And now I want to get that combination like :
int[] RNDComb = C.ElementAt(RNDCombination);
But .ElementAt() takes int as input index. Am I making some mistake ??? How could I get some combination from Your class, lets say 233423435th combination.
Thank You





Again, there is no ElementAt method! Not one that takes an int, none. The algorithm used in my library can’t efficiently generate an arbitrary combination, only iterate through all combinations. I already told you in my previous message how to generate a random combination, which doesn’t use this library; that solution I described is efficient and I recommend you use it.





Your code is perfect for getting all combinations for a given set of inputs. For a program that need to calculate best rating option by combining various groups I have a requirement to put back the combinations together, objecive is to find all potential combinations.
for example if the inputs are ABC , I have to generate all combinations A, B, C, AB, AC, BC and ABC. This can be achieved by your code. I have to make cost computations for each of those options.
Now I have to select the cheapest option for putting combination together.
A + BC
B+AC
C +AB
ABC
Do you have any suggestions on how I can use your code to best accomplish this. Basically I have to concatenate combinations to a length of 3 without repeating any elements.
Appreciate your help.






@Piebaldconsult, Saw your article yesterday, but was not sure how to quickly use it. This one is packaged very well. But I am seeing performance issues once I get past 12 inputs. Will try your logic and see.





OK, let me know how it goes. Good luck.





Adding a plus sign to the character set will not work, because combinations are orderagnostic. So you will not see “A+BC” and “AB+C” as separate outputs, because they are equivalent.





I thought you were doing permutations. But, no, it may not work; just something to consider and try.





If you actually want all possible constructions of ABC, there are quite a lot: A+B+C, AB+C, A+BC, AC+B, and ABC. I don’t think my library is what you’re looking for. I think you are better off allocating n “groups”, placing each element into a group, and then building the whole by “buying” each group as a construct. So when A, B, and C are all in group 0, you are buying object ABC. When A and B are in group 0 and C is in group 1, you are buying AB+C. There will be duplicates with the simplest approach to implementing this algorithm, but those can probably be eliminated. However, this is not the problem the library described in this article solves.





A recursive c program thats generates all of the combinations nCk in a fast possible way.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int nCk(int n,int loopno,int ini,int *a,int k)
{
if(k==0)
return 1;
static int count=0;
int i;
loopno;
if(loopno<0)
{
a[k1]=ini;
for(i=0;i<k;i++)
{
printf("%d,",a[i]);
}
printf("\n");
count++;
return 0;
}
for(i=ini;i<=nloopno1;i++)
{
a[k1loopno]=i+1;
nCk(n,loopno,i+1,a,k);
}
if(ini==0)
return count;
else
return 0;
}
void main()
{
int n,k,*a,count;
printf("Enter the value of n and k\n");
scanf("%d %d",&n,&k);
a=(int*)malloc(k*sizeof(int));
count=nCk(n,k,0,a,k);
printf("No of combinations=%d\n",count);
getch();
}
modified 20Jan12 14:53pm.






Best i m rating it to by 5
If you can think then I Can.





This code is super fast, i translated the combinations and entity classes from c# to vb and it works superb under vb.net, this code has stood the test of time. Many thanks for sharing with us





Hi Hawk777,
Your code is just what I'm looking for, but a bit more, new and rustty in C's, dont know were to start. Is is possible to have a demo of this code?
Yabadabado





There are examples of how to use the library in the article. The ZIP file also contains a fullyworked benchmarking tool which (naturally) calls the library.





Sorry for the trouble, when I run the code in v studio, I got a messages part of it that say I have to add an executable project which reference the library... Should I copy "int[] input = new int[] { 1, 2, 3, 4, 5 }; into the line before "foreach".





Well, you want to do what the message says. Create an executable project (console project or Windows forms project or whatever type), and use the Visual Studio UI to make the executable project "reference" the Combinations project. Then you will copy the int[] input = … line as well as the foreach and its contents into, say, Main .





Hi,
Your code is fantastic but I have a little problem.
It seems that you do not take care of the order of the subsets.
For example, if the input set is {1,2,3,4,5} the produced by your code combinations of 3 are: {1,2,3}...{3,4,5}.
But if the order matters, then there are missing combinations such as {3,2,1}.
Is there an easy way to fix this?
Thanks in advance
antonis





Look on the second page of comments at the thread titled "Beautiful implemenation! Is it easy to reorder the combinations?" started by trevstar. What you're looking for is permutations as well as combinations; he mentions where he found a permutation generator which could be applied after the combination generator.





First of all thank you for the code.
I have a particular situation.
int[] deepLevels=new int[] { 0, 1, 0};
The second value (1) says that for "B" value there are 2 possible values (0 and 1).
string[] input = new string[] { "A", "B", "C" };
Combinations combinations = new Combinations(input, 2);
How can I use (or modify) the code in order to obtain the following output:
("A{0}","B{0}")
("A{0}","B{1}")
("B{0}","C{0}")
("B{1}","C{0}")
("A{0}","C{0}")
Thanks.





I think what you want is for the letter B to appear only once in any given output array, but for each time B appears, to have it appear in two consecutive arrays. I believe the following code will do what you want, as long as inputStrings has no duplicate elements, though the code is untested as I no longer have a .NET development environment installed.
string[] inputStrings = new string[]{"A", "B", "C"};
int[] deepLevels = new int[]{0, 1, 0};
int[] indices = new int[inputStrings.Length];
for (int i = 0; i < indices.Length; ++i) {
indices[i] = i;
}
Combination<int> combinations = new Combination<int>(indices, 2);
foreach (int[] combination in combinations) {
string[] outputStrings = new string[combination.Length];
for (int i = 0; i < combination.Length; ++i) {
outputStrings[i] = inputStrings[combination[i]];
}
int[] outputLevels = new int[combination.Length];
for (int i = 0; i < outputLevels.Length; ++i) {
outputLevels[i] = 0;
}
for (;;) {
bool carry = true;
for (int i = 0; carry && i < outputLevels.Length; ++i) {
if (outputLevels[i] == deepLevels[combination[i]]) {
outputLevels[i] = 0;
carry = true;
} else {
++outputLevels[i];
carry = false;
}
}
if (carry) {
break;
}
}
}





Hi, Hawk777,
It' a very useful software and the speed of runing is very fast.
I want to use a progressbar when runing, so how to count collection sizes before run?
Thanks.





There's no closedform formula, because the answer depends on how many duplicate items there are in the input.
If there are no duplicates, then there are (n!)/(r!(nr)!) lengthr combinations from an input corpus of n elements.
If the corpus may have duplicates, I don't know the formula to compute the number of combinations. There probably is one though.







General News Suggestion Question Bug Answer Joke Rant Admin Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Type  Article 
Licence  CPOL 
First Posted  11 Jun 2006 
Views  107,719 
Downloads  1,926 
Bookmarked  51 times 

