Imagine you're Google Maps. A user drops a pin from Delhi to Mumbai and hits "Get Directions." Behind the scenes, one of the most elegant algorithms in computer science quietly runs — evaluating hundreds of roads, intersections, and traffic weights — to hand back the fastest route in milliseconds.

That algorithm is Dijkstra's Shortest Path Algorithm, published by Dutch computer scientist Edsger W. Dijkstra in 1959. It's a staple of every DSA course, every competitive programming contest, and every real-world routing system. Today we break it down from scratch — no jargon overload, just clean logic and a working example.

What is Dijkstra's Algorithm?

Dijkstra's algorithm finds the shortest (minimum-weight) path from a single source node to all other nodes in a weighted graph. The key constraint: all edge weights must be non-negative. It does not work correctly with negative weights — use Bellman-Ford for those cases.

Core Idea: Always expand the unvisited node with the smallest known distance from the source. Update its neighbors if a shorter path is found through it.

Where is it Used?

The Graph We'll Use

Let's define a weighted undirected graph with 6 nodes: A, B, C, D, E, F. We'll find the shortest path from node A to every other node.

4 2 1 5 8 10 2 6 3 A source B C D E F

Fig 1. Weighted undirected graph — Source node: A

Edge List

EdgeWeight
A — B4
A — C2
B — C1
B — D5
C — D8
C — E10
D — E2
D — F6
E — F3

Step-by-Step Walkthrough

We initialise all distances to except source A which starts at 0. A min-heap priority queue always picks the node with the smallest tentative distance next.

StepCurrentVisited dist[A]dist[B]dist[C] dist[D]dist[E]dist[F]
Init{ }0
1A{A}042
2C{A,C}0321012
3B{A,C,B}032812
4D{A,C,B,D}03281014
5E{A,C,B,D,E}03281013
6F{A,C,B,D,E,F}03281013

Final Shortest Distances from A

Key Insight: A→B via C costs 3 (2+1), not 4 (direct). Dijkstra catches this because it always relaxes edges through the currently cheapest known node.

The Algorithm — Pseudocode

pseudocode.txt — Dijkstra's Algorithm
function Dijkstra(graph, source):
    dist[source] = 0
    dist[all other nodes] = ∞
    priority_queue = [(0, source)]
    visited = {}

    while priority_queue is not empty:
        (current_dist, u) = pop minimum from priority_queue

        if u in visited → skip
        mark u as visited

        for each neighbor v of u with edge weight w:
            new_dist = current_dist + w
            if new_dist < dist[v]:
                dist[v] = new_dist
                push (new_dist, v) into priority_queue

    return dist

Python Implementation

dijkstra.py — Full Implementation
import heapq

def dijkstra(graph, source):
    dist = {node: float('inf') for node in graph}
    dist[source] = 0

    pq = [(0, source)]   # (distance, node)
    visited = set()

    while pq:
        current_dist, u = heapq.heappop(pq)

        if u in visited:
            continue
        visited.add(u)

        for neighbor, weight in graph[u]:
            new_dist = current_dist + weight
            if new_dist < dist[neighbor]:
                dist[neighbor] = new_dist
                heapq.heappush(pq, (new_dist, neighbor))

    return dist


# ── Graph Definition ──
graph = {
    'A': [('B', 4), ('C', 2)],
    'B': [('A', 4), ('C', 1), ('D', 5)],
    'C': [('A', 2), ('B', 1), ('D', 8), ('E', 10)],
    'D': [('B', 5), ('C', 8), ('E', 2), ('F', 6)],
    'E': [('C', 10), ('D', 2), ('F', 3)],
    'F': [('D', 6), ('E', 3)],
}

result = dijkstra(graph, 'A')

print("Shortest distances from A:")
for node, distance in sorted(result.items()):
    print(f"  A → {node} : {distance}")

Output

Output
Shortest distances from A:
  A → A : 0
  A → B : 3
  A → C : 2
  A → D : 8
  A → E : 10
  A → F : 13

Time & Space Complexity

ImplementationTimeSpace
Simple array (linear scan)O(V²)O(V)
Binary min-heap (heapq)O((V + E) log V)O(V + E)
Fibonacci heapO(E + V log V)O(V + E)

For most real-world use cases the binary heap version — O((V+E) log V) — is the standard choice. It's exactly what Python's heapq gives you.

Limitations of Dijkstra

Dijkstra vs Other Shortest Path Algorithms

AlgorithmNegative Weights?Single Source?Best For
DijkstraNoYesSparse graphs, GPS routing
Bellman-FordYesYesGraphs with negative weights
Floyd-WarshallYesNo (all pairs)Dense graphs, all-pairs shortest path
A*NoYes (single target)Game pathfinding with heuristic

Quick Summary

Try modifying the graph above — add a new node G with edges to D (weight 1) and F (weight 2). Run the algorithm again and find the new shortest path from A to G. If you can do that without looking anything up, you understand Dijkstra.

Found this helpful? Share it with your study group and bookmark Codenosis for more DSA breakdowns, Python tutorials, and real-world coding guides.