Skip to content

Instantly share code, notes, and snippets.

@uztbt
Created January 15, 2021 11:50
Show Gist options
  • Save uztbt/6895379f8f4834ffdf94e7a07d7fbadb to your computer and use it in GitHub Desktop.
Save uztbt/6895379f8f4834ffdf94e7a07d7fbadb to your computer and use it in GitHub Desktop.
A Dijkstra's shortest path algorithm implementation in Python 3
class Graph:
def __init__(self, V: set[str], E: dict[dict[str, int]]) -> None:
super().__init__()
self.V = V
self.E = E
def dijkstra(self, s: str, t: str) -> int:
"""
Returns the shortest path from s to t
"""
Dist = dict()
for vertex in self.V:
Dist[vertex] = 10 ** 9
Dist[s] = 0
SptSet = set()
while SptSet != self.V:
u = closest(Dist, self.V.difference(SptSet))
SptSet.add(u)
EdgeFromU = self.E[u]
Adjacents = set(EdgeFromU.keys()).difference(SptSet)
for adj in Adjacents:
Dist[adj] = min(Dist[adj], Dist[u] + EdgeFromU[adj])
return Dist[t]
def closest(Dist: dict[str, int], cands: set[str]) -> str:
try:
ret = cands.pop()
except KeyError:
raise RuntimeError("This won't happen!")
for cand in cands:
if Dist[cand] < Dist[ret]:
ret = cand
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment