Shortest Paths
in Weighted Graphs
Learn how to find the minimum-cost route between nodes using fundamental graph algorithms: from intuition to Dijkstra's algorithm.
What is a Weighted Graph?
A graph is a collection of vertices (nodes) connected by edges. When each edge carries a number called its weight, we have a weighted graph. Weights often represent distance, cost, or time.
Why Do Shortest Paths Matter?
Finding shortest paths is one of the most widely applied problems in mathematics and computer science. Every time you use GPS navigation, route a data packet on the internet, or plan a delivery schedule, a shortest-path algorithm is at work.
Key Terminology
The Optimal Substructure Property
Shortest paths obey a powerful principle: every sub-path of a shortest path is itself a shortest path. This is the foundation that makes efficient algorithms possible.
The Triangle Inequality
For any edge \( (u, v) \) with weight \( w(u,v) \) and any source \( s \):
In words: the shortest distance to \( v \) can never exceed the shortest distance to \( u \) plus the direct edge cost from \( u \) to \( v \). This is the key test used by Dijkstra's algorithm to improve distance estimates.
BFS vs Dijkstra's: Key Distinction
| BFS | Dijkstra's | |
|---|---|---|
| Graph type | Unweighted | Weighted (non-negative) |
| Measures | Fewest edges | Smallest total weight |
| Data structure | Queue (FIFO) | Priority queue (min-heap) |
| Time complexity | \( O(V + E) \) | \( O((V + E) \log V) \) |
| Negative weights? | N/A | Not allowed |
How Dijkstra's Algorithm Works
How to Apply It: Checklist
Example 1: Find Shortest Path A to E
Example 1: Dijkstra's Trace
| Step | Visited | d(A) | d(B) | d(C) | d(D) | d(E) |
|---|---|---|---|---|---|---|
| Init | 0 | \( \infty \) | \( \infty \) | \( \infty \) | \( \infty \) | |
| 1 | A | 0 | 4 | 2 | \( \infty \) | \( \infty \) |
| 2 | A, C | 0 | 3 | 2 | 9 | 8 |
| 3 | A, C, B | 0 | 3 | 2 | 8 | 8 |
| 4 | A, C, B, D | 0 | 3 | 2 | 8 | 8 |
| 5 | A, C, B, D, E | 0 | 3 | 2 | 8 | 8 |
Example 1: Solution Path
Example 2: A Larger Graph
Example 2: Solution
Common Mistakes
Algorithm Summary
| Feature | BFS | Dijkstra's | Bellman-Ford |
|---|---|---|---|
| Edge weights | All equal (1) | Non-negative | Any (including negative) |
| Core idea | Level-by-level | Greedy relaxation | Repeated relaxation |
| Time | \( O(V + E) \) | \( O((V+E)\log V) \) | \( O(V \cdot E) \) |
| Detects neg. cycles? | No | No | Yes |
| Best for | Unweighted graphs | Road networks, maps | Finance, general |
Question 1 of 5
In Dijkstra's algorithm, what does the "relaxation" step do?
Question 2 of 5
A graph has vertices \( \{S, A, B, T\} \) with edges: \( S \to A \) (weight 2), \( S \to B \) (weight 5), \( A \to B \) (weight 1), \( A \to T \) (weight 6), \( B \to T \) (weight 2). What is \( d(S, T) \)?
Question 3 of 5
Why does Dijkstra's algorithm fail with negative edge weights?
Question 4 of 5
If all edges in a graph have weight 1, which algorithm finds shortest paths most efficiently?
Question 5 of 5
The optimal substructure property of shortest paths states that: