How to generate all the permutations of a multiset?

Generating all the possible permutations and then discarding the repeated ones is highly inefficient. Various algorithms exist to directly generate the permutations of a multiset in lexicographical order or other kind of ordering. Takaoka’s algorithm is a good example, but probably that of Aaron Williams is better

http://webhome.csc.uvic.ca/~haron/CoolMulti.pdf

moreover, it has been implemented in the R package ”multicool”.

Btw, if you just want the total number of distinct permutations, the answer is the Multinomial coefficient:
e.g., if you have, say, n_a elements ‘a’, n_b elements ‘b’, n_c elements ‘c’,
the total number of distinct permutations is (n_a+n_b+n_c)!/(n_a!n_b!n_c!)

Leave a Comment