01 / 20

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.

Finite Mathematics

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.

4 2 5 1 7 3 6 A B C D E

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.

Navigation
GPS apps compute fastest routes across road networks with millions of intersections.
Networking
Internet routers use shortest-path protocols (OSPF) to forward data packets efficiently.
Logistics
Delivery companies minimize fuel and time by solving shortest-path variants daily.

Key Terminology

1
Path: A sequence of vertices connected by edges, with no vertex repeated.
2
Path weight: The sum of all edge weights along a path: \( w(p) = \sum_{i=1}^{k} w(e_i) \).
3
Shortest path: A path from source \( s \) to destination \( t \) with the minimum total weight.
4
Distance \( d(s,t) \): The weight of the shortest path from \( s \) to \( t \). If no path exists, \( d(s,t) = \infty \).

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.

s u v t sub-path u → v shortest path s → t

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.

d(s, u) w(u, v) d(s, v) s u v

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

1
Initialize: Set the distance to the source as \( 0 \). Set all other distances to \( \infty \). Mark all vertices as unvisited.
2
Select: Pick the unvisited vertex with the smallest current distance.
3
Relax neighbors: For each neighbor \( v \), check if \( d(u) + w(u,v) < d(v) \). If so, update \( d(v) \).
4
Mark visited: The selected vertex's distance is now finalized.
5
Repeat steps 2-4 until all vertices are visited or the destination is reached.

How to Apply It: Checklist

1
Draw the graph. Label every vertex and every edge weight clearly.
2
Create a distance table. One column per vertex. Initialize source to \( 0 \), all others to \( \infty \).
3
Process vertices greedily. Always pick the smallest unvisited distance next.
4
Relax edges. Update a neighbor's distance whenever you find a cheaper route through the current vertex.
5
Trace the path. When finished, read the shortest path by following the predecessor pointers backward from the destination.

Example 1: Find Shortest Path A to E

4 2 5 1 7 6 3 A B C D 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

2 6 A B C D E
Shortest path: \( A \to C \to E \)

Example 2: A Larger Graph

3 7 1 4 8 2 6 4 5 S A B C D T

Example 2: Solution

3 1 2 4 S A B C D T
\( S \to A \to C \to D \to T = 3 + 1 + 2 + 4 = 10 \)

Common Mistakes

✕ Confusing Hops with Weight
Choosing the path with fewest edges instead of the smallest total weight. BFS counts hops; Dijkstra's counts weight.
✕ Not Updating Distances
Forgetting to relax a neighbor when a cheaper route is found. Always check: is \( d(u) + w(u,v) < d(v) \)?
✕ Re-visiting Finalized Nodes
Going back to a vertex that has already been marked as visited. In Dijkstra's, once finalized, a vertex's distance never changes.

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: