Finding the Fastest Way Out: How Dijkstra’s Algorithm Finds Shortest Paths
Sr. Manager, Technical Product Marketing, Neo4j
5 min read

Imagine you’re an explorer searching an old mine for abandoned treasure. You have in your possession an old map that shows all the chambers and the tunnels connecting them. Each chamber is a node in our graph, and each tunnel has a travel time.
You’re currently in Cave A, and the treasure is in Cave F. The air down there can be toxic, so you want to get in and out as quickly as possible. Lucky for you, that’s exactly the problem Dijkstra’s algorithm solves.
Dijkstra doesn’t just find the fastest route from Cave A to Cave F. It calculates the shortest distance from A to every other cave, updating those distances over multiple iterations. To keep everything straight, we will track a few things in both a diagram of the cave system and a table:
- What nodes are considered visited (which we put in grey), and what nodes have not been visited (we will leave those as white)
- The current shortest distance between the starting node (which is the second column in our table) and the node of interest (which we will put in green)
- And finally, the previous node that leads us to the current shortest distance (the third column in our table)
You have to give Dijkstra a starting node, which in our case is Cave A (colored green), so we can add to the table to show that the distance from Cave A to Cave A is 0.
Next, we look at Cave A’s unvisited neighbors, which are Cave B and Cave C, which we will color orange. Let’s record their distances from Cave A.
Once we’ve calculated the distance from Cave A to all of A’s neighbors, A is officially “visited.”
Here’s the elegant trick: we now choose the closest unvisited cave. In this case, that’s Cave C, so we will now calculate the difference between Cave C and all unvisited neighboring caves of C:
- A is visited at this point. We know the distance to all its neighboring nodes, so we will color it grey now.
- D has not been explored at all at this point, so it definitely counts as unvisited
- B has been explored, but not via C, so it is considered unvisited.
Now we update the table. Let’s focus in on Cave B. The current shortest distance between Cave A and Cave B is 6. But by going through Cave C, the newest shortest distance will be 5, so we will update the table.
We also update the distance for Cave D to be 10. Why? Well, because from D to C is 8 and from C to A is 2, you add them to get 10!
Okay, so now we are finished with C because we have measured all previously unvisited nodes of C. We then choose the nodes from C’s unvisited neighbors with the shortest distance, which in this case is B, as 5 is less than 10.
B only has one unvisited neighbor—E. It takes a length of 9 to reach E from A, so we update the table. We now mark B as visited and designate E as our observation cave.
Get the Free eBook
Download “A Practical Guide to Graph Algorithms” and learn the math intuition behind five of the most commonly used graph algorithms!
Cave E has two unvisited neighbors—D and F. The quickest path to D through E would be 16 compared to just 10 when going through C, and F’s would be 12. We can now mark E as visited.
Take another look at our table above. At this point, D has the shortest distance of our unvisited neighbors, even though we are off what had previously been the “shortest path.” This means that Cave D is now under the microscope!
D has only one unvisited neighbor—F. Our previous shortest distance to D was 10, so the shortest path through D to F would be 13, which—devastatingly—is longer than the path through E.
Our table shows the shortest distance between Cave A and each of the other caves. Our previous node gives us a clue about the path we need to take.
It’s 12 total units from Cave A to Cave F. The previous node was Cave E. Its previous was B, and so on all the way back to A.
So What?
Dijkstra’s algorithm shows up all over the place once you start modeling real systems as networks. It’s used to figure out the fastest delivery routes in supply chains, power routing in mapping and ride-sharing apps, and to move data efficiently across telecom networks. You’ll also see it in things like tracing risk or fraud paths in financial systems or understanding dependencies in IT infrastructure. At the end of the day, it’s a simple idea—find the shortest path—but it ends up being incredibly useful anywhere you need to move something through a complex system efficiently.
If you want to see it in action, check out this past article I wrote! learn how to find the shortest route between two stations in the world’s largest subway system — the NYC Subway, with over 470 stations — using Dijkstra’s algorithm in our hands-on walkthrough. We’ll use Neo4j Graph Analytics for Snowflake to load NYC subway stations and track connections, create a digital twin, and run Dijkstra to compute the fewest-stop route between two stations. Then we’ll simulate a disruption by removing a closed station from the network and rerun Dijkstra to reveal the new shortest route riders would take.







