Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Graph search solution in Speedy category for How to Find Friends by ale1ster
def check_connection(net, fst, snd):
net = [(f, t) for f,t in map(lambda s: s.split(sep='-'), net)]
netd = dict()
for el in net:
netd[el[0]] = netd.setdefault(el[0], []) + [el[1]]
netd[el[1]] = netd.setdefault(el[1], []) + [el[0]]
(stack, visited) = (set(netd[fst]), set([fst]))
while stack != set():
e = stack.pop()
if e == snd:
break
if e not in visited:
visited.add(e)
stack.update(netd[e])
else:
return False
return True
June 4, 2014
Comments: