-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathud_graph.py
More file actions
393 lines (348 loc) · 13.5 KB
/
Copy pathud_graph.py
File metadata and controls
393 lines (348 loc) · 13.5 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# Course: Data Structures
# Author: Hiromi Watanabe
# Assignment: 6
# Description: This file includes methods related to undirected graphs.
import collections
import heapq
from collections import deque
class UndirectedGraph:
"""
Class to implement undirected graph
- duplicate edges not allowed
- loops not allowed
- no edge weights
- vertex names are strings
"""
def __init__(self, start_edges=None):
"""
Store graph info as adjacency list
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
self.adj_list = dict()
# populate graph with initial vertices and edges (if provided)
# before using, implement add_vertex() and add_edge() methods
if start_edges is not None:
for u, v in start_edges:
self.add_edge(u, v)
def __str__(self):
"""
Return content of the graph in human-readable form
DO NOT CHANGE THIS METHOD IN ANY WAY
"""
out = [f'{v}: {self.adj_list[v]}' for v in self.adj_list]
out = '\n '.join(out)
if len(out) < 70:
out = out.replace('\n ', ', ')
return f'GRAPH: {{{out}}}'
return f'GRAPH: {{\n {out}}}'
# ------------------------------------------------------------------ #
def add_vertex(self, v: str) -> None:
"""
Add new vertex to the graph
"""
self.adj_list.setdefault(v, [])
# self.adj_list[v] = []
# print(self)
def add_edge(self, u: str, v: str) -> None:
"""
Add edge to the graph. Duplicated elements not allowed.
"""
if u == v:
return
# u and v exist. append the value if it does not exist.
elif u in self.adj_list and v in self.adj_list:
if u not in self.adj_list[v]:
self.adj_list[v].append(u)
if v not in self.adj_list[u]:
self.adj_list[u].append(v)
# empty case
elif u not in self.adj_list and v not in self.adj_list:
self.adj_list[u] = [v]
self.adj_list[v] = [u]
# only v exist. create v. append u
elif u not in self.adj_list and v in self.adj_list:
self.adj_list[u] = [v]
if u not in self.adj_list[v]:
self.adj_list[v].append(u)
else:
return
# only u exist. create u. append v
elif v not in self.adj_list and u in self.adj_list:
self.adj_list[v] = [u]
if v not in self.adj_list[u]:
self.adj_list[u].append(v)
else:
return
def remove_edge(self, v: str, u: str) -> None:
"""
Remove edge from the graph. If either (or both) vertex names
do not exist in the graph, or if there is no edge between them, the
method does nothing
"""
if v == u:
return
elif u not in self.adj_list and v not in self.adj_list:
return
elif u not in self.adj_list:
return
elif v not in self.adj_list:
return
else:
l = self.adj_list[u]
if v in self.adj_list[u]:
l.remove(v)
m = self.adj_list[v]
if u in self.adj_list[v]:
m.remove(u)
def remove_vertex(self, v: str) -> None:
"""
Remove vertex and all connected edges
"""
# delete the vertex
if v in self.adj_list.keys():
self.adj_list.pop(v)
# delete connected edges
for key in self.adj_list:
if v in self.adj_list[key]:
l = self.adj_list[key]
l.remove(v)
else:
return
def get_vertices(self) -> []:
"""
Return list of vertices in the graph (any order)
"""
keys_list = []
for key in self.adj_list:
keys_list.append(key)
return keys_list
def get_edges(self) -> []:
"""
Return list of edges in the graph (any order)
"""
keys_list = []
pair_list = []
for key in self.adj_list:
for item in self.adj_list[key]:
if item not in keys_list:
pair_list.append((key,item))
keys_list.append(key)
return pair_list
def is_valid_path(self, path: []) -> bool:
"""
Return true if provided path is valid, False otherwise.
There should not be any restrictions on repetition of vertices/edges.
"""
if len(path) == 0:
return True
if len(path) == 1:
if path[0] in self.adj_list:
return True
else:
return False
# 1. check all vertices exist in the path
for index in range(len(path) - 1):
if path[index] not in self.adj_list:
return False
# 2. check if there is a path exist from one to another
for i in range(len(path) - 1):
if path[i + 1] not in self.adj_list[path[i]]:
return False
return True
def dfs(self, v_start, v_end=None) -> []:
"""
Return list of vertices visited during DFS search
Vertices are picked in alphabetical order
"""
# Initialize an empty set of reachable vertices.
visited = []
l = []
if v_start not in self.adj_list:
return visited
# Initialize an empty stack. Add vi to the stack.
stack = collections.deque([v_start])
while len(stack) != 0:
p = stack.pop()
# the vertex is stored to visited[]
if p not in visited:
visited.append(p)
# store elements in ascending order
for i in range(len(self.adj_list[p])):
l.append(self.adj_list[p][i])
sorted_l = sorted(l)
# reset the list
l = []
# append sorted list to stack in backwards (so that it pops in ascending order)
for j in range(len(sorted_l) - 1, -1, -1):
stack.append(sorted_l[j])
# return if it reached the v_end
if p == v_end:
return visited
return visited
def bfs(self, v_start, v_end=None) -> []:
"""
Return list of vertices visited during BFS search
Vertices are picked in alphabetical order
"""
# Initialize an empty set of reachable vertices.
visited = []
l = []
if v_start not in self.adj_list:
return visited
# Initialize an empty queue. Add vi to the stack.
q = collections.deque([v_start])
while len(q) != 0:
p = q.popleft()
# the vertex is stored to visited[]
if p not in visited:
visited.append(p)
# store elements in ascending order
for i in range(len(self.adj_list[p])):
if self.adj_list[p][i] not in q:
l.append(self.adj_list[p][i])
sorted_l = sorted(l)
# reset the list
l = []
# append sorted list to queue in ascending order)
for j in range(len(sorted_l)):
q.append(sorted_l[j])
# return if it reached the v_end
if p == v_end:
return visited
return visited
def count_connected_components(self):
"""
Return number of connected components in the graph
"""
visited = []
count = 0
for i in range(len(self.adj_list)):
# transform vertex in list form
vertex_list = list(self.adj_list)
# visited vertex from dfs method is returned and add 1 to count
if vertex_list[i] not in visited:
vertices = self.dfs(vertex_list[i])
count += 1
# visited vertex is stored to visited
if len(vertices) > 0:
for j in range(len(vertices)):
if vertices[j] not in visited:
visited.append(vertices[j])
return count
def has_cycle(self):
"""
Return True if graph contains a cycle, False otherwise
"""
# Initialize an empty set of reachable vertices.
visited = []
# Initialize an empty stack. Add vi to the stack.
vertices = list(self.adj_list.keys())
# To be sure we check every single vertex.
for m in range(len(vertices) - 1):
# check that vertex has neighbors
if len(self.adj_list[vertices[m]]) != 0:
stack = collections.deque(vertices[m])
# keep track of parents (at first we can pass whatever we want)
parent_s = collections.deque('a')
while len(stack) != 0:
p = stack.pop()
parent = parent_s.pop()
# the vertex is stored to visited[]
if p not in visited:
visited.append(p)
for i in range(len(self.adj_list[p])):
if self.adj_list[p][i] not in visited:
stack.append(self.adj_list[p][i])
parent_s.append(p)
# check if the destination is already where it's been visited
# and also it is not parent
elif self.adj_list[p][i] in visited and self.adj_list[p][i] != parent and self.adj_list[p][i] != 'a':
return True
return False
if __name__ == '__main__':
# print("\nPDF - method add_vertex() / add_edge example 1")
# print("----------------------------------------------")
# g = UndirectedGraph()
# print(g)
#
# for v in 'ABCDE':
# g.add_vertex(v)
# print(g)
#
# g.add_vertex('A')
# print(g)
# for u, v in ['AE', 'AC', 'BE', 'CE', 'CD', 'CB', 'BD', 'ED', 'BH', 'QG', 'FG']:
# # for u, v in ['AB', 'AC', 'BC', 'BD', 'CD', 'CE', 'DE', ('B', 'C')]:
# g.add_edge(u, v)
# print(g)
#
#
# print("\nPDF - method remove_edge() / remove_vertex example 1")
# print("----------------------------------------------------")
# g = UndirectedGraph(['AB', 'AC', 'BC', 'BD', 'CD', 'CE', 'DE'])
# g.remove_vertex('DOES NOT EXIST')
# g.remove_edge('A', 'B')
# g.remove_edge('X', 'B')
# print(g)
# g.remove_vertex('D')
# print(g)
#
# print("\nPDF - method get_vertices() / get_edges() example 1")
# print("---------------------------------------------------")
# g = UndirectedGraph()
# print(g.get_edges(), g.get_vertices(), sep='\n')
# g = UndirectedGraph(['AB', 'AC', 'BC', 'BD', 'CD', 'CE'])
# print(g.get_edges(), g.get_vertices(), sep='\n')
# print("\nPDF - method is_valid_path() example 1")
# print("--------------------------------------")
# g = UndirectedGraph(['AB', 'AC', 'BC', 'BD', 'CD', 'CE', 'DE'])
# # test_cases = ['ECABDCBE']
# test_cases = ['ABC', 'ADE', 'ECABDCBE', 'ACDECB', '', 'D', 'Z']
# for path in test_cases:
# print(list(path), g.is_valid_path(list(path)))
# print("\nPDF - method dfs() and bfs() example 1")
# print("--------------------------------------")
# edges = ['AE', 'AC', 'BE', 'CE', 'CD', 'CB', 'BD', 'ED', 'BH', 'QG', 'FG']
# g = UndirectedGraph(edges)
# test_cases = 'ABCDEGH'
# print(g.bfs('A', 'Q'))
# for case in test_cases:
# print(f'{case} DFS:{g.dfs(case)} BFS:{g.bfs(case)}')
# print('-----')
# for i in range(1, len(test_cases)):
# v1, v2 = test_cases[i], test_cases[-1 - i]
# print(f'{v1}-{v2} DFS:{g.dfs(v1, v2)} BFS:{g.bfs(v1, v2)}')
#
# print("\nPDF - method count_connected_components() example 1")
# print("---------------------------------------------------")
# edges = ['AE', 'AC', 'BE', 'CE', 'CD', 'CB', 'BD', 'ED', 'BH', 'QG', 'FG']
# g = UndirectedGraph(edges)
# test_cases = (
# 'add QH', 'remove FG', 'remove GQ', 'remove HQ',
# 'remove AE', 'remove CA', 'remove EB', 'remove CE', 'remove DE',
# 'remove BC', 'add EA', 'add EF', 'add GQ', 'add AC', 'add DQ',
# 'add EG', 'add QH', 'remove CD', 'remove BD', 'remove QG')
# for case in test_cases:
# command, edge = case.split()
# u, v = edge
# g.add_edge(u, v) if command == 'add' else g.remove_edge(u, v)
# print(g.count_connected_components(), end=' ')
# print()
print("\nPDF - method has_cycle() example 1")
print("----------------------------------")
# edges = ['AE', 'AC', 'BE', 'CE', 'CD', 'CB', 'BD', 'ED', 'BH', 'QG', 'FG']
edges = ['FC', 'FJ', 'CF', 'CK', 'HI', 'HB', 'HE', 'IH', 'IB', 'IA', 'BI', 'BH', 'EH', 'AI']
g = UndirectedGraph(edges)
# test_cases = (
# 'add QH', 'remove FG', 'remove GQ', 'remove HQ',
# 'remove AE', 'remove CA', 'remove EB', 'remove CE', 'remove DE',
# 'remove BC', 'add EA', 'add EF', 'add GQ', 'add AC', 'add DQ',
# 'add EG', 'add QH', 'remove CD', 'remove BD', 'remove QG',
# 'add FG', 'remove GE')
# for case in test_cases:
# command, edge = case.split()
# u, v = edge
# g.add_edge(u, v) if command == 'add' else g.remove_edge(u, v)
print(g)
# print('{:<10}'.format(case), g.has_cycle())
print(g.has_cycle())