“The shortest way is not always obvious — but Dijkstra’s Algorithm always finds it.”
🧭 Introduction
Imagine you’re using Google Maps to find the quickest route from your home to work.
Behind that magic lies an algorithm that’s been solving such problems since 1956 — Dijkstra’s Algorithm.
Developed by Edsger W. Dijkstra, this algorithm is one of the most elegant and efficient ways to find the shortest path between nodes in a weighted graph.
And guess what? It’s not just used in navigation.
It’s the backbone of network routing, logistics optimization, AI game movement, and many real-world systems that deal with connections and distances.
🧩 What Is Dijkstra’s Algorithm?
Dijkstra’s Algorithm is a shortest path algorithm that finds the minimum distance from a starting point (called the source) to all other points (nodes) in a graph.
In simple words:
It explores all possible paths, step by step, always picking the next nearest location — ensuring the shortest total route.
A graph here means a network of connected nodes — for example:
- Cities connected by roads
- Computers connected in a network
- Users connected in a social graph
Each edge (connection) has a weight — such as distance, cost, or time.
⚙️ The Core Idea (in Plain English)
Think of yourself standing in a city (the starting node).
You want to know the shortest route to every other city on the map.
Here’s how Dijkstra’s Algorithm thinks:
- Start at your city (distance = 0).
- All other cities are unknown (distance = infinity).
- Visit the nearest unvisited city.
- Update the distances to all its neighbors.
- Repeat until all cities are visited or destination is reached.
In each step, the algorithm always chooses the path with the least total distance so far — just like a smart GPS system recalculating routes.
🗺️ Example — Step by Step
Let’s take a small example graph:
(4)
A ------- B
| \ \
| \ \
(2) (5) (10)
| \ \
C------ D------E
(3) (1)
We need the shortest distance from A → E.
Step 1: Initialization
Start at A.
| Node | Distance | Previous |
|---|---|---|
| A | 0 | — |
| B | ∞ | — |
| C | ∞ | — |
| D | ∞ | — |
| E | ∞ | — |
Step 2: From A
Direct connections:
- A → B = 4
- A → C = 2
- A → D = 5
Update distances:
| Node | Distance | Previous |
|---|---|---|
| A | 0 | — |
| B | 4 | A |
| C | 2 | A |
| D | 5 | A |
| E | ∞ | — |
Next, choose the smallest unvisited node → C (2).
Step 3: From C
C → D = 3
New distance to D = 2 + 3 = 5 (already 5, no change).
Step 4: Next smallest = B (4)
B → E = 10
New distance to E = 4 + 10 = 14
| Node | Distance | Previous |
|---|---|---|
| A | 0 | — |
| B | 4 | A |
| C | 2 | A |
| D | 5 | A |
| E | 14 | B |
Step 5: Next smallest = D (5)
D → E = 1
New distance to E = 5 + 1 = 6 (better than 14 ✅)
Final distances:
| Node | Distance | Previous |
|---|---|---|
| A | 0 | — |
| B | 4 | A |
| C | 2 | A |
| D | 5 | A |
| E | 6 | D |
✅ Shortest path A → E = 6
Path: A → D → E
💻 Python Implementation
Here’s a clean and simple Python version using a priority queue (heapq) for efficiency
import heapq
def dijkstra(graph, start):
distances = {node: float('inf') for node in graph}
distances[start] = 0
pq = [(0, start)] # (distance, node)
while pq:
current_distance, current_node = heapq.heappop(pq)
# Skip if already found a shorter path
if current_distance > distances[current_node]:
continue
for neighbor, weight in graph[current_node].items():
distance = current_distance + weight
if distance < distances[neighbor]:
distances[neighbor] = distance
heapq.heappush(pq, (distance, neighbor))
return distances
# Example usage
graph = {
'A': {'B': 4, 'C': 2, 'D': 5},
'B': {'E': 10},
'C': {'D': 3},
'D': {'E': 1},
'E': {}
}
print(dijkstra(graph, 'A'))
Output:
{‘A’: 0, ‘B’: 4, ‘C’: 2, ‘D’: 5, ‘E’: 6}
🧠 Time & Space Complexity
| Operation | Complexity |
|---|---|
| Time Complexity | O((V + E) log V) using a priority queue |
| Space Complexity | O(V) (to store distances and queue) |
Where:
V= number of vertices (nodes)E= number of edges (connections)
It’s very efficient for dense graphs and scales well in real-world systems.
🌍 Real-World Applications
| Area | Use |
|---|---|
| 🚗 Navigation Systems | Finding fastest route between cities (Google Maps, Uber) |
| 📡 Network Routing | Finding least-cost path between servers |
| 🕹️ Game Development | Pathfinding for NPCs (non-player characters) |
| 🏭 Logistics | Optimizing delivery and travel routes |
| 🧮 AI & Robotics | Helping robots plan movements and avoid obstacles |
⚖️ Dijkstra vs Other Algorithms
| Algorithm | Works With | Negative Weights | Usage |
|---|---|---|---|
| Dijkstra’s | Weighted Graphs | ❌ No | Shortest path from one node |
| Bellman-Ford | Weighted Graphs | ✅ Yes | When negative edges exist |
| A* | Weighted Graphs + Heuristic | ❌ No | Faster pathfinding (used in AI, maps) |
| Floyd-Warshall | Weighted Graphs | ✅ Yes | All-pairs shortest paths |
🔮 In Short
| Concept | Summary |
|---|---|
| Purpose | Find the shortest path in a weighted graph |
| Created By | Edsger Dijkstra (1956) |
| Key Idea | Always expand the nearest unvisited node |
| Data Structures | Priority Queue (Min Heap), HashMap |
| Complexity | O((V + E) log V) |
| Applications | Maps, Networks, Games, AI, Logistics |
🧭 Final Thoughts
Dijkstra’s Algorithm is more than just a DSA topic —
it’s a real-world problem-solving tool used in systems you use every day.
Learning it teaches you:
- Graph theory fundamentals
- Optimization thinking
- Efficient data structure combinations
So next time you open Google Maps and it says
“Fastest route found,”
remember — Dijkstra just ran under the hood. ⚙️✨
