Last active
August 16, 2018 19:27
-
-
Save martinr448/1d53250c51be32c6363d6cd7e3dadd12 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 from_permutation_to_disjoints_cycles(perm): | |
mappings = { a: b for a, b in zip(*perm) } | |
cycles = [] | |
for a in perm[0]: | |
b = mappings.pop(a, None) | |
if b is None: | |
continue # `a` has already been handled | |
cycle = [a] | |
while a != b: | |
cycle.append(b) | |
b = mappings.pop(b) | |
cycles.append(cycle) | |
return cycles |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment