How to generate all the permutations of a multiset?

piyukr picture piyukr · Oct 30, 2013 · Viewed 8.9k times · Source

A multi-set is a set in which all the elements may not be unique.How to enumerate all the possible permutations among the set elements?

Answer

ruggero picture ruggero · Jan 22, 2014

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!)