Fast permutation - number - permutation mapping algorithms
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In combinatorics and computer science, the task of converting between permutations and integers represents a fundamental operation. This topic covers fast algorithms for mapping permutations to numbers and vice versa, useful in applications like combinatorial enumeration, cryptographic systems, and efficient data storage. This article delves into the technical underpinnings of these algorithms, providing examples and a summary table for clarity.
Permutations and Factorial Number System
A permutation of a set is a rearrangement of its elements. For a set , there are (n factorial) possible permutations. The factorial number system or factoradics provides a powerful framework for mapping permutations to numbers.
Factorial Number System
In the factorial number system, numbers are expressed in terms of factorials. For a number in the base- system:
where . This representation is unique, just like binary or decimal systems, but it is based on factorials rather than powers of a base.
Permutation -> Number Mapping
Algorithmically, converting a permutation to a number requires determining the factoradic representation. Suppose we have a permutation .
Here's an example with , :
- Determine the Position: For each element , count the number of elements in the remaining list that are smaller. This count gives you the corresponding factoradic digit.
- For : there are 2 fewer elements ; hence .
- For : there are 0 fewer elements; hence .
- For : there is 1 fewer element ; hence .
- For : there are 0 fewer elements; hence .
- Calculate the Factoradic: Use these values:
Number -> Permutation Mapping
To convert a number back to a permutation, reverse the process using successive divisions:
- Initialize: Take number and for set , iteratively determine each position.
- Determine Factoradic Digits: For each factorial term , determine and update .
- Construct Permutation: Starting with a sorted list of numbers, choose elements based on factoradic digits , removing each chosen element from the list.
Using as an example:
- R (choose 3rd smallest, remove , result )
- R (choose 1st smallest, remove , result )
- R (choose 2nd smallest, remove , result )
- remains
Permutation .
Key Takeaways
To summarize the mapping operations from permutations to numbers and numbers to permutations efficiently:
| Task | Process | Example |
| Permutation -> Number | Count smaller remaining elements to create a factoradic representation | produces |
| Number -> Permutation | Use successive divisions to select elements by index indicated by factoradic | maps to |
Applications and Further Considerations
Fast permutation-number mapping algorithms are crucial in generating permutations efficiently, which is essential in simulation models, combinatorial game theory, and cryptographic systems. This representation enables easy ranking and unranking of permutations, extending their utility significantly.
These algorithms also enable memory-efficient data types in programming environments, with numerous libraries implementing them. Utilizing these allows for significant performance improvements in situations where handling and manipulating large datasets of permutations is necessary.
By mastering these conversions between numeric and permutation representations, developers can harness the full power of combinatorial operations in efficient and elegant ways.
Related reading
- Fast Prime Factorization Algorithm
- Fast prime factorization module
- Fast sigmoid algorithm
- Fast solution to Subset sum algorithm by Pisinger
- Fast way to calculate n mod m where m is prime?
- Fast way to generate pseudo-random bits with a given probability of 0 or 1 for each bit
- Fast stable sorting algorithm implementation in javascript
- Fast String Hashing Algorithm with low collision rates with 32 bit integer

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.