Skip to content

Instantly share code, notes, and snippets.

@kachayev
kachayev / dijkstra.py
Last active July 28, 2024 13:10
Dijkstra shortest path algorithm based on python heapq heap implementation
from collections import defaultdict
from heapq import *
def dijkstra(edges, f, t):
g = defaultdict(list)
for l,r,c in edges:
g[l].append((c,r))
q, seen, mins = [(0,f,())], set(), {f: 0}
while q: