Crates.io | derangement |
lib.rs | derangement |
version | 0.1.3 |
source | src |
created_at | 2021-11-17 15:42:11.350849 |
updated_at | 2021-11-18 22:24:40.814647 |
description | A Rust implementation of a permutation with no fixed points, a derangement |
homepage | https://github.com/ray33ee/derangement |
repository | https://github.com/ray33ee/derangement |
max_upload_size | |
id | 483458 |
size | 17,577 |
Library used to generate a random derangement and map values, calculate the inverse and get the underlying map
This library was created for a specific project in mind, but any issues, improvements or suggestions would be appreciated!
A derangement is a permutation with no fixed points
We can think of a permutation just a rearrangement of a set of values. So if we have the list [0, 1, 2] then a permutation of this would be [0, 2, 1].
To get from the former to the latter, we note that 0 maps to 0, 1 maps to 2 and 2 maps to 1. We can write this as
(0 1 2)
(0 2 1)
Where the number on top maps to the number underneath. We can also write this in cyclic form, where each number maps to the next in the cycle, and cycles are separated by brackets:
(0)(1 2)
Also note that the order if a cycle is the number of elements in it (so the order of (0) is 1, and the order of (1 2) is 2)
If an element maps to itself, it is a fixed point. It also is within a cycle of order 1 (like 0 in the above example)
The most important aspect of the derangement algorithm is noting that a derangement is a permutation group where all the cycles have order greater than 2. This means we can use this fact to partition a set of numbers into cycles with an order greater than 2.
So here we define an algorithm to randomly generate derangement:
Each time we partition the list, is essential that we do not allow any cycles of order 1, and therefore prevent fixed points. So if we have n
elements left to partition then the next partition can be in the range [2, n-1) ∩ { n }
.
We cannot allow a partition of n-1
as this would leave one element which would become a fixed point.