Skip to main content
PreparationZone Logo
PreparationZone
Back to Fundamentals

Fundamentals · Hard · Free

Explain Dijkstra's algorithm at a high level (non-negative weights).

FresherFundamentalsData Structures & Algorithms
Model answer

Dijkstra

Shortest path from source with non-negative weights—greedy expand closest unvisited node using min-heap.

java
// Relax edges from u; if dist[v] improves, push heap
// O((V+E) log V) with binary heap
  • Cannot handle negative edges—use Bellman-Ford
  • Used in maps routing, network OSPF ideas
Contrast with BFS when all edge weights = 1.