Art of Computer Programming Volume 4: Fascicle 3 has a ton of these that might fit your particular situation better then how I describe.
Gray Codes
An issue that you will come across is of course memory and pretty quickly, you'll have problems by 20 elements in your set -- 20C3 = 1140! And if you want to iterate over the set it's best to use a modified gray code algorithm so you aren't holding all of them in memory. There are many of these, and for different uses, based on the distance of elements. Do we want to maximize the differences between successive applications? minimize? et cetera.
Some of the original papers describing gray codes:
- Some Hamilton Paths and a Minimal Change Algorithm
- Adjacent Interchange Combination Generation Algorithm
Here are some other papers covering the topic:
- An Efficient Implementation of the Eades, Hickey, Read Adjacent Interchange Combination Generation Algorithm (PDF, with code in Pascal)
- Combination Generators
- Survey of Combinatorial Gray Codes (PostScript)
- An Algorithm for Gray Codes
Chase's Twiddle (algorithm)
Phillip J Chase, `Algorithm 382: Combinations of M out of N Objects' (1970)
The algorithm in C...
Index of Combinations in Lexicographical Order (Buckles Algorithm 515)
You can also reference a combination by it's index (in lexicographical order), realizing that the index should be some amount of change from right to left based on the index.
So, we have a set {1,2,3,4,5,6}... when we want three elements {1,2,3} we can say that the difference between the elements is one and in order. {1,2,4} has one change, and is lexicographically number 2. So the number of 'changes' in the last place accounts for one change in the lexicographical ordering. The second place, with one change {1,3,4} has one change, but accounts for more change since it's in the second place.
The method I've described is a deconstruction, as it seems, from set to the index, we need to do the reverse --which is much trickier. This is how Buckles solves the problem. I wrote some C to compute them, with other minor changes --I used the index of the sets rather then a number range to represent the set, so we are always working from 0...n.
Note:
- Since combinations are unordered, {1,3,2} = {1,2,3} --we order them to be lexicographical.
- This method has an implicit 0 to start the set for the first difference.
Index of Combinations in Lexicographical Order (McCaffrey)
There is another way:, it's concept is easier to grasp and program but it's without the optimizations of Buckles:
The set, $x_k...x_1 \in \mathbb{N}$ that maximizes $i = C(x_1,k) + C(x_2,k-1) + ... C(x_k,1)$, where $C(n,r) = {n \choose r}$.
For an example: $27 = C(6,4) + C(5,3) + C(2,2) + C(1,1)$. So, the 27th lexicographical combination of four things is: {1,2,5,6}, those are the indexes of whatever set you want to look at. Example below (ocaml), requires choose
function, left to reader:
(* this will find the [x] combination of a [set] list when taking [k] elements *)
let combination_maccaffery set k x =
(* maximize function -- maximize a that is aCb *)
(* return largest c where c < i and choose(c,i) <= z *)
let rec maximize a b x =
if (choose a b ) <= x then a else maximize (a-1) b x
in
let rec iterate n x i = match i with
| 0 -> []
| i ->
let max = maximize n i x in
max :: iterate n (x - (choose max i)) (i-1)
in
if x < 0 then failwith "errors" else
let idxs = iterate (List.length set) x k in
List.map (List.nth set) (List.sort (-) idxs)