How to generate all the permutations of a multiset?

前端 未结 5 1619
时光取名叫无心
时光取名叫无心 2020-11-28 13:01

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?

5条回答
  •  萌比男神i
    2020-11-28 13:36

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

提交回复
热议问题