Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Network Loops by UFO665
# migrated from python 2.7
def paths(dct, begin, current, lstPath=[], lstPaths=[]):
lstPath.append(current)
for related in dct[current]:
if related == begin and len(lstPath) > 2:
lstPaths.append(list(lstPath))
if related not in lstPath:
lstPaths = paths(dct, begin, related, lstPath, lstPaths)
lstPath.pop()
return lstPaths
def find_cycle(connections):
dct = {}
for conn in connections:
a, b = conn
if a not in dct:
dct[a] = []
if b not in dct:
dct[b] = []
dct[a].append(b)
dct[b].append(a)
key, lenVal = connections[0][0], 0
for k, v in dct.items():
if len(v) > lenVal:
lenVal = len(v)
key = k
lstPaths = sorted(paths(dct, key, key, [], []), key=lambda x: len(x))
return tuple(lstPaths[-1] + [lstPaths[-1][0]]) if lstPaths else []
Feb. 15, 2016