Shortest Path
Algorithms
Discover how BFS and Dijkstra's algorithm find the most efficient routes through graphs: the backbone of navigation, networking, and optimization.
What is a Graph?
A graph \( G = (V, E) \) consists of a set of vertices (nodes) \( V \) and a set of edges \( E \) connecting them. Graphs model relationships: cities connected by roads, computers on a network, or friends in a social group.
Weighted vs Unweighted Graphs
Why Shortest Paths Matter
Shortest path algorithms power critical systems you use every day. Understanding them opens the door to graph theory, optimization, and algorithm design.
BFS: Breadth-First Search
BFS explores a graph layer by layer. Starting from a source vertex \( s \), it visits all neighbors at distance \( 1 \), then distance \( 2 \), and so on. It uses a queue (first-in, first-out) to track the frontier.
BFS Step by Step
Dijkstra's Algorithm: The Big Idea
Dijkstra's algorithm generalizes BFS to weighted graphs with non-negative edge weights. Instead of a simple queue, it uses a priority queue (min-heap) that always processes the vertex with the smallest tentative distance next.
Dijkstra's Step by Step
BFS vs Dijkstra
| BFS | Dijkstra | |
|---|---|---|
| Graph type | Unweighted | Weighted (non-negative) |
| Data structure | Queue (FIFO) | Priority queue (min-heap) |
| Distance metric | Edge count | Sum of edge weights |
| Time complexity | \( O(|V| + |E|) \) | \( O((|V| + |E|) \log |V|) \) |
| Negative weights? | N/A | Not supported |
The Relaxation Operation
Relaxation is the core operation in Dijkstra's algorithm. When we process vertex \( u \) and examine edge \( (u, v) \), we check whether going through \( u \) gives a shorter path to \( v \):
\( \text{dist}[v] = \text{dist}[u] + w(u, v) \)
\( \text{prev}[v] = u \) // track the path
How to Apply These Algorithms
Example 1: BFS on an Unweighted Graph
Example 2: Dijkstra on a Weighted Graph
Dijkstra Trace Table
Here is the step-by-step processing order for the graph from Example 2:
| Step | Extract | dist[] | Relaxed |
|---|---|---|---|
| 1 | S (0) | A:4, B:1 | A, B |
| 2 | B (1) | D:3 | D |
| 3 | D (3) | T:11 | T |
| 4 | A (4) | C:5 | C |
| 5 | C (5) | T:10 | T updated |
| 6 | T (10) | Done | — |
Example 3: Choosing the Right Algorithm
A social network has 500 users. You want to find the fewest number of friend connections between User A and User B. Each friendship is a direct, equal-cost link.
Common Mistakes
Summary at a Glance
| BFS | Dijkstra | |
|---|---|---|
| Best for | Unweighted shortest path | Weighted shortest path (\( w \geq 0 \)) |
| Core structure | FIFO queue | Min-heap / priority queue |
| Key operation | Visit layer by layer | Relax edges greedily |
| Time | \( O(|V| + |E|) \) | \( O((|V|+|E|)\log|V|) \) |
| Space | \( O(|V|) \) | \( O(|V|) \) |
| Negative weights | N/A | Not supported |
| Optimal for equal-weight | ✓ Yes, preferred | Works but slower |
Question 1 of 5
In BFS, what data structure is used to manage the frontier of vertices to explore next?
Question 2 of 5
Dijkstra's algorithm requires that all edge weights are:
Question 3 of 5
You have a graph where every edge has weight \( 1 \). Which algorithm is the best choice for finding the shortest path?
Question 4 of 5
During Dijkstra's algorithm, vertex \( u \) has \( \text{dist}[u] = 7 \). Edge \( (u, v) \) has weight \( 3 \), and currently \( \text{dist}[v] = 12 \). After relaxing this edge, what is \( \text{dist}[v] \)?
Question 5 of 5
What is the time complexity of Dijkstra's algorithm when implemented with a binary min-heap?