Shortest Path — Definition, Formula & Example
A shortest path is a route between two vertices of a graph whose edge weights, such as miles, minutes, or dollars, add up to the smallest possible total. The shortest path problem asks you to find that route and its total weight.
Let be a graph with a weight function . A shortest path from to is a path from to that minimizes . The minimum value is the distance . If no path exists, . The distance is well defined only when no negative-weight cycle can be reached on the way from to . In an unweighted graph, every edge has weight 1, so a shortest path is one with the fewest edges.
Key Formula
Where:
- = The distance, meaning the total weight of a shortest path from s to t
- = Any path from vertex s to vertex t
- = An edge on the path P
- = The weight of edge e, such as a length in miles or a travel time in minutes
Worked Example
Problem: Five intersections S, A, B, C, T are joined by two-way streets with these travel times in minutes: S–A 7, S–B 2, B–A 3, A–C 1, B–C 8, C–T 2, A–T 6. Use Dijkstra's method to find the fastest route from S to T.
Step 1: Start at S. Its neighbors give first estimates: B is 2 minutes away and A is 7. B has the smallest estimate, so its distance is final. Going through B improves A to 2 + 3 = 5 and gives C a first estimate of 2 + 8 = 10.
Step 2: A now has the smallest open estimate, so d(S,A) = 5 is final. Going through A improves C to 5 + 1 = 6 and gives T a first estimate of 5 + 6 = 11.
Step 3: C is next, with d(S,C) = 6. Going through C gives T the estimate 6 + 2 = 8, which beats 11. T is the last vertex, so its distance is final.
Answer: The fastest route is S → B → A → C → T, taking 8 minutes. It uses four streets, while S → A → T uses only two but takes 13 minutes.
Why It Matters
GPS apps run shortest path algorithms such as Dijkstra's algorithm and A* search on road networks to plan driving routes. Internet routers use the same idea to send data packets along low-delay links. In a college discrete math or algorithms course, the shortest path problem is often your first encounter with greedy algorithms and dynamic programming.
Common Mistakes
Mistake: Assuming the path with the fewest edges is the shortest path in a weighted graph.
Correction: Fewest edges is the right measure only when every edge has the same positive weight. In the example above, S → A → T has two edges and takes 13 minutes, while S → B → A → C → T has four edges and takes 8 minutes. Always add the weights, not the edges.
