How to generate all the permutations of a multiset?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Generating permutations of a multiset is an intriguing computational problem with practical applications in areas such as combinatorics, optimization, and algorithm design. Unlike standard sets, a multiset allows for repeated elements, making the task of generating permutations slightly more complex. This article delves into the algorithms used to generate permutations of a multiset, walks through technical explanations, and illustrates how these work with examples.
Understanding Multisets
A multiset is a collection of elements where order does not matter, but unlike a set, duplicates are allowed. For example, \{'a', 'a', 'b'\}
is a multiset with two 'a'
elements and one 'b'
element. Permutations of a multiset are all possible orderings of its elements, considering repetitions.
Example:
Consider the multiset \{'a', 'a', 'b'\}
. The permutations are:
- {'a', 'a', 'b'}
- {'a', 'b', 'a'}
- {'b', 'a', 'a'}
Generating Multiset Permutations
When generating permutations of a multiset, the objective is to explore each unique ordering. The algorithmic approach to generate these permutations typically involves both recursive backtracking and iterative methods.
Technical Explanation
There are different approaches to generate permutations of a multiset effectively. Two commonly used methods are:
- Recursive Backtracking
- Lexicographic Permutation Generation
1. Recursive Backtracking Method
Recursive backtracking is a powerful technique used in combinatorial problems. It involves exploring all potential solutions and evaluating which of these are valid permutations, while managing repeated elements efficiently.
Detailed Steps:
• Base Case: If the multiset is empty, return an empty list containing an empty permutation. • Recursive Case: • For each unique element in the multiset: • Remove one instance of the element from the multiset. • Generate all permutations of the remaining elements. • Prepend the removed element to each of the generated permutations.
Example Code (Python):
• Sort the multiset, ensuring the duplicates are adjacency. • Generate the next permutation in lexicographic order: • Identify the longest non-increasing suffix. • Switch the pivot with the successor. • Reverse the suffix to get the first following permutation.
• Combinatorial Chemistry: Generating compounds and configurations. • Cryptography: In algorithms requiring exhaustive key searches. • Bioinformatics: DNA sequencing where repeated nucleotides are common.
Related reading
- How to Generate Combinations of Elements of a ListT in .NET 4.0
- How to generate maximally unbalanced AVL trees
- How to generate n different colors for any natural number n?
- how to generate Narcissistic numbers faster?
- How to generate random graphs?
- How to generate random numbers biased towards one value in a range?
- How to generate Sudoku boards with unique solutions
- How to generate the power-set of a given List?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.