permutation_rank¶
Determining rank of a permutation and generating permutation given rank.
This implementation is due to: https://rosettacode.org/wiki/Permutations/Rank_of_a_permutation#Python
Implementation of algorithms described in [1].
References
[1] | (1, 2, 3) Myrvold, Wendy, and Frank Ruskey. “Ranking and unranking permutations in linear time.” Information Processing Letters 79.6 (2001): 281-284. |
-
mr_utils.utils.permutation_rank.
pi2rank
(pi, method='rank2', iterative=True)[source]¶ Return rank of permutation pi.
Parameters: - pi (list) – Permutation.
- method ({'rank1', 'rank2'}, optional) – Which ranking method to use.
- iterative (bool, optional) – Whether or not to use iterative or recursive version.
Returns: rank – The rank of the provided permutation.
Return type: int
Notes
The permutation pi should be a permutation of the list range(n) and contain n elements.
‘method’ should be one of {‘rank1’, ‘rank2’} corresponding to the two schemes presented in the Myrvold and Ruskey paper. There is an iterative version available for both algorithms.
Implements algorithms from [1].
-
mr_utils.utils.permutation_rank.
rank2pi
(r, n, method='rank2')[source]¶ Given rank and permutation length produce the corresponding permutation.
Parameters: - r (int) – Rank.
- n (int) – Lenth of the permutation.
- method ({'rank1', 'rank2'}) – Which ranking method to use.
Returns: pi – Associated permutation.
Return type: list
Notes
Implements algorithms from [1].