-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnected_graph.py
More file actions
37 lines (33 loc) · 1.35 KB
/
connected_graph.py
File metadata and controls
37 lines (33 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class Node(object):
def __init__(self, value, edges=None):
#This is describing the Node itself
self.value = value
#This is describing all the connections of this Node
self.edges = edges or []
def connected_to(self, target):
#Create a dictionary to store all nodes/stops we have already
#been to avoid going through one stop twice
beenThere = {}
if target is None:
print("error");
#Check direct connections
if target in self.edges:
beenThere[vars(target)['value']] = True;
return True
#Check indirect connections
elif self.edges:
connections = []
for edge in self.edges:
currentEdgeValue = vars(edge)['value']
#Check if edges allow any further connections and if you have already been there
targetConnected = Node.connected_to(edge, target) and not beenThere.get(currentEdgeValue, False)
connections = targetConnected
connections.append(connections)
return any(connections)
#Check if Node equals itself because it's theorectically connected
elif Node.__eq__(self, target):
return True
else:
return False
def __eq__(self, other):
return self.value == other.value