permutation mapping
algorithm design
computational mathematics
number theory
combinatorial algorithms

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.

Practice algorithms

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 S=1,2,...,nS = {1, 2, ..., n}, there are n!n! (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 XX in the base-n!n! system:

X=an1(n1)!+an2(n2)!++a11!+a00!X = a_{n-1} \cdot (n-1)! + a_{n-2} \cdot (n-2)! + \ldots + a_1 \cdot 1! + a_0 \cdot 0!

where 0aii0 \leq a_i \leq i. 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 π=[π1,π2,,πn]\pi = [\pi_1, \pi_2, \ldots, \pi_n].

Here's an example with n=4n = 4, π=[3,1,4,2]\pi = [3, 1, 4, 2]:

  1. Determine the Position: For each element πi\pi_i, count the number of elements in the remaining list that are smaller. This count gives you the corresponding factoradic digit.
    • For π1=3\pi_1 = 3: there are 2 fewer elements 1,2{1, 2}; hence d1=2d_1 = 2.
    • For π2=1\pi_2 = 1: there are 0 fewer elements; hence d2=0d_2 = 0.
    • For π3=4\pi_3 = 4: there is 1 fewer element 2{2}; hence d3=1d_3 = 1.
    • For π4=2\pi_4 = 2: there are 0 fewer elements; hence d4=0d_4 = 0.
  2. Calculate the Factoradic: Use these did_i values:
    • Number=23!+02!+11!+00!=12+0+1+0=13\text{Number} = 2 \cdot 3! + 0 \cdot 2! + 1 \cdot 1! + 0 \cdot 0! = 12 + 0 + 1 + 0 = 13

Number -> Permutation Mapping

To convert a number back to a permutation, reverse the process using successive divisions:

  1. Initialize: Take number NN and for set 1,2,...,n{1, 2, ..., n}, iteratively determine each position.
  2. Determine Factoradic Digits: For each factorial term k!k!, determine ak=Nk!a_k = \lfloor \frac{N}{k!} \rfloor and update NNmodk!N \leftarrow N \bmod k!.
  3. Construct Permutation: Starting with a sorted list of nn numbers, choose elements based on factoradic digits aka_k, removing each chosen element from the list.

Using 1313 as an example:

  1. 13÷3!=213 \div 3! = 2 R 11 (choose 3rd smallest, remove 3{3}, result 1,2,4{1, 2, 4})
  2. 1÷2!=01 \div 2! = 0 R 11 (choose 1st smallest, remove 1{1}, result 2,4{2, 4})
  3. 1÷1!=11 \div 1! = 1 R 00 (choose 2nd smallest, remove 4{4}, result 2{2})
  4. 2{2} remains

Permutation π=[3,1,4,2]\pi = [3, 1, 4, 2].

Key Takeaways

To summarize the mapping operations from permutations to numbers and numbers to permutations efficiently:

TaskProcessExample
Permutation -> NumberCount smaller remaining elements to create a factoradic representationπ=[3,1,4,2]\pi = [3, 1, 4, 2] produces 1313
Number -> PermutationUse successive divisions to select elements by index indicated by factoradic1313 maps to π=[3,1,4,2]\pi = [3, 1, 4, 2]

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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.