def check_connection(network, first, second):
elements = set()
future = []
neighbours = {}
for i in network:
for m in i.split('-'):
elements.add(m)
for i in elements:
neighbours[i]=set()
for element in elements:
for i in network:
if element in i.split('-'):
for k in i.split('-'):
neighbours[element].add(k)
if first in neighbours:
for i in neighbours[first]:
if i == second:
return True
else:
future.append(i)
while len(future) != 0:
for i in future[:]:
for g in neighbours[i]:
if g == second:
return True
else:
future.append(g)
future.remove(i)
else:
return False

Created at: 2015/08/07 18:35; Updated at: 2015/08/08 09:57