-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.py
100 lines (81 loc) · 3.41 KB
/
graph.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
class Node(object):
def __init__(self, value):
self.value = value
self.edges = []
class Edge(object):
def __init__(self, value, node_from, node_to):
self.value = value
self.node_from = node_from
self.node_to = node_to
class Graph(object):
def __init__(self, nodes=[], edges=[]):
self.nodes = nodes
self.edges = edges
def insert_node(self, new_node_val):
new_node = Node(new_node_val)
self.nodes.append(new_node)
def insert_edge(self, new_edge_val, node_from_val, node_to_val):
from_found = None
to_found = None
for node in self.nodes:
if node_from_val == node.value:
from_found = node
if node_to_val == node.value:
to_found = node
if from_found == None:
from_found = Node(node_from_val)
self.nodes.append(from_found)
if to_found == None:
to_found = Node(node_to_val)
self.nodes.append(to_found)
new_edge = Edge(new_edge_val, from_found, to_found)
from_found.edges.append(new_edge)
to_found.edges.append(new_edge)
self.edges.append(new_edge)
def get_edge_list(self):
"""Don't return a list of edge objects!
Return a list of triples that looks like this:
(Edge Value, From Node Value, To Node Value)"""
edge_list = []
for edge in self.edges:
edge_list.append((edge.value, edge.node_from.value, edge.node_to.value))
return edge_list
def get_adjacency_list(self):
"""Don't return any Node or Edge objects!
You'll return a list of lists.
The indecies of the outer list represent
"from" nodes.
Each section in the list will store a list
of tuples that looks like this:
(To Node, Edge Value)"""
adjacency_list = [None]*(len(self.nodes)+1)
for node in self.nodes:
for edge in node.edges:
if adjacency_list[edge.node_from.value] == None:
adjacency_list[edge.node_from.value] = []
edge_tup = (edge.node_to.value, edge.value)
if edge_tup not in adjacency_list[edge.node_from.value]:
adjacency_list[edge.node_from.value].append(edge_tup)
return adjacency_list
def get_adjacency_matrix(self):
"""Return a matrix, or 2D list.
Row numbers represent from nodes,
column numbers represent to nodes.
Store the edge values in each spot,
and a 0 if no edge exists."""
adjacency_matrix = [[0 for i in range(len(self.nodes) + 1)] \
for j in range(len(self.nodes) + 1)]
for edge in self.edges:
adjacency_matrix[edge.node_from.value][edge.node_to.value] = edge.value
return adjacency_matrix
graph = Graph()
graph.insert_edge(100, 1, 2)
graph.insert_edge(101, 1, 3)
graph.insert_edge(102, 1, 4)
graph.insert_edge(103, 3, 4)
# Should be [(100, 1, 2), (101, 1, 3), (102, 1, 4), (103, 3, 4)]
print(graph.get_edge_list())
# Should be [None, [(2, 100), (3, 101), (4, 102)], None, [(4, 103)], None]
print(graph.get_adjacency_list())
# Should be [[0, 0, 0, 0, 0], [0, 0, 100, 101, 102], [0, 0, 0, 0, 0], [0, 0, 0, 0, 103], [0, 0, 0, 0, 0]]
print(graph.get_adjacency_matrix())