Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Network Loops by TovarischZhukov
from copy import copy
def find(data, current, start, path):
retval=[]
for val in data[current]:
n_data=copy(data)
if current in data:
a=set(n_data[val])
a.remove(current)
n_data[val]=a
a=set(n_data[current])
a.remove(val)
n_data[current]=a
if val not in path:
d=find(n_data,val,start,path+[val])
if d: retval.append(d)
else:
pos=path.index(val)
retval.append(path[pos:]+[val])
return max(retval,key=len) if retval else []
def find_cycle(connections):
dctn={}
for val in connections:
if val[1] not in dctn: dctn[val[1]]=set()
dctn[val[1]].add(val[0])
if val[0] not in dctn: dctn[val[0]]=set()
dctn[val[0]].add(val[1])
return find(dctn,1,1,[1])
Jan. 21, 2016