Created
August 24, 2021 10:51
-
-
Save quantra-go-algo/0d67d7bb5bf4f4ddf4a104f58a6972e7 to your computer and use it in GitHub Desktop.
A simple graph to test the shortest path using Dijkstra algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A, B, C, D, E = nodes = list("ABCDE") | |
graph = Graph() | |
graph.add_edge(A, B, 6) | |
graph.add_edge(A, E, 15) | |
graph.add_edge(B, C, 1) | |
graph.add_edge(C, D, 1) | |
graph.add_edge(D, E, 4) | |
dijkstra = DijkstraSPF(graph, A) | |
print("%-5s %-5s" % ("label", "distance")) | |
for u in nodes: | |
print("%-5s %8f" % (u, dijkstra.get_distance(u))) | |
print("\nShortest path:") | |
print(" -> ".join(dijkstra.get_path(E))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment