-
Notifications
You must be signed in to change notification settings - Fork 0
/
edge.py
49 lines (42 loc) · 1.62 KB
/
edge.py
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
38
39
40
41
42
43
44
45
46
47
48
49
from node import Node
class Edge:
"""
An edge is a connection between two nodes.
Edges have a unique identifier and a list of two nodes. Optionally, an edge can have a numerical weight. Edges can
also be directed, in which case it is only possible to traverse the edge in one direction, not both.
"""
n_edges = 0
def __init__(self, node1, node2, weight=1, directed=False):
"""
Create an edge with its own unique identifier
:param node1: node at first end of the edge
:param node2: node at second end of the edge
:param weight: numerical weight of the connection between node1 and node2
:param directed: if True, the edge is oriented from node1 to node 2
:return: an initialized Edge object
"""
Edge.n_edges += 1
self.ID = Edge.n_edges
self.weight = weight
self.nodes = frozenset([node1, node2])
self.directed = directed
def __eq__(self, other):
return (self.ID, self.weight, self.nodes, self.directed) == (other.ID, other.weight, other.nodes, other.directed)
def __hash__(self):
return hash(self.ID)
def copy(self):
nodes = []
for node in self.nodes:
nodes.append(node.copy())
return Edge(nodes[0], nodes[1], self.weight, self.directed)
def generate_dict(self):
nodes = []
for node in self.nodes:
nodes.append(node.generate_dict())
dictionary = {
'ID': self.ID,
'weight': self.weight,
'nodes': nodes,
'directed': self.directed
}
return dictionary