I have a python3 program that gives cycles for a given permutation. I want to know how to find a children out of it.
This is what I have to create a cycles;
Thanks for your input.
This is what I have to create a cycles;
Code:
def to_cycles(perm):
pi = {i+1: perm[i] for i in range(len(perm))}
cycles = []
while pi:
elem0 = next(iter(pi)) # arbitrary starting element
this_elem = pi[elem0]
next_item = pi[this_elem]
cycle = []
while True:
cycle.append(this_elem)
del pi[this_elem]
this_elem = next_item
if next_item in pi:
next_item = pi[next_item]
else:
break
cycles.append(cycle)
return cycles
aa = to_cycles([4,1,6,2,3,5,8,9,7,10])
print("array: ", aa)
dicts = {}
keys = range(0, len(aa)-1)
for i in keys:
dicts[i] = aa[i]
print(dicts)