Shortest Path
When you use Google Maps to ask for directions from your home to your Aunt’s house, you are usually looking for the shortest path between the two locations. Computer applications use representations of the street maps as graphs, with estimated driving times as edge weights.
While often it is possible to find the shortest path on a small graph by guess-and-check, our goal in this section is to develop methods to solve complex problems in a systematic way by following algorithms. An algorithm is a step-by-step procedure for solving a problem. Dijkstra’s (pronounced dike-stra) algorithm will find the shortest path between two vertices.
Dijkstra’s Algorithm
- Mark the ending vertex with a distance of zero. Designate this vertex as current.
- Find all vertices leading to the current vertex. Calculate their distances to the end. Since we already know the distance the current vertex is from the end, this will just require adding the most recent edge. Don’t record this distance if it is longer than a previously recorded distance.
- Mark the current vertex as visited. We will never look at this vertex again.
- Mark the vertex with the smallest distance as current, and repeat from step [latex]2[/latex].

Dijkstra’s algorithm is an optimal algorithm, meaning that it always produces the actual shortest path, not just a path that is pretty short, provided one exists. This algorithm is also efficient, meaning that it can be implemented in a reasonable amount of time. Dijkstra’s algorithm takes around [latex]V2[/latex] calculations, where [latex]V[/latex] is the number of vertices in a graph.[1] A graph with [latex]100[/latex] vertices would take around [latex]10,000[/latex] calculations. While that would be a lot to do by hand, it is not a lot for a computer to handle.
In contrast, an inefficient algorithm might try to list all possible paths and then compute the length of each path. Trying to list all possible paths could easily take [latex]1025[/latex] calculations to compute the shortest path with only [latex]25[/latex] vertices; that’s a [latex]1[/latex] with [latex]25[/latex] zeros after it! To put that in perspective, the fastest computer in the world would still spend over [latex]1000[/latex] years analyzing all those paths.
Efficiency is a hallmark of mathematical practice. When mathematicians seek a proof for something they have conjectured to be true, the most elegant proof is often the most efficient one: an argument that packs the most information in the fewest words, so to speak.
The ideas explored in graph theory are frequently applied to computing algorithms: the language and instructions of software. Since resources are limited (time, computing power), mathematicians and computer scientists seek the most efficient ways to compute. Graph theory helps them find the shortest path from [latex]A[/latex] to [latex]B[/latex].
- It can be made to run faster through various optimizations to the implementation. ↵