-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbellman_kalaba.py
159 lines (127 loc) · 4.05 KB
/
bellman_kalaba.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
import tkinter
from tkinter import ttk
import time
from json.encoder import INFINITY as inf
graphe = {}
graphe_arc = {}
def d_sommet(x0, y0):
rayon = 15
id_sommet = zone_d.create_oval(
x0 - rayon, y0 - rayon, x0 + rayon, y0 + rayon, fill="white", activefill="blue"
)
zone_d.create_text(
x0,
y0,
text=str(id_sommet),
justify="center",
anchor="c",
font=("courier", 10),
fill="black",
)
graphe[id_sommet] = [id_sommet]
graphe_arc[(id_sommet, id_sommet)] = [0, None]
def clic_cercle(event):
clic_x, clic_y = event.x, event.y
d_sommet(clic_x, clic_y)
def kalaba(*args):
arr = int(entry_arr.get())
etape_list = list()
etape_dist = list()
dist = {}
succ = {}
dist[arr] = 0
succ[arr] = None
for key in graphe:
if key != arr:
dist[key] = inf
succ[key] = None
etape_list.append(succ)
etape_dist.append(dist)
toDo = [arr]
etape = 0
while toDo != []:
etape = etape + 1
x = toDo.pop()
zone_d.itemconfig(x, fill="yellow")
for y in graphe[x]:
if dist[x] + graphe_arc[(y, x)][0] < dist[y]:
dist[y] = dist[x] + graphe_arc[(y, x)][0]
succ[y] = x
toDo.append(y)
zone_d.itemconfig(x, fill="white")
etape_list.append(succ)
etape_dist.append(dist)
key = 1
while succ[key] != None:
zone_d.itemconfig(key, fill="yellow")
zone_d.itemconfig(graphe_arc[(key, succ[key])][1], fill="red")
key = succ[key]
zone_d.itemconfig(arr, fill="yellow")
print("terminer")
for k in range(etape):
print(etape_list[k])
print(etape_dist[k])
def arc(*arg):
global v
dep = int(entr_deb.get())
arr = int(entr_arr.get())
pds = eval(entr_poids.get())
a = zone_d.coords(dep)
b = zone_d.coords(arr)
if pds == "":
pds = 0
else:
pds = int(pds)
dep_x = (a[0] + a[2]) // 2
dep_y = (a[1] + a[3]) // 2
arr_x = (b[0] + b[2]) // 2
arr_y = (b[1] + b[3]) // 2
arc = zone_d.create_line(
dep_x, dep_y, arr_x, arr_y, fill="black", width=0.5, smooth=1, arrow="last"
)
zone_d.create_text(
(arr_x + dep_x) // 2,
(dep_y + arr_y) // 2,
text=pds,
justify="center",
anchor="c",
fill="white",
)
graphe_arc[dep, arr] = [pds, arc]
graphe[arr].append(dep)
fen_p = tkinter.Tk() # La fenetre principale
dessin_c = tkinter.LabelFrame(fen_p, text="Sommet") # Cadre contenant la zone de dessin
zone_d = tkinter.Canvas(
dessin_c, bg="gray", width=600, height=500
) # Canvas pour dessiner le graphe
zone_d.bind("<Button-1>", clic_cercle) # Binding : surveillance des clics
# Cadre permettant de saisir les arcs de la graphe
arc_entry = tkinter.LabelFrame(dessin_c, text="Arc")
entr_deb_l = tkinter.Label(arc_entry, text="Depart")
entr_arr_l = tkinter.Label(arc_entry, text="Arrive")
entr_poids_l = tkinter.Label(arc_entry, text="Poids")
entr_deb = tkinter.Entry(arc_entry, width=5)
entr_arr = tkinter.Entry(arc_entry, width=5)
entr_poids = tkinter.Entry(arc_entry, width=5)
btn_val = tkinter.Button(arc_entry, text="Valider", command=arc)
error_str = tkinter.StringVar()
entr_deb_l.grid(row=1, column=1)
entr_arr_l.grid(row=1, column=2)
entr_poids_l.grid(row=1, column=3)
entr_deb.grid(row=2, column=1)
entr_arr.grid(row=2, column=2)
entr_poids.grid(row=2, column=3)
btn_val.grid(row=2, column=4)
# Cadre pour demarre l'algorithme
debut = tkinter.LabelFrame(fen_p)
msg = tkinter.Label(debut, text="Saisir le but de votre graphe")
entry_arr = tkinter.Entry(debut)
kalaba_btn = tkinter.Button(debut, text="Demmrer l'algorithme", command=kalaba)
msg.pack()
entry_arr.pack()
kalaba_btn.pack()
dessin_c.pack()
zone_d.grid(row=1, column=1, sticky="nw")
arc_entry.grid(row=2, column=1, sticky="nw")
debut.pack()
fen_p.mainloop()