-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
320 lines (264 loc) · 8.04 KB
/
main.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
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
import turtle
import random
import math
import time
import tkinter
"""
def write_slogan():
frame = canvas
frame.pack()
button = tkinter.Button(frame,
text="QUIT",
fg="red",
command=quit)
button.pack(side=tkinter.LEFT)
slogan = tkinter.Button(frame,
text="Hello",
command=write_slogan)
slogan.pack(side=tkinter.LEFT)
canvas.mainloop()
"""
global n, cnt, t, t2, cykle, header, last_deleted, g1, screen, window, button, var
class Graph:
def __init__(self, vertices):
self.graph = [[0 for column in range(vertices)]
for row in range(vertices)]
self.V = vertices
def is_safe(self, v, pos, path):
if self.graph[path[pos - 1]][v] == 0:
return False
for vertex in path:
if vertex == v:
return False
return True
def hamilton_util(self, path, pos):
if pos == self.V:
if self.graph[path[pos - 1]][path[0]] == 1:
return True
else:
return False
for v in range(1, self.V):
if self.is_safe(v, pos, path) is True:
path[pos] = v
if self.hamilton_util(path, pos + 1) is True:
return True
path[pos] = -1
return False
def hamilton(self):
path = [-1] * self.V
path[0] = 0
if self.hamilton_util(path, 1) is False:
return False
path.append(0)
if path not in cykle:
t2.pendown()
cykle.append(path)
t2.write("->"+str(path))
t2.penup()
t2.goto(t2.pos()[0], t2.pos()[1]-20)
return path
def check_hamilton(self, cycle):
for i in range(len(cycle)-1):
if self.graph[cycle[i]][cycle[i+1]] != 1:
return False
return True
def losuj_krawedz(self):
row = random.choice(range(len(self.graph)))
while 1 not in self.graph[row]:
row = random.choice(range(len(self.graph)))
column = random.choice(range(len(self.graph[row])))
while self.graph[row][column] == 0:
column = random.choice(range(len(self.graph[row])))
self.graph[row][column] = 0
self.graph[column][row] = 0
new = [row, column]
return new
def dodaj_krawedz(self, deleted):
if deleted is not None:
self.graph[deleted[0]][deleted[1]] = 1
self.graph[deleted[1]][deleted[0]] = 1
def gnp(n, p):
a = [[0 for _ in range(n)] for _ in range(n)]
for i in range(1, n):
for j in range(i):
if random.random() <= p:
a[i][j] = a[j][i] = 1
return a
def rysujgraf(n, a):
t.pencolor("black")
t.penup()
t.goto(300, 250)
t.write("Liczba iteracji: "+str(cnt), font=header)
r = 8
s = 150
p = 2 * 3.1415 / n
x = [s * math.sin(p * (i + 1)) for i in range(n)]
y = [s * math.cos(p * (i + 1)) for i in range(n)]
t.pencolor("#f8f8f2")
for i in range(n):
if condition is False:
exit()
for j in range(i):
if a[i][j] == 1:
t.penup()
t.goto(x[i], y[i])
t.pendown()
t.goto(x[j], y[j])
t.pencolor("black")
t.fillcolor('#50fa7b')
for i in range(n):
if condition is False:
exit()
t.pencolor("#50fa7b")
t.penup()
t.goto(x[i] + r / 2 - 3, y[i] - r / 2 - 3)
t.pendown()
t.begin_fill()
t.circle(r)
t.end_fill()
t.goto(x[i] + r / 2 - 3 + 5, y[i] - r / 2 - 3)
t.pencolor("#50fa7b")
if x[i] > 0 and y[i] > 0:
t.penup()
t.goto(x[i]+10, y[i]+10)
t.pendown()
t.write(i, font=("Arial", 15, "normal"))
elif x[i] > 0 and y[i] < 0:
t.penup()
t.goto(x[i]+15, y[i]-20)
t.pendown()
t.write(i, font=("Arial", 15, "normal"))
elif x[i] < 0 and y[i] < 0:
t.penup()
t.goto(x[i] - 25, y[i] - 25)
t.pendown()
t.write(i, font=("Arial", 15, "normal"))
else:
t.penup()
t.goto(x[i] - 10, y[i] + 15)
t.pendown()
t.write(i, font=("Arial", 15, "normal"))
def rysujhamilton(a, n):
t.speed(2)
t.pencolor('#50fa7b')
s = 150
p = 2 * 3.1415 / n
x = [s * math.sin(p * (i + 1)) for i in range(n)]
y = [s * math.cos(p * (i + 1)) for i in range(n)]
for i in range(0, len(a)-1):
if condition is False:
exit()
t.penup()
t.goto(x[a[i]], y[a[i]])
t.st()
t.pendown()
t.goto(x[a[i+1]], y[a[i+1]])
t.ht()
t.speed(10)
def komunikat(typ, ham, pre):
t.penup()
t.goto(0, 300)
t.pendown()
t.pencolor('#8be9fd')
if ham is False:
t.write('Brak cyklu hamiltona', font=('Courier', 20, 'italic'), align='center')
return
if len(ham) <= 11:
style = ('Courier', 20, 'italic')
elif len(ham) <= 18:
style = ('Courier', 17, 'italic')
else:
style = ('Courier', 12, 'italic')
if typ is False:
t.write('Brak cyklu hamiltona', font=style, align='center')
else:
t.write(pre+str(ham), font=style, align='center')
def usun_krawedz(a, n):
t.speed(2)
t.pencolor("#ff5555")
s = 150
p = 2 * 3.1415 / n
x = [s * math.sin(p * (i + 1)) for i in range(n)]
y = [s * math.cos(p * (i + 1)) for i in range(n)]
t.penup()
t.goto(x[a[0]], y[a[0]])
t.st()
t.pendown()
t.goto(x[a[1]], y[a[1]])
t.ht()
t.speed(10)
condition = True
def loop():
global n, last_deleted, cnt, button, var
while condition:
found = False
cycle = None
if last_deleted is not None:
screen.tracer(0)
rysujgraf(n, g1.graph)
screen.update()
screen.tracer(1)
for i in cykle:
if g1.check_hamilton(i) is True:
komunikat(True, i, "Poprzedni cykl ->")
rysujhamilton(i, n)
found = True
break
if found is False:
cycle = g1.hamilton()
if cycle is False:
komunikat(False, cycle, "")
elif cycle is not None:
komunikat(True, cycle, "Nowy cykl -> ")
rysujhamilton(cycle, n)
g1.dodaj_krawedz(last_deleted)
last_deleted = g1.losuj_krawedz()
time.sleep(1)
usun_krawedz(last_deleted, n)
time.sleep(1)
button.wait_variable(var)
t.clear()
cnt += 1
def stop():
global condition
condition = False
exit()
def main_code(vertices, p):
global n, cnt, t, t2, cykle, header, last_deleted, g1, screen, window, var, button
n = vertices
cykle = []
cnt = 1
header = ('Courier', 12, 'italic')
window = tkinter.Tk()
window.resizable(False, False)
window.title("Poszukiwanie cykli Hamiltona w grafie ze zmiennymi krawędziami")
canvas = tkinter.Canvas(window, width=1100, height=800)
canvas.pack()
screen = turtle.TurtleScreen(canvas)
screen.bgcolor("#44475a")
var = tkinter.IntVar()
button = tkinter.Button(window, bg="#282a36", fg="#f8f8f2", text="Kolejna iteracja", width=34, height=3, font="Arial, 20", command=lambda: var.set(1))
button.pack(side=tkinter.LEFT)
exit_button = tkinter.Button(window, bg="#282a36", fg="#f8f8f2", text="Zakończ program", width=34, height=3, font="Arial, 20", command=stop)
exit_button.pack(side=tkinter.RIGHT)
t2 = turtle.RawTurtle(screen)
t2.ht()
t2.penup()
t2.goto(-500, 250)
t2.write("Lista wykorzystanych cykli:", font=header)
t2.goto(-500, 230)
t2.st()
a = gnp(n, p)
g1 = Graph(n)
g1.graph = a
t = turtle.RawTurtle(screen)
t.ht()
t.width(2)
t.speed(0.1)
t.penup()
t.goto(-200, 0)
t.pendown()
rysujgraf(n, g1.graph)
last_deleted = None
loop()
main_code(10,0.5)