-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
218 lines (167 loc) · 6.24 KB
/
graph.py
File metadata and controls
218 lines (167 loc) · 6.24 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
"""
graph.py
Implemented in lectures in CMPUT274 taught by Michael Bowman, Walter Bischov,
Leah Hackman & Zack friggstadt. Updated by Parash Rahman & Jacob Denson.
"""
class Graph:
"""
Implements a graph class with standard features expected in a graph, like
searching, pathfinding, and finding the tangent line at a specific point ;)
"""
def __init__(self, vertices = [], edges = [], is_directed = True):
"""
Given a list of vertices and edges (a list of tuples of vertex pairs),
this function creates a graph with a given set of vertices and edges.
If no vertices and edges are specified, an empty graph is created. One
can also specify whether the graph is directed or not, which by default
is directed.
>>> a = Graph()
>>> a.adjacency_dict == {}
True
>>> b = Graph([1,2,3], [(1,2), (2,3)], True)
>>> b.adjacency_dict
{1: {2}, 2: {3}, 3: set()}
"""
self.is_directed = is_directed
self.adjacency_dict = {}
for vertex in vertices:
self.add_vertex(vertex)
for vertex_from, vertex_to in edges:
self.add_edge(vertex_from, vertex_to)
def is_vertex(self, vertex):
"""
Returns true if the given vertex is in the graph.
>>> a = Graph([1,2])
>>> a.is_vertex(1)
True
>>> a.is_vertex(3)
False
"""
return vertex in self.adjacency_dict.keys()
def is_edge(self, vertex_from, vertex_to):
"""
Returns true if the specified edge is in the graph.
>>> a = Graph([1,2], [(1,2)])
>>> a.is_edge(1,2)
True
>>> a.is_edge(2,1)
False
>>> a.is_edge(3,1)
False
"""
# If the first vertex isn't even in the graph, the edge itself can't be.
if not self.is_vertex(vertex_from):
return False
return vertex_to in self.adjacency_dict[vertex_from]
def vertices(self):
"""
Returns a copy of the set of vertices in the graph.
>>> a = Graph([1,2,3], [(1,2), (2,3)])
>>> a.vertices() == {1,2,3}
True
"""
return set(self.adjacency_dict.keys())
def edges(self):
"""
Returns a list of edges in the graph.
>>> a = Graph([1,2,3], [(1,2), (2,3)])
>>> a.edges()
[(1, 2), (2, 3)]
>>> b = Graph()
>>> b.edges()
[]
"""
edges = [(vertex_from, vertex_to)
for vertex_from in self.adjacency_dict.keys()
for vertex_to in self.adjacency_dict[vertex_from]]
return edges
def add_vertex(self, vertex):
"""rom].add(v
Adds a vertex to the graph.
>>> a = Graph()
>>> a.add_vertex(1)
>>> a.vertices()
{1}
>>> a.add_vertex(2)
>>> a.vertices()
{1, 2}
"""
# Adding a vertex twice to the graph may have been made in error
# made in error, we specifically let the user know.
if self.is_vertex(vertex):
raise ValueError("Vertex {} is already in graph".format(vertex))
self.adjacency_dict[vertex] = set()
def add_edge(self, vertex_from, vertex_to):
"""
Given a tuple of 2 vertices, adds an edge between them. If the graph is
undirected, both directions of edge are added. If the graph is directed,
an edge from the first to the second is added. Two edges cannot exist
between the same two nodes.
>>> a = Graph([1,2])
>>> a.adjacency_dict
{1: set(), 2: set()}
>>> a.add_edge(1,2)
>>> a.adjacency_dict
{1: {2}, 2: set()}
>>> b = Graph([1,2], is_directed = False)
>>> b.add_edge(1,2)
>>> b.adjacency_dict
{1: {2}, 2: {1}}
"""
if not self.is_vertex(vertex_from):
raise ValueError("Vertex {} is not in graph".format(vertex_1))
if not self.is_vertex(vertex_to):
raise ValueError("Vertex {} is not in graph".format(vertex_2))
if self.is_edge(vertex_from, vertex_to):
raise ValueError("Edge {} already in graph".format(edge))
if self.is_directed:
self.adjacency_dict[vertex_from].add(vertex_to)
else:
self.adjacency_dict[vertex_from].add(vertex_to)
self.adjacency_dict[vertex_to].add(vertex_from)
def remove_vertex(self, vertex):
"""
Removes a vertex from the graph, also removing all edges connected
to the edge and from the edge in the graph.
>>> a = Graph([1,2,3], [(1,2), (2,3),(3,1)], False)
>>> a.remove_vertex(1)
>>> a.adjacency_dict
{2: {3}, 3: {2}}
"""
for other_vertex in self.adjacency_dict[vertex]:
self.adjacency_dict[other_vertex].discard(vertex)
self.adjacency_dict.pop(vertex)
def remove_edge(self, vertex_from, vertex_to):
"""
Removes an edge from the graph
>>> a = Graph([1,2], [(1,2),(2,1)])
>>> a.remove_edge(1,2)
>>> a.edges()
[(2, 1)]
>>> b = Graph([1,2], [(1,2)])
>>> b.remove_edge(1,2)
>>> b.edges()
[]
"""
if not self.is_vertex(vertex_from):
raise ValueError("Vertex {} is not in the graph".format(vertex_from))
if not self.is_vertex(vertex_to):
raise ValueError("Vertex {} is not in the graph".format(vertex_to))
if not self.is_edge(vertex_from, vertex_to):
raise ValueError("""Edge from {} to {} does
not exist""".format(vertex_from, vertex_to))
if self.is_directed:
self.adjacency_dict[vertex_from].remove(vertex_to)
else:
self.adjacency_dict[vertex_from].remove(vertex_to)
self.adjacency_dict[vertex_to].remove(vertex_from)
def neighbours(self, vertex):
"""
Given a vertex, returns a list of vertices reachable from that vertex.
>>> g = Graph([1,2,3], [(1,2), (1,3)])
>>> g.neighbours(1)
[2, 3]
"""
if vertex not in self.adjacency_dict.keys():
raise ValueError("Vertex {} is not in graph".format(vertex))
return list(self.adjacency_dict[vertex])