Skip to content

Instantly share code, notes, and snippets.

@dingran
dingran / dijkstra.py
Last active November 30, 2023 22:43
Python implementation of Dijkstra's algorithm, single source all desinations and single source single destination
from collections import defaultdict
def build_graph(edge_list):
graph = defaultdict(list)
seen_edges = defaultdict(int)
for src, dst, weight in edge_list:
seen_edges[(src, dst, weight)] += 1
if seen_edges[(src, dst, weight)] > 1: # checking for duplicated edge entries
continue