Let's be honest. When you first hear the name "Dijkstra" in a computer science class or an interview prep session, it sounds intimidating. It sounds like one of those algorithms that require a PhD in mathematics to understand.
In reality, once you sketch nodes and weighted edges on a whiteboard—tracing the shortest path between intersections on a map—the core idea clicks immediately: Dijkstra's algorithm is simply a systematic, priority-driven way of finding the lowest-cost route.
It’s the backbone of Google Maps, network routing protocols, and countless coding interview questions. If you can wrap your head around this, graph problems become significantly less scary. So, let’s break it down simply. No complex jargon, just the raw intuition and the Python code to back it up.

The Core Intuition: The Water Metaphor
Imagine a graph of interconnected nodes. You are standing at the starting node with a bucket of water. When you pour the water out, it flows along the edges to the neighboring nodes. But the edges have different lengths (or "weights").
The water will reach the nodes connected by shorter edges before it reaches the nodes connected by longer edges.
Dijkstra’s algorithm does exactly this. It radiates outwards from the starting point, always exploring the closest, cheapest, or shortest unvisited node next. It essentially asks: "Of all the nodes I can currently see, which one is the closest?" It moves there, updates its view of the world, and repeats.
The Three Golden Rules of Dijkstra
Before we look at code, let's establish the rules of the game:
- Keep track of distances: We need a way to remember the shortest distance from the start node to every other node. We initially set these to infinity (since we haven't explored them yet), except the start node, which is 0.
- Prioritize the closest: We always want to process the node with the smallest known distance next. (Spoiler alert: A Priority Queue or Min-Heap is perfect for this).
- Relax the edges: When we visit a node, we look at all its neighbors. If the distance to a neighbor through our current node is shorter than the neighbor's current known distance, we update it. This is called "relaxation."

A technical diagram showing nodes, edges, and the distance weights being calculated.
Why Not Just Use Breadth-First Search (BFS)?
You might be thinking, "Doesn't BFS already find the shortest path?"
Yes, but only if all the edges have the exact same weight (e.g., every step takes 1 minute). The real world isn't like that. Some roads have traffic. Some network links are slower. Dijkstra handles graphs where edges have different non-negative weights.
(Note: If your graph has negative weights, Dijkstra will break. You'll need the Bellman-Ford algorithm for that. But that's a story for another day.)
The Python Implementation
Alright, let's translate this intuition into Python code. We'll use the built-in heapq module to act as our priority queue.

import heapq
def dijkstra(graph, start):
# Dictionary to track the shortest distance to each node.
# Initialize all to infinity.
distances = {node: float('infinity') for node in graph}
distances[start] = 0
# Priority queue to hold (distance, node).
pq = [(0, start)]
while pq:
# Pop the node with the smallest distance
current_distance, current_node = heapq.heappop(pq)
# If we've found a shorter path to this node previously, skip it.
# This is an important optimization!
if current_distance > distances[current_node]:
continue
# Check all neighbors of the current node
for neighbor, weight in graph[current_node].items():
distance = current_distance + weight
# If we found a shorter path to the neighbor, update and push to queue
if distance < distances[neighbor]:
distances[neighbor] = distance
heapq.heappush(pq, (distance, neighbor))
return distances
# Example Usage:
# A graph represented as an adjacency list
graph = {
'A': {'B': 4, 'C': 2},
'B': {'C': 5, 'D': 10},
'C': {'E': 3},
'D': {'F': 11},
'E': {'D': 4},
'F': {}
}
print(dijkstra(graph, 'A'))
# Output: {'A': 0, 'B': 4, 'C': 2, 'D': 9, 'E': 5, 'F': 20}
Walking Through the Code
Let's dissect the most critical parts:
heapq.heappop(pq): This is the engine of the algorithm. It guarantees that we are always processing the node that is currently closest to our starting point.if current_distance > distances[current_node]: continue: This line handles outdated entries in our priority queue. Sometimes, we find a shorter path to a node after we've already added a longer path to the queue. This check ensures we don't waste time processing the old, longer path.- The Relaxation Step:
if distance < distances[neighbor]:is where the magic happens. We only update a node's distance if we've genuinely found a faster route.
The Time Complexity
In interviews, you must know the time complexity.
With an adjacency list and a priority queue (min-heap), the time complexity is O((V + E) log V), where V is the number of vertices (nodes) and E is the number of edges.
Why? Because in the worst case, we might add every edge to the priority queue. Extracting the minimum from a heap of size V takes O(log V) time. So, doing that for E edges gives us O(E log V).
Dijkstra vs. Bellman-Ford vs. BFS vs. A*: Choosing the Right Shortest-Path Algorithm
In coding and system design interviews, interviewers rarely say "Implement Dijkstra's algorithm." Instead, they describe a routing, network latency, or dependency graph problem and evaluate whether you pick the right shortest-path algorithm for the edge constraints:
| Algorithm | Edge Weight Constraint | Time Complexity | Space Complexity | Best Interview Use Case |
|---|---|---|---|---|
| Breadth-First Search (BFS) | Unweighted (or all weights = 1) | O(V + E) | O(V) | Minimum hops in social graphs, word ladder, shortest grid path without obstacles cost. |
| 0-1 BFS (Deque) | Edge weights are only 0 or 1 | O(V + E) | O(V) | Grid problems where moving in current direction costs 0 and turning costs 1. |
| Dijkstra's Algorithm | Non-negative weights (w >= 0) | O((V + E) log V) | O(V + E) | Network delay time, cheapest flight without hop limits, road navigation with traffic weights. |
| Bellman-Ford | Supports negative weights & detects negative cycles | O(V * E) | O(V) | Currency arbitrage detection, shortest path with at most K stops (Cheapest Flights Within K Stops). |
| A-Star (A) Search* | Non-negative weights + admissible heuristic h(u) | O(E) best case | O(V) | Point-to-point game map pathfinding with Manhattan or Euclidean distance heuristic. |
The Classic Interview Trap: Lazy Deletion in Binary Heaps
Standard library priority queues in Python (heapq), Java (PriorityQueue), and C++ (std::priority_queue) do not support decreaseKey in O(log V) time. Instead, we push a new pair (new_dist, neighbor) into the min-heap and leave the stale entry inside.
When popped later, you must skip stale heap entries using a single guard line:
if current_dist > distances[current_node]:
continue # Stale heap entry; we already processed a shorter path
Without that two-line check, a dense graph with many edge relaxations will re-process vertices repeatedly, degrading runtime from O((V + E) log V) toward O(E * V) and triggering a Time Limit Exceeded (TLE) on large test cases.
Final Thoughts
Dijkstra's algorithm is a beautiful piece of computer science history. Once you understand that it's just systematically exploring the closest options first and updating distances when it finds a shortcut, the code practically writes itself.
If you're gearing up for a technical interview and want to practice graph traversal problems like this in a realistic setting, check out totop.app. It’s designed to help you simulate these high-pressure coding scenarios so you can walk into your interview feeling confident, not intimidated. Keep practicing, and those shortest paths will start to look a lot clearer!
Practice Staff Backend Engineer Questions Live with AI Mock Interviews
Turn any job description across Engineering, DevOps, Data, Marketing, Sales, Finance, or HR into a personalized test and live voice or text AI mock interview on totop.app, or test your speed right now in the 60-Second Staff Backend Engineer Blitz.
Play 60s Staff Backend Engineer Blitz (Free, No Login) | Start 14-Day Free Trial | Browse All Interview Guides