JavaScript Snippet

Dijkstra Shortest Path Template

Difficulty: Hard

Dijkstra's algorithm finds the shortest path from a source to every reachable node in a weighted graph with non-negative edge weights. The textbook O((V + E) log V) implementation pairs a min-heap with a relaxation loop. This snippet covers the heap-based template, a parent-tracking variant that reconstructs the actual path, and an early-exit form for single-target queries.

Code Snippets
/

Dijkstra Shortest Path Template

Dijkstra Shortest Path Template

Dijkstra's algorithm finds the shortest path from a source to every reachable node in a weighted graph with non-negative edge weights. The textbook O((V + E) log V) implementation pairs a min-heap with a relaxation loop. This snippet covers the heap-based template, a parent-tracking variant that reconstructs the actual path, and an early-exit form for single-target queries.

JavaScript
Hard
algorithms
dijkstra
code-template
heap

1,111 views

17

This code snippet is available for premium members only.

Upgrade to Premium