You know how Google Maps tells you the fastest way to get from point A to point B?
Thatβs Dijkstra algorithm in action. Well, sort of. Itβs behind many shortest path calculations, and it’s the foundation of modern pathfinding tools. When I first learned about it in college, I honestly had no clue what was going on. A bunch of nodes? Edges? Weights? Sounds more like gym equipment than computer science. π
But once I saw it in action, it clicked. And today, Iβm breaking it down β in plain English β for you.
So if you’re curious about how computers figure out the most efficient path in a weighted graph using a greedy algorithm, letβs dive in.
π¦ What is Dijkstra Algorithm?

Let me say it again: Dijkstra algorithm helps you find the shortest path between two nodes in a weighted graph (where each edge has a βcostβ or βdistanceβ).
If you’re currently exploring a data structures and algorithms course, understanding Dijkstra’s algorithm is a must β it’s one of the most essential concepts you’ll encounter in technical interviews and competitive programming.
π The catch? All edge weights must be non-negative. That means no “reverse tunnels” or “shortcuts” that reward you with negative points.
It’s called a greedy algorithm because it always picks the βclosestβ unvisited node. It doesnβt overthink β it just grabs the best option right now. Simple, right?
π§ Real-World Uses of Dijkstra Algorithm

Letβs get practical. Hereβs where youβve already seen Dijkstra in action:
- GPS apps (Google Maps, Waze): find shortest driving or walking paths.
- Network routing protocols like OSPF (Open Shortest Path First).
- Video games (AI enemy pathfinding).
- Logistics & supply chain: route optimization for delivery trucks.
- Urban planning tools: best infrastructure layouts.
Every time you find an optimized route, there’s a high chance Dijkstra’s algorithm (or one of its cousins) had a say.
π§ Key Concepts You Need to Know First
Before we jump to code, letβs break down some terms:
- Weighted graph: A network of nodes (points) and edges (paths) where each edge has a weight (distance, time, cost).
- Greedy algorithm: Chooses the best immediate option without thinking too far ahead.
- Priority queue: A data structure that always lets you access the smallest weight node quickly.
- Visited nodes: Once a nodeβs shortest path is found, we donβt revisit it.
βοΈ Dijkstra Pseudocode (Algorithm)
Here’s a simplified version of Dijkstraβs pseudocode:
1. Mark all nodes unvisited. Set distance to all nodes as infinity.
2. Set starting nodeβs distance to 0.
3. While there are unvisited nodes:
a. Choose the unvisited node with the smallest distance.
b. For each neighbor of this node:
– Calculate tentative distance through this node.
– If it’s smaller, update the neighborβs distance.
c. Mark the node as visited.
4. Done when all nodes are visited or destination reached.
Think of it like exploring a city. You visit the closest block, update routes for nearby places, and move on.
π§ͺ Python Code Example Using heapq

import heapq
def dijkstra(graph, start):
distances = {node: float('inf') for node in graph}
distances[start] = 0
queue = [(0, start)]
while queue:
current_distance, current_node = heapq.heappop(queue)
for neighbor, weight in graph[current_node]:
distance = current_distance + weight
if distance < distances[neighbor]:
distances[neighbor] = distance
heapq.heappush(queue, (distance, neighbor))
return distances
Try it with a graph like this:
graph = {
'A': [('B', 1), ('C', 4)],
'B': [('C', 2), ('D', 5)],
'C': [('D', 1)],
'D': []
}
Run dijkstra(graph, ‘A’) and it returns shortest distances from A.
This example is written in Python, but you can also implement Dijkstraβs algorithm in Java β something we often teach in our Java course in Chennai and Python course in Chennai, where students build real-time graph applications.
π Time Complexity β How Efficient is It?
Letβs talk performance:
| Implementation | Time Complexity |
| Using arrays | O(VΒ²) |
| Using min-heap (heapq) | O(E log V) |
Thatβs why priority queues (like Python’s heapq) matter.
βοΈ Dijkstra vs Bellman-Ford vs A*
So when shouldnβt you use Dijkstraβs algorithm?
- If your graph has negative weights, skip Dijkstra. Use Bellman-Ford.
- If you’re optimizing for both speed and accuracy, check out A* (which adds a heuristic).
- For all-pairs shortest paths, look at Floyd-Warshall.
β Limitations of Dijkstra Algorithm
- π« No negative weights
- π’ Can be slow on massive graphs (use A* with heuristics or bidirectional Dijkstra)
- β Doesnβt guarantee the best route in weighted graphs with weird constraints
π‘ Final Thoughts
I hope this breakdown of Dijkstra algorithm helps you not just memorize it β but understand it.
Whether you’re prepping for a coding interview, optimizing a project, or just love how algorithms mimic the real world β this oneβs a must-know.
Want to master these concepts hands-on? Consider enrolling in a practical data structures and algorithms course or boost your programming skills with our Java or Python courses in Chennai.