Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quantra-go-algo/0d67d7bb5bf4f4ddf4a104f58a6972e7 to your computer and use it in GitHub Desktop.
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
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