Skip to content

Instantly share code, notes, and snippets.

@martinr448
Last active August 16, 2018 19:27
Show Gist options
  • Save martinr448/1d53250c51be32c6363d6cd7e3dadd12 to your computer and use it in GitHub Desktop.
Save martinr448/1d53250c51be32c6363d6cd7e3dadd12 to your computer and use it in GitHub Desktop.
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