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.