-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirected.py
More file actions
292 lines (251 loc) · 9.29 KB
/
Copy pathdirected.py
File metadata and controls
292 lines (251 loc) · 9.29 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# Course: CS261 - Data Structures
# Author:
# Assignment:
# Description:
import heapq
from collections import deque
class DirectedGraph:
"""
Class to implement directed weighted graph
- duplicate edges not allowed
- loops not allowed
- only positive edge weights
- vertex names are integers
"""
def __init__(self, start_edges=None):
"""
Store graph info as adjacency matrix
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
self.v_count = 0
self.adj_matrix = []
# populate graph with initial vertices and edges (if provided)
# before using, implement add_vertex() and add_edge() methods
if start_edges is not None:
v_count = 0
for u, v, _ in start_edges:
v_count = max(v_count, u, v)
for _ in range(v_count + 1):
self.add_vertex()
for u, v, weight in start_edges:
self.add_edge(u, v, weight)
def __str__(self):
"""
Return content of the graph in human-readable form
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
if self.v_count == 0:
return 'EMPTY GRAPH\n'
out = ' |'
out += ' '.join(['{:2}'.format(i) for i in range(self.v_count)]) + '\n'
out += '-' * (self.v_count * 3 + 3) + '\n'
for i in range(self.v_count):
row = self.adj_matrix[i]
out += '{:2} |'.format(i)
out += ' '.join(['{:2}'.format(w) for w in row]) + '\n'
out = f"GRAPH ({self.v_count} vertices):\n{out}"
return out
# ------------------------------------------------------------------ #
def add_vertex(self) -> int:
"""
add a vertex to a directed graph in the form of an adjacency matrix
"""
self.adj_matrix = [[0 for i in range(self.v_count + 1)] for j in range(self.v_count + 1)]
self.v_count += 1
return self.v_count
def add_edge(self, src: int, dst: int, weight=1) -> None:
"""
Add an edge wih an associated weight to a directed graph
"""
if not (0 <= src < self.v_count and 0 <= dst < self.v_count):
return None
else:
if weight > 0:
if src is not dst:
self.adj_matrix[src][dst] = weight
def remove_edge(self, src: int, dst: int) -> None:
"""
Remove an edge from a directed graph by setting the weight to 0
"""
if not (0 <= src < self.v_count and 0 <= dst < self.v_count):
return None
else:
if src is not dst:
self.adj_matrix[src][dst] = 0
def get_vertices(self) -> []:
"""
Return a list of vertices from a directed graph
"""
return [i for i in range(self.v_count)]
def get_edges(self) -> []:
"""
return the edges between 2 vertices and those edge's associated weights
"""
list = []
for i in range(self.v_count):
for j in range(self.v_count):
if (i and j) >= 0 and self.adj_matrix[i][j] > 0:
list.append((i, j, self.adj_matrix[i][j]))
return list
def is_valid_path(self, path: []) -> bool:
"""
return True if there is a way to get from a starting vertex to another through the given vertices
"""
if len(path) == 0:
return True
edges = self.get_edges()
for i in range(0, len(path) - 1):
if (path[i], path[i + 1], self.adj_matrix[path[i]][path[i + 1]]) not in edges:
return False
return True
def dfs(self, v_start, v_end=None) -> []:
"""
Do a depth first search on a directed graph
"""
if v_start not in range(0, self.v_count):
return []
if v_end not in range(0, self.v_count):
v_end = None
visited = []
stack = deque([v_start])
while len(stack) is not 0:
v = stack.pop()
if v not in visited:
visited += [v]
for i in range(len(self.adj_matrix[v]) - 1, -1, -1):
if self.adj_matrix[v][i] > 0:
stack.append(i)
if v == v_end:
break
return visited
def bfs(self, v_start, v_end=None) -> []:
"""
Do a breadth first search on a directed graph
"""
if v_start not in range(0, self.v_count):
return []
if v_end not in range(0, self.v_count):
v_end = None
visited = []
queue = deque([v_start])
while len(queue) is not 0:
v = queue.popleft()
if v not in visited:
visited += [v]
if v == v_end:
break
for i in range(len(self.adj_matrix[v])):
if i not in visited and self.adj_matrix[v][i] > 0:
queue.append(i)
return visited
def has_cycle(self):
"""
Check if there is a cycle in a directed graph
"""
visited = []
for i in range(self.v_count - 1):
visited += [i]
for j in self.dfs(i):
if self.adj_matrix[j][i] > 0:
return True
return False
def dijkstra(self, src: int) -> []:
"""
Return the shortest possible route from a given vertex to all other vertices in a directed graph, return 'inf'
for vertices that don't have a path from given vertex to another
"""
# initialize parent,neighbors, and shortest distance dictionaries
distance = {}
parent = {}
vertices = self.get_vertices()
for i in range(self.v_count):
distance[i] = float('inf')
parent[i] = None
distance[src] = 0
neighbors = {}
for x in vertices:
neighbors[x] = []
for y in range(len(self.adj_matrix[x])):
if self.adj_matrix[x][y] > 0:
neighbors[x].append(y)
v = src
# loop through all vertices of a directed graph, starting with the source vertex and continuing with the vertex
# with the lowest cost to the source until no vertices are left unvisited
while len(vertices) != 0:
for k in vertices:
if distance[v] > distance[k]:
v = k
vertices.remove(v)
for z in neighbors[v]:
if z in vertices:
route = distance[v] + self.adj_matrix[v][z]
if route < distance[z]:
distance[z] = route
parent[z] = v
if len(vertices) > 0:
v = vertices[0]
dijkstra = []
for a in distance.values():
dijkstra += [a]
return dijkstra
if __name__ == '__main__':
print("\nPDF - method add_vertex() / add_edge example 1")
print("----------------------------------------------")
g = DirectedGraph()
print(g)
for _ in range(5):
g.add_vertex()
print(g)
edges = [(12, 11, 9), (4, 0, 12), (1, 4, 15), (4, 3, 3),
(3, 1, 5), (2, 1, 23), (3, 2, 7)]
for src, dst, weight in edges:
g.add_edge(src, dst, weight)
print(g)
print("\nPDF - method get_edges() example 1")
print("----------------------------------")
g = DirectedGraph()
print(g.get_edges(), g.get_vertices(), sep='\n')
edges = [(0, 1, 10), (4, 0, 12), (1, 4, 15), (4, 3, 3),
(3, 1, 5), (2, 1, 23), (3, 2, 7)]
g = DirectedGraph(edges)
print(g.get_edges(), g.get_vertices(), sep='\n')
print("\nPDF - method is_valid_path() example 1")
print("--------------------------------------")
edges = [(0, 1, 10), (4, 0, 12), (1, 4, 15), (4, 3, 3),
(3, 1, 5), (2, 1, 23), (3, 2, 7)]
g = DirectedGraph(edges)
test_cases = [[0, 1, 4, 3], [1, 3, 2, 1], [0, 4], [4, 0], [], [2]]
for path in test_cases:
print(path, g.is_valid_path(path))
print("\nPDF - method dfs() and bfs() example 1")
print("--------------------------------------")
edges = [(0, 1, 10), (4, 0, 12), (1, 4, 15), (4, 3, 3),
(3, 1, 5), (2, 1, 23), (3, 2, 7)]
g = DirectedGraph(edges)
for start in range(5):
print(f'{start} DFS:{g.dfs(start)} BFS:{g.bfs(start)}')
print("\nPDF - method has_cycle() example 1")
print("----------------------------------")
edges = [(0, 1, 10), (4, 0, 12), (1, 4, 15), (4, 3, 3),
(3, 1, 5), (2, 1, 23), (3, 2, 7)]
g = DirectedGraph(edges)
edges_to_remove = [(3, 1), (4, 0), (3, 2)]
for src, dst in edges_to_remove:
g.remove_edge(src, dst)
print(g.get_edges(), g.has_cycle(), sep='\n')
edges_to_add = [(4, 3), (2, 3), (1, 3), (4, 0)]
for src, dst in edges_to_add:
g.add_edge(src, dst)
print(g.get_edges(), g.has_cycle(), sep='\n')
print('\n', g)
print("\nPDF - dijkstra() example 1")
print("--------------------------")
edges = [(0, 1, 10), (4, 0, 12), (1, 4, 15), (4, 3, 3),
(3, 1, 5), (2, 1, 23), (3, 2, 7)]
g = DirectedGraph(edges)
for i in range(5):
print(f'DIJKSTRA {i} {g.dijkstra(i)}')
g.remove_edge(4, 3)
print('\n', g)
for i in range(5):
print(f'DIJKSTRA {i} {g.dijkstra(i)}')