Dijkstra's algorithm works like Google Maps finding the fastest driving route! It calculates the shortest total travel time from your starting city to every other city through weighted toll roads.
How it Works Step-by-Step:
1Set Start Distance to 0: Set distance to starting city = 0, and distance to all other cities = ∞ (Infinity).
2Pick Closest Unvisited City: Pick the unvisited city with the current shortest total distance from the start.
3Look for Shortcuts: Check all outgoing roads to neighbor cities. If current time + road weight < neighbor known time, update the neighbor's shortcut time!
4Lock City & Repeat: Mark the current city as permanently finalized, and repeat until all cities are visited!
🚗 Real-World Analogy: Driving Trip through Toll Roads
Imagine driving from City A to City F. You write down the current fastest drive time to each city on a whiteboard. Starting at A (0 mins), you look at roads to B (4 mins) and C (2 mins). C is faster! You travel to C, check roads out from C to D and E, and update shortcuts whenever you find a quicker highway. Once you reach F, your whiteboard holds the absolute fastest guaranteed route!