i want load in a list the combination of N number without repetition, giving to input the elements and group. For example, with 4 elements [1,2,3,4], i have for:
Group 1: [1][2][3][4];
Group 2: [1,2][1,3][1,4][2,3][2,4][3,4];
Group 3: [1,2,3][1,2,4][1,3,4][2,3,4]
Group 4: [1,2,3,4]
Now, i have solved it using nested loop for, for example with group 2, i write:
for x1 := 1 to 3 do
for x2 := Succ(x1) to 4 do
begin
// x1, x2 //
end
or for group 3, i wrote:
for x1 := 1 to 2 do
for x2 := Succ(x1) to 3 do
for x3 := Succ(x2) to 4 do
begin
// x1, x2, x3 //
end
and so for other groups. In general, if i want to do it for group N, as i can to do, without write N procedures with nested loops? I have thinked to a double while..do loop one to use for counter and one to use for groups count, but so is little hard, i wanted know if there was some solution more simple and fast, too using operator boolean or something so. Who can give me some suggest about it? Thanks very much.
Following the link that David posted and clicking around led me to an article where they coin the term "Banker's Search", which seems to fit your pattern.
The article provides an example solution in C++, utilizing recursion:
Efficiently Enumerating the Subsets of a Set
This seems to be a question that comes up over and over and a few bits of code are kicking about that address the problem. A very nice algorithm in some code has been written but it wasn't strictly clean C and not portable across UNIX or Linux or any POSIX system, therefore I cleaned it up and added warning messages, usage and the ability to provide a set size and sub_set size on the command line. Also comb[] has been transitioned to a more general pointer to an array of integers and calloc used to zero out the memory needed for whatever set size one may want.
The following is ISO IEC 9899:1999 C clean :
One may compile the above on an Opteron based machine thus :
A quick trivial test with a set size of 10 and a sub-set of 6 will be thus :
The math is correct :
Now that the integer array comb is based on a pointer system we are only restricted by available memory and time. Therefore we have the following :
This looks correct :
We may now push the limits a bit thus :
Again the math agrees with the result :
Feel free to push the limits of your system :
I have yet to try anything larger but the math looks correct as well as the output thus far. Feel free to let me know if some correction is needed.
Dennis Clarke dclarke@blastwave.org 28 Dec 2012
It seems you are looking for a fast algorithm to calculate all k-combinations. The following Delphi code is a direct translation of the C code found here: Generating Combinations. I even fixed a bug in that code!
Output
Here's a rather fun solution reliant on bitsets. As it stands it's limited to sets of size not greater than 32. I don't think that's a practical limitation since there are a lot of subsets for a set of cardinality greater than 32.
The output is not in the order that you want, but that would be easy enough to remedy if it matters to you.
Output
And here is a variant that just lists the subsets of a specified cardinality:
Output
Unless you can't make function calls by some requirement, do this:
You really need some sort of recursion because you need automatic storage for intermediate results. Let me know if there's special requirement that makes this solution don't work.
I created this script here and worked very well: