I searching for an algorithm which gives me the permutation count of the elements 1....n. If i define the cycle lengths.
For example n := 4
<Set of cycle lengths> -> permutation count
1,1,1,1 -> 1 read 4 cycles of length 1 leads to 1 permutation: 1,2,3,4
1,1,2 -> 5 read 2 cycles of length 1 and 1 cycle of length 2 leads to 5 permutations: 1,2,4,3, 1,4,3,2, 1,3,2,4, 2,1,3,4, 3,2,1,4,
2,2 -> 3 read 2 cycles of length 2 leads to 3 permutations: 2,1,4,3, 3,4,1,2,4,3,2,1
1,3 -> 9 read 1 cycle of length 1 and 1 cycle of length 3 leads to 9 permutations 1,3,2,4, 1,3,4,2, 1,4,2,3, 2,3,1,4, 2,4,3,1, 3,1,2,4, 3,2,4,1,4,1,3,2, 4,2,1,3,
4 -> 6 read 1 cycle of length 4 leads to 6 permutations:
2,3,4,1, 2,4,1,3, 3,1,4,2, 3,4,2,1, 4,1,2,3, 4,3,1,2
How can i compute the permutation count of a given set consisting cycle lengths? Iterating through all permutations is not an option.
For a given cycle type, we can produce a permutation with that cycle type by writing down a permutation of the list
1, ..., nand then bracketing it appropriately, according to the lengths in the cycle type, to get a permutation written in cycle notation.For example, if we want cycle type
(3, 2, 2), then the permutation1, 2, 3, 4, 5, 6, 7is bracketed as(1 2 3)(4 5)(6 7), while5, 1, 6, 2, 4, 3, 7gives(5 1 6)(2 4)(3 7).It's clear that we get all permutations of cycle type
(3, 2, 2)this way, but it's also clear that we can get each permutation in multiple different ways. There are two causes of overcounting: first, we can make a cyclic shift for any of the cycles:(5 1 6)(2 4)(3 7)is the same permutation as(1 6 5)(2 4)(3 7)or(6 5 1)(2 4)(3 7). Second, cycles of the same length can be permuted arbitrarily:(5 1 6)(2 4)(3 7)is the same permutation as(5 1 6)(3 7)(2 4). A bit of thought should convince you that these are the only possible causes of overcounting.To account for both causes of overcounting, we divide the total number of permutations by (a) the product of the cycle lengths, and also (b) the factorial of the number of cycles for any given cycle length. In the
(3, 2, 2)case: we divide by3 × 2 × 2for (a), and2!for (b), because there are two cycles of length 2.Since this is Stack Overflow, here's some Python code:
Example:
To double check correctness, we can add the counts for all cycle types of a given length
n, and check that we getn!. The cycle types are the partitions ofn. We can compute those fairly simply by a recursive algorithm. Here's some code to do that.partitionsis the function we want;bounded_partitionsis a helper.Example:
And here's the double check: the sum of all the cycle type counts, for total lengths
5,6,7and20. We get the expected results of5!,6!,7!and20!.