Dijkstra’s Algorithm — Finding the Shortest Path, Smartly
- Shreyas Naphad
- Jul 3
- 2 min read
Let’s say you're in a city trying to get to a friend’s house 🚶♂️, and there are many roads with different travel times. You want to get there as fast as possible.
Dijkstra’s Algorithm is your personal GPS. It tells you the shortest path from one point to all others in a weighted graph.
🧠 The Big Idea
Start from the source node and gradually expand by always choosing the next closest node — updating distances as you go.
🛠️ How It Works
Set the distance to the starting node as 0, and all others as ∞ (infinity).
Mark all nodes as unvisited.
Pick the unvisited node with the smallest known distance.
Update the distances of its neighbors.
Mark the current node as visited.
Repeat until all nodes are visited or the destination is reached.
🎯 Real-Life Analogy
You’re at a train station. You want to reach your final stop as quickly as possible. You look at all possible next trains, pick the fastest one, then repeat — until you reach your destination.
No guesswork. Just logic.
🔁 Example
Let’s say:
A --4-- B
| |
1 2
| |
C --3-- D
Start at A:
A → C = 1
C → D = 3 → total = 4
D → B = 2 → total = 6
A → B = 4 → already better!
So, A → B directly (4) is shorter than A → C → D → B (6)
Dijkstra chooses wisely 💡.
🚀 Used In:
Google Maps & GPS
Internet routing
AI pathfinding in games
Robotics navigation
⚠️ Note
Works best with non-negative weights (doesn’t like negative roads!).
🏁 Final Thought
Dijkstra is like a traveler with a map and a calculator. Always calculating, always choosing the best next step — until the destination is reached.
It doesn’t guess. It calculates smartly, step by step.





Comments