Created
March 27, 2012 02:25
-
-
Save begriffs/2211881 to your computer and use it in GitHub Desktop.
Decompose a permutation into disjoint cycles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def cycles(perm): | |
remain = set(perm) | |
result = [] | |
while len(remain) > 0: | |
n = remain.pop() | |
cycle = [n] | |
while True: | |
n = perm[n] | |
if n not in remain: | |
break | |
remain.remove(n) | |
cycle.append(n) | |
result.append(cycle) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Below how I did it in the repo LABelsToolkit on github.
Not sure it is more efficient that the previous solution.
I tried (unsuccessfully) to implement it recursively, than I tried (again unsuccessfully) to make only the bottleneck
merge_decoupled_permutation
recursive.