TP: Algorithme Dijkstra: 1. Implémentation Du Graphe

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

14/11/2022 10:16 Algorithme Dijsktra.

ipynb - Colaboratory

TP : Algorithme Dijkstra

1. Implémentation du graphe

G = {
'A':[(1,'B'), (2,'C')],
'B':[(1,'A'), (2,'D'), (3,'F')],
'C':[(2,'A'), (3,'D'), (4,'E')],
'D':[(2,'B'), (3,'C'), (3,'F'), (2,'E'), (3,'G')],
'E':[(4,'C'), (2,'D'), (5,'G')],
'F':[(3,'B'), (3,'D'), (4,'G')],
'G':[(4,'F'), (3,'D'), (5,'E')],
}

M = [
[0, 1, 2, 0, 0, 0, 0],
[1, 0, 0, 2, 0, 3, 0],
[2, 0, 0, 3, 4, 0, 0],
[0, 2, 3, 0, 2, 3, 3],
[0, 0, 4, 2, 0, 0, 5],
[0, 3, 0, 3, 0, 0, 4],
[0, 0, 0, 3, 5, 4, 0]
]

2. Fonctionsunitlitaire pour modéliser un labyrinthe par un graphe

import matplotlib.pyplot as plt


import matplotlib.patches as patches
import numpy as np

labyrinthe = [
[1,1,1,1,1],
[1,1,0,1,1],
[1,0,0,0,0],
[1,1,1,1,1],
[1,0,0,0,1]
]

def creer_graphe( LAB ):


d = len(LAB)*len(LAB)
M = np.zeros((d, d))
for i in range(len(LAB)):
for j in range(len(LAB)):
n = i*len(LAB) + j
if i>0 and LAB[i-1][j]==1:
a = (i-1)*len(LAB) + j
M[n][a] = 1
https://colab.research.google.com/drive/1ib696PUovH87W40CHZ-ISFQODUG3L0KH#scrollTo=6695de3b-e6de-49fe-b8ff-44400f1abfd1&printMo… 1/4
14/11/2022 10:16 Algorithme Dijsktra.ipynb - Colaboratory

if i<len(LAB)-1 and LAB[i+1][j]==1:


a = (i+1)*len(LAB) + j
M[n][a] = 1
if j>0 and LAB[i][j-1]==1:
a = i*len(LAB) + j-1
M[n][a] = 1
if j<len(LAB)-1 and LAB[i][j+1]==1:
a = i*len(LAB) + j+1
M[n][a] = 1
return M

def dessiner_labyrinthe(LAB, chemin):


fig, ax = plt.subplots()
for i in range(len(LAB)):
for j in range(len(LAB)):
color = 'black'
if LAB[i][j]==1 : color = 'white'
rect = patches.Rectangle((j, -i), 1, 1, edgecolor='black', facecolor=color)
ax.add_patch(rect)
for n in chemin :
i = n//len(LAB)
j = n%len(LAB)
rect = patches.Circle((j+0.5, -i+0.5), 0.1, edgecolor='black', facecolor='blue')
ax.add_patch(rect)

plt.axis('equal')
plt.show()

3. Implémentation de dijkstra

def voisins(G , noeud ) :


if type(G)==dict :
return G[noeud]
V = []
for i in range(len(G)):
if G[noeud][i]!=0 :
V.append((G[noeud][i], i))
return V

from queue import PriorityQueue

def dijkstra(G, depart, arrivee):

noeud_visites = []

file_priorite = PriorityQueue()
file_priorite.put((0, depart))

distance_min = {}
distance_min[depart] = 0

https://colab.research.google.com/drive/1ib696PUovH87W40CHZ-ISFQODUG3L0KH#scrollTo=6695de3b-e6de-49fe-b8ff-44400f1abfd1&printMo… 2/4
14/11/2022 10:16 Algorithme Dijsktra.ipynb - Colaboratory

precedents = {}

while not file_priorite.empty() :

distance, noeud_courant = file_priorite.get()


if noeud_courant == arrivee :
break

for longueur_arc, voisin in voisins(G, noeud_courant):


if voisin in noeud_visites:
continue
nouvelle_distance = longueur_arc + distance
if voisin not in distance_min or nouvelle_distance < distance_min[voisin] :
distance_min[voisin]=nouvelle_distance
file_priorite.put((nouvelle_distance, voisin))
precedents[voisin]=noeud_courant

noeud_visites.append(noeud_courant)

# Calculer le chemin
chemin = []
x = arrivee
while x!=depart :
chemin.insert(0, x)
x = precedents[x]
chemin.insert(0, x)

return chemin, distance_min[arrivee]

# Tester la fonction avec le graphe


print(dijkstra(G, 'A', 'G'))
print(dijkstra(M, 0, 6))

(['A', 'B', 'D', 'G'], 6)


([0, 1, 3, 6], 6)

chemin, distance = dijkstra(creer_graphe( labyrinthe ), 4, 24)


dessiner_labyrinthe(labyrinthe, chemin)

https://colab.research.google.com/drive/1ib696PUovH87W40CHZ-ISFQODUG3L0KH#scrollTo=6695de3b-e6de-49fe-b8ff-44400f1abfd1&printMo… 3/4
14/11/2022 10:16 Algorithme Dijsktra.ipynb - Colaboratory

Produits payants Colab - Résilier les contrats ici

check 0 s terminée à 10:15

https://colab.research.google.com/drive/1ib696PUovH87W40CHZ-ISFQODUG3L0KH#scrollTo=6695de3b-e6de-49fe-b8ff-44400f1abfd1&printMo… 4/4

You might also like