- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
Latest commit
315 lines (241 loc) · 9.34 KB
/
Copy pathgraph.py
File metadata and controls
315 lines (241 loc) · 9.34 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
# graph.py
# imports
importos
importmath
importtime
importjson
importpickle
importrandom
importpygame
fromcollectionsimportdeque
importcopy
# globals
fromuiimportUI
fromglobyimport*
# graph tools
fromgraph_toolsimport*
# pygame gui
importpygame_gui
frompygame_gui.elementsimportUILabel
frompygame_gui.elementsimportUIImage
frompygame_gui.elementsimportUIPanel
frompygame_gui.elementsimportUIWindow
frompygame_gui.elementsimportUIButton
frompygame_gui.elementsimportUITextBox
frompygame_gui.elementsimportUIDropDownMenu
frompygame_gui.windowsimportUIMessageWindow
frompygame_gui.elementsimportUISelectionList
frompygame_gui.elementsimportUITextEntryLine
frompygame_guiimportUIManager, PackageResource
frompygame_gui.elementsimportUIHorizontalSlider
frompygame_gui.elementsimportUIScreenSpaceHealthBar
classGraphSearch:
def__init__(self, ui, s=Options.slider_default):
self.ui=ui
self.size=s
self.stored_graphs=None
withopen(Options.graph_storage_file, "rb") asf:
self.stored_graphs=pickle.load(f)
self.running=True
self.delay=0
# drawing
self.radius=0
self.draw_array= []
# for drawing
self.current_vertices= {}
self.current_edges= {}
self.set_size(s)
# bfs
self.parents= {}
defset_size(self, s):
self.size=s
self.reset()
defreset_vertices(self):
forvertexinself.current_vertices:
self.current_vertices[vertex] =0
self.current_vertices[self.graph.start_vertex] =1
self.current_vertices[self.graph.end_vertex] =2
defreset_edges(self):
self.current_edges= {e: 0foreinself.graph.get_edges()}
defreset_colours(self):
self.reset_vertices()
self.reset_edges()
defreset(self):
self.graph=self.stored_graphs[self.size]
self.current_vertices= {vertex: 0forvertexinrange(len(self.graph))}
self.current_edges= {e: 0foreinself.graph.get_edges()}
self.current_vertices[self.graph.start_vertex] =1
self.current_vertices[self.graph.end_vertex] =2
defrender(self):
# print edges
foredgeinself.current_edges:
colour=Options.edge_c_map[self.current_edges[edge]]
i, j=edge
p1=self.graph.get_vertex_center(i)
p2=self.graph.get_vertex_center(j)
pygame.draw.line(self.ui.get_window(), colour, p1, p2)
# print vertices
forvertexinself.current_vertices:
colour=Options.vertex_c_map[self.current_vertices[vertex]]
pygame.draw.circle(self.ui.get_window(), colour,
self.graph.get_vertex_center(vertex), self.graph.get_radius())
pygame.display.update()
ifself.delay!=0: pygame.time.delay(self.delay)
defprocess_slider(self, x):
new_size=x
ifself.size!=new_size:
self.set_size(new_size)
defprocess_button(self, state):
ifstate==True:
self.ui.disable()
self.ui.disable_extras()
self.start=True
defprocess_compute(self, state):
# if state == True:
# self.ui.enable_start()
# self.reset_vertices()
# self.current_vertices[self.from_vertex] = 1
# self.current_vertices[self.to_vertex] = 2
pass
defprocess_inputs(self, input_1, input_2):
# if input_1.strip() == "" or input_2.strip() == "":
# self.ui.disable_compute()
# self.ui.disable_start()
# # print(1)
# return
# a = int(input_1)
# b = int(input_2)
# if a == b:
# self.ui.disable_compute()
# self.ui.disable_start()
# # print(2)
# return
# if a not in self.current_vertices or b not in self.current_vertices:
# self.ui.disable_compute()
# self.ui.disable_start()
# # print(3)
# return
# if a != self.from_vertex or b != self.to_vertex:
# self.ui.disable_start()
# # print(4)
# self.from_vertex = a
# self.to_vertex = b
# self.ui.enable_compute()
pass
defupdate(self):
# process events
foreventinpygame.event.get():
ifevent.type==pygame.QUIT:
self.running=False
pygame.quit()
exit()
self.ui.manager.process_events(event)
# process ui events
self.process_slider(self.ui.get_slider_value())
self.process_inputs(self.ui.get_input_1(), self.ui.get_input_2())
self.process_button(self.ui.button_pressed())
self.process_compute(self.ui.compute_pressed())
# update-render
self.ui.update()
self.ui.render()
defupdate_render(self):
self.update()
self.render()
defbfs(self):
self.parents= {self.graph.start_vertex: None}
visited= {self.graph.start_vertex}
queue= [self.graph.start_vertex]
whilelen(queue) >0:
vertex=queue.pop(0)
# visualize
self.current_vertices[vertex] =3
self.update_render()
foradjacentinself.graph.get_adjacent(vertex):
ifadjacentnotinvisited:
visited.add(adjacent)
# visualize
self.current_vertices[adjacent] =4
self.current_edges[(vertex, adjacent)] =1
self.update_render()
self.parents[adjacent] =vertex
queue.append(adjacent)
ifvertex==self.graph.start_vertex:
self.current_vertices[vertex] =1
elifvertex==self.graph.end_vertex:
self.current_vertices[vertex] =2
break
else:
self.current_vertices[vertex] =5
self.update_render()
# show shortest path
current_vertex=self.graph.end_vertex
whilecurrent_vertex!=Noneandcurrent_vertex!=self.graph.start_vertex:
# visualize
edge=tuple(sorted((current_vertex, self.parents[current_vertex])))
self.current_edges[(self.parents[current_vertex], current_vertex)] =2
ifcurrent_vertex!=self.graph.end_vertex:
self.current_vertices[current_vertex] =6
self.update_render()
current_vertex=self.parents[current_vertex]
defdfs(self):
visited= {self.graph.start_vertex}
stack= [self.graph.start_vertex]
self.parents= {self.graph.start_vertex: None}
whilelen(stack) >0:
# visualize
vertex=stack.pop() # pop -1
visited.add(vertex)
self.current_vertices[vertex] =3
self.update_render()
foradjacentinreversed(self.graph.get_adjacent(vertex)):
ifadjacentnotinvisited:
# visualize
self.current_vertices[adjacent] =4
self.current_edges[(vertex, adjacent)] =1
self.update_render()
self.parents[adjacent] =vertex
stack.append(adjacent)
# visualize
ifvertex==self.graph.start_vertex:
self.current_vertices[vertex] =1
elifvertex==self.graph.end_vertex:
self.current_vertices[vertex] =2
break
else:
self.current_vertices[vertex] =5
self.update_render()
# show found path
current_vertex=self.graph.end_vertex
whilecurrent_vertex!=Noneandcurrent_vertex!=self.graph.start_vertex:
# visualize
edge=tuple(sorted((current_vertex, self.parents[current_vertex])))
self.current_edges[(self.parents[current_vertex], current_vertex)] =2
ifcurrent_vertex!=self.graph.end_vertex:
self.current_vertices[current_vertex] =6
self.update_render()
current_vertex=self.parents[current_vertex]
# https://www.baeldung.com/cs/dfs-vs-bfs-vs-dijkstra#tracing-the-path-in-iterative-depth-first-search
defrun(self) ->bool:
self.running=True
self.start=False
self.ui.disable_extras()
whileself.runningandself.ui.get_dropdown_topic() ==Options.topics[1]:
self.update()
# visualize
ifnotself.start:
self.render()
else:
ifself.ui.get_dropdown_algo() ==Options.graph_options[0]:
self.reset_colours()
# self.delay = int((Options.frame_rate // Options.graph_fps_delay_ratio) * (1))
self.dfs()
elifself.ui.get_dropdown_algo() ==Options.graph_options[1]:
self.reset_colours()
# self.delay = int((Options.frame_rate // Options.graph_fps_delay_ratio) * (1))
self.bfs()
self.delay=0
# restart ui
self.ui.enable()
self.start=False
self.running=True
returnself.running