Skip to main content
3 of 7
added 165 characters in body

i'm not sure if the general answer is in this paper Algorithm 515: Generation of a Vector from the Lexicographical Index (as i havent read it, not available online), but it gives the algorithm for combinations, i.e from combination to index and index to combination.

In Knuth's The Art Of Computer Programming (TAOCP), Semi-numerical Algorithms also has examples and references of such algorithms (as mentioned in comment).

In general combinatorics this is called ranking and unranking (as mentioned in comment).

Given the fact that the items of a combinatorial sequence are lexicographicaly ordered and the fact that one can (efficiently) count the combinatorial items up to $N$.

The general algorithm to find the index of any combinatorial object in lexicographic order would be (rough outline):

  1. For k in 0..n-1
  2. Count how many items of length n-1-k have the kth element less than comb[k]
  3. index is the sum of the counters.

Of course the previous algorithm is based on the fact that there is a lexicographic order and the counting of combinatorial items (e.g combinations, permutations, partitions etc..) is efficient (which is not always the case).

Especially note that partitions are easier to generate in reverse lexicographic order (i.e descending). A PhD thesis on encoding partitions as lexicographicaly ascending (instead of descending) and the reference FAST ALGORITHMS FOR GENERATING INTEGER PARTITIONS, ANTOINE ZOGHBIU and IVAN STOJMENOVIC, 1998