5.7: Weighted Graphs and Dijkstra's Algorithm
( \newcommand{\kernel}{\mathrm{null}\,}\)
Investigate!
In the table, the time it takes to travel between various locations by bus in South Bend is given. Incorporate this information in a graph, and then find shortest paths from the airport to every other location.
| Saint Mary's | Holy Cross | Notre Dame | Ranjan's House | Chocolate Cafe | Crooked Ewe | Airport | |
| Saint Mary's | 0 | 3 | 7 | - | - | - | 17 |
| Holy Cross | 0 | 6 | - | 11 | - | - | |
| Notre Dame | 0 | 8 | - | 14 | - | ||
| Ranjan's House | 0 | - | 15 | 22 | |||
| Chocolate Cafe | 0 | 12 | 15 | ||||
| Crooked Ewe | 0 | - | |||||
| Airport | 0 |
Definition
A graph with a number (usually positive) assigned to each edge is called a weighted graph. (A graph without weights can be thought of as a weighted graph with all weights equal to 1.) We denote the weight between vertices
In the previous example, the weights represented distances. What else could we represent using weights?
In many situations, we want to find a shortest path (or path of least weight) between two locations. Dijkstra's algorithm gives us a way to do this.
Dijkstra's algorithm
- Input: a weighted graph,
, with a source vertex- for each vertex
in : set of all vertices in- while
is not empty: vertex in with smallest distance- remove
from - for each neighbor
of- if
- return
- for each vertex
Remark: If you only want to know the distance from the source to a particular vertex, you can terminate the algorithm when that vertex is removed from
Challenge: Find a big-O estimate for the number of operations (additions and comparisons) used by Dijkstra's algorithm.
Exercise
Saint Mary's once had tunnels connecting various buildings on campus. The tunnels and their lengths are as follows (and definitely not accurate): Regina to McCandless (400 ft), Regina to Student Center (200), McCandless to Student Center (100), McCandless to Angela (500), Student Center to Angela (800), Student Center to Library (1000), Angela to Library (200), Angela to Madeleva (600), Library to Madeleva (300). Find a shortest path from Regina to Madeleva using Dijkstra's Algorithm.
Theorem
Dijkstra's Algorithm finds a shortest path between two vertices in a simple undirected weighted graph.
- Proof
-
We will prove the theorem by induction on
. The first vertex removed from is . In this case and Dijkstra's algorithm tells us that the distance from the source to (ie from to itself) is 0. Therefore, the base case is true.The inductive hypothesis is that for any vertex not in
, the distance assigned to that vertex by the algorithm is in fact the minimum distance from the source to that vertex.Let
be the most recent vertex removed from . By the inductive hypothesis, the algorithm has provided the minimum distance from the source for every vertex in besides .Suppose by contradiction that a shortest path
from to has length less than . In other words, . Let be the first edge along that isn't in and let be the sub-path of from to . Then ,and by the induction hypothesis
.Since
is adjacent to , the algorithm recalculates , so .Combining these inequalities, we see that
. But the algorithm removed from and not , so we must have that . This is a contradiction, and the proof is complete.

