Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
13-liner: cyclotron solution in Clear category for Network Loops by przemyslaw.daniel
def find_cycle(connections):
out, stack = [], [([1], list(connections))]
while stack:
loop, connections = stack.pop()
if len(loop) != len(set(loop)):
loop = loop[loop.index(loop[-1]):]
out = loop if len(loop) > len(out) else out
continue
possible = [x for x in connections if loop[-1] in x]
for i in possible:
stack += [(loop + list(set(i)-{loop[-1]}),
[x for x in connections if x != i])]
return out
Oct. 9, 2016