Skip to content

Improve bellman_ford.py #1575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 19, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions graphs/bellman_ford.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
from typing import Dict, List


def printDist(dist, V):
print("\nVertex Distance")
for i in range(V):
if dist[i] != float("inf"):
print(i, "\t", int(dist[i]), end="\t")
else:
print(i, "\t", "INF", end="\t")
print()
print("Vertex Distance")
distances = ("INF" if d == float("inf") else d for d in dist)
print("\t".join(f"{i}\t{d}" for i, d in enumerate(distances)))


def BellmanFord(graph, V, E, src):
def BellmanFord(graph: List[Dict[str, int]], V: int, E: int, src: int) -> int:
r"""
Returns shortest paths from a vertex src to all
other vertices.
"""
mdist = [float("inf") for i in range(V)]
mdist[src] = 0.0

Expand All @@ -30,6 +33,7 @@ def BellmanFord(graph, V, E, src):
return

printDist(mdist, V)
return src


if __name__ == "__main__":
Expand All @@ -38,7 +42,7 @@ def BellmanFord(graph, V, E, src):

graph = [dict() for j in range(E)]

for i in range(V):
for i in range(E):
graph[i][i] = 0.0

for i in range(E):
Expand Down