Skip to content

Instantly share code, notes, and snippets.

@quantra-go-algo
Created August 24, 2021 10:48
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/2bd61f7441c2baabecf8ce96de351a77 to your computer and use it in GitHub Desktop.
Save quantra-go-algo/2bd61f7441c2baabecf8ce96de351a77 to your computer and use it in GitHub Desktop.
Importing math
import math
class AbstractDijkstraSPF(object):
""" Dijkstra's shortest path algorithm, abstract class. """
def __init__(self, G, s):
""" Calculate shortest path from s to other nodes in G. """
self.__dist = dist = dict()
self.__prev = prev = dict()
visited = set()
queue = set()
dist[s] = 0
prev[s] = s
queue.add(s)
while queue:
u = min(queue, key=dist.get)
for v in self.get_adjacent_nodes(G, u):
if v in visited:
continue
alt = self.get_distance(u) + self.get_edge_weight(G, u, v)
if alt < self.get_distance(v):
dist[v] = alt
prev[v] = u
queue.add(v)
queue.remove(u)
visited.add(u)
@staticmethod
def get_adjacent_nodes(G, u):
raise NotImplementedError()
@staticmethod
def get_edge_weight(G, u, v):
raise NotImplementedError()
def get_distance(self, u):
""" Return the length of shortest path from s to u. """
return self.__dist.get(u, math.inf)
def get_path(self, v):
""" Return the shortest path to v. """
path = [v]
while self.__prev[v] != v:
v = self.__prev[v]
path.append(v)
return path[::-1]
class DijkstraSPF(AbstractDijkstraSPF):
@staticmethod
def get_adjacent_nodes(G, u):
return G.get_adjacent_nodes(u)
@staticmethod
def get_edge_weight(G, u, v):
return G.get_edge_weight(u, v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment