Files
TermNSI/Routage/dijkstra.py

44 lines
1.2 KiB
Python

G = (["A","B","C","D","E","F","G"],
[("A","B",1),("A","D",2),("A","C",3),
("B","D",5),("B","E",1),("C","D",4),
("C","F",1),("D","E",1),("D","F",1),
("D","G",2),("E","G",3),("F","G",4)])
IND = {"A":0,
"B":1,
"C":2,
"D":3,
"E":4,
"F":5,
"G":6}
def select_min(t1, t2):
mini = IND[t2[0]]
for s in t2[1:]:
if t1[mini] > t1[IND[s]]:
mini = IND[s]
return mini
def dijkstra(g, s):
res = [float('inf') for _ in g[0]]
res[IND[s]] = 0
a_faire = [e for e in g[0]]
while a_faire != []:
ind_courrant = select_min(res, a_faire)
sommet_courrant = g[0][ind_courrant]
a_faire.remove(sommet_courrant)
cout_courrant = res[ind_courrant]
for l in g[1]:
if l[0]==sommet_courrant:
cout_tmp=l[2]+cout_courrant
if res[IND[l[1]]]>=cout_tmp:
res[IND[l[1]]] = cout_tmp
return res
G2 = (["A","B","C","D","E"],
[("A","B",4),("A","C",3),("A","D",2),
("B","E",1),("C","D",1),("C","E",1),("D","E",1)])