Created
July 28, 2014 16:30
-
-
Save sansarun/3a58a7b7826933c279b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def toDict(network): | |
result = {} | |
for c in network: | |
left, right = c.split("-") | |
if left in result: | |
result[left].append(right) | |
else: | |
result[left] = [right] | |
if right in result: | |
result[right].append(left) | |
else: | |
result[right] = [left] | |
return result | |
def check_connection(network, first, second): | |
networkDict = toDict(network) | |
previousNodes = set(); previousNodes.add(first) | |
for i in range(0, len(network)): | |
currentNodes = set() | |
for node in previousNodes: | |
for adj in networkDict[node]: | |
currentNodes.add(adj) | |
if second in currentNodes: | |
return True | |
previousNodes = currentNodes | |
return False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment