Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First - Brute force using recursion solution in Clear category for Network Loops by carel.anthonissen
def find_cycle(connections):
neighbours = lambda k,c: [n[0] if n[1]==k else n[1] for n in c if k in n]
remove_node = lambda k,c: [n for n in c if k not in n]
longest_cycle = []
def recursive_cycle_search(cons,partial):
nonlocal longest_cycle
if (len(partial) > len(longest_cycle)-1) and (partial[0] in neighbours(partial[-1],connections)):
longest_cycle = partial+[partial[0]]
for n in neighbours(partial[-1],cons):
recursive_cycle_search(remove_node(partial[-1],cons),partial+[n])
nodes = {node for edge in connections for node in edge}
for n in nodes:
recursive_cycle_search(connections,[n])
connections = remove_node(n,connections)
if len(longest_cycle) < 4: longest_cycle = []
return longest_cycle
May 9, 2017