Python Snippet

Dijkstra in Python with heapq

Difficulty: Hard

Dijkstra finds the shortest path in a weighted graph with non-negative edge weights. The Python idiom is `heapq` (a binary min-heap) plus a distance dict, which gives O((V + E) log V) without external libraries. This entry covers the standard single-source template, path reconstruction, and the early-exit shortest-path-to-one-target variant.

Code Snippets
/

Dijkstra in Python with heapq

Dijkstra in Python with heapq

Dijkstra finds the shortest path in a weighted graph with non-negative edge weights. The Python idiom is `heapq` (a binary min-heap) plus a distance dict, which gives O((V + E) log V) without external libraries. This entry covers the standard single-source template, path reconstruction, and the early-exit shortest-path-to-one-target variant.

Python
Hard
dijkstra
graph-algorithms
min-heap
algorithms

674 views

16

This code snippet is available for premium members only.

Upgrade to Premium