Many practical problems can be modeled using graphs with weights assigned to their edges. Graphs that have a number assigned to each edge are called weighted graphs.
For instance, an airline system can be modeled where vertices represent cities and edges represent flights:
Assigning distances between cities yields a distance model.
Assigning flight times yields a time model.
Assigning fares yields a cost model.
Similarly, computer networks can be modeled using weighted graphs representing connection distances, response times, or monthly lease costs:
Length of a Path in a Weighted Graph
Let the length of a path in a weighted graph be the sum of the weights of the edges in that path:
length of path (e1,e2,…,ek)=i=1∑kw(ei)(Note: In an unweighted graph, length denotes the number of edges. In a weighted graph, length refers to the sum of the edge weights.)
A shortest path between two vertices is a path of minimum length connecting them.
Dijkstra's Algorithm
Dijkstra's algorithm (discovered by Edsger W. Dijkstra in 1959) finds the length of a shortest path between two vertices a and z in an undirected, connected, simple weighted graph with positive edge weights.
Intuitive Approach
Formal Description of Dijkstra's Algorithm
Dijkstra's algorithm builds a distinguished set of vertices S. Initially, S=∅.
Each vertex v maintains a label L(v) representing the current estimate of the shortest path length from a to v.
Initialization: L(a)=0, and L(v)=∞ for all v=a.
At each iteration:
Select a vertex u∈/S with minimal label L(u).
Add u to S.
For every neighbor v∈/S, update its label:
L(v):=min{L(v),L(u)+w(u,v)}
The algorithm terminates when the target vertex z is added to S.
Correctness and Complexity
Proof (by Mathematical Induction):
Inductive Hypothesis: At the end of the kth iteration:
For every vertex v∈Sk, L(v) is the exact length of a shortest path from a to v.
For every vertex u∈/Sk, L(u) is the length of a shortest path from a to u whose intermediate vertices all lie in Sk.
Basis Step (k=0):S0=∅. L(a)=0 is correct, and L(v)=∞ for v=a is correct since no paths contain only vertices from ∅.
Inductive Step: Assume the hypothesis holds after k iterations. Let v be the vertex added to S at step k+1 (minimal label among vertices not in Sk).
If there existed a shorter path from a to v, it would have to leave Sk at some first vertex u∈/Sk. But by induction, the subpath to u would have length at least L(u)≥L(v), a contradiction. Thus L(v) is the true shortest distance to v, proving condition (1).
Updating L(u) for vertices remaining outside Sk+1 via min{L(u),L(v)+w(v,u)} correctly considers all shortest paths through Sk∪{v}, maintaining condition (2).
Therefore, when z is added to S, L(z) is the true shortest path length. ■
Complexity Analysis:
There are at most n−1 iterations of the while loop.
In each iteration:
Finding the vertex with minimal label takes at most n−1 comparisons.
Updating the labels of the remaining vertices takes at most n−1 additions and n−1 comparisons (at most 2(n−1) operations).
Total operations: at most (n−1)×3(n−1)=O(n2).
(Note: Using a Fibonacci heap or min-priority queue, Dijkstra's algorithm can be implemented in O(m+nlogn) time, where m is the number of edges.)
The Traveling Salesperson Problem (TSP)
The traveling salesperson problem (TSP) asks for the circuit of minimum total weight in a weighted, complete, undirected graph that visits every vertex exactly once and returns to the starting vertex. This is equivalent to finding a Hamilton circuit of minimum total weight.
Example: Five Michigan/Ohio Cities
Consider five cities: Detroit, Grand Rapids, Kalamazoo, Saginaw, and Toledo, with pairwise distances given in Figure 5:
Starting at Detroit, there are (5−1)!/2=24/2=12 distinct Hamilton circuits to examine:
The optimal route is Detroit–Toledo–Kalamazoo–Grand Rapids–Saginaw–Detroit (or its reverse) with a minimum total distance of 458 miles.
Computational Complexity of TSP
Exhaustive Search: For n vertices, choosing a starting vertex leaves (n−1)! circuits. Accounting for direction reversal yields:
2(n−1)! circuits
For n=25, this requires evaluating 24!/2≈3.1×1023 circuits. At 1 nanosecond per circuit, exhaustive search would take approximately 10 million years.
NP-Completeness: TSP is an NP-complete problem; no polynomial-time algorithm is known for finding exact solutions.
Approximation Algorithms: In practice, approximation algorithms are used:
An approximation algorithm guarantees a tour of length W′ such that W≤W′≤cW, where W is the optimal length and c is a constant approximation ratio.
When the edge weights satisfy the triangle inequality (w(u,v)≤w(u,x)+w(x,v)), polynomial-time algorithms exist with c=3/2 (Christofides' algorithm).
For practical applications, heuristics solve TSP instances with thousands of vertices to within 2% of optimal in minutes.