mirror of
https://github.com/Fare-spec/cours.git
synced 2025-12-07 02:40:38 +00:00
bellman
This commit is contained in:
28
programmation_dynamique/bellman/main.py
Normal file
28
programmation_dynamique/bellman/main.py
Normal file
@@ -0,0 +1,28 @@
|
||||
def bellman_ford(graphe, sommet):
|
||||
distance = {sommet: float("inf") for sommet in graphe}
|
||||
distance[sommet] = 0
|
||||
|
||||
for i in range(len(graphe) - 1):
|
||||
for u in graphe:
|
||||
for v, coef in graphe[u]:
|
||||
if distance[u] + coef < distance[v]:
|
||||
distance[v] = distance[u] + coef
|
||||
|
||||
for u in graphe:
|
||||
for v, coef in graphe[u]:
|
||||
if distance[u] + coef < distance[v]:
|
||||
raise ValueError("Cycle négatif détecté")
|
||||
|
||||
return distance
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
graphe = {
|
||||
"A": [("G", 8), ("H", 4)],
|
||||
"G": [("H", 1), ("L", 2)],
|
||||
"H": [("L", 5), ("E", 7)],
|
||||
"L": [("E", -2)],
|
||||
"E": [],
|
||||
}
|
||||
distances = bellman_ford(graphe, "A")
|
||||
print(distances)
|
||||
Reference in New Issue
Block a user