Shortest Path
4/14/2015 2:57 PM
Weighted Graphs
In a weighted graph, each edge has an associated numerical
value, called the weight of the edge
Edge weights may represent, distances, costs, etc.
Example:
Shortest Paths
A
8
0
In a flight route graph, the weight of an edge represents the
distance in miles between the endpoint airports
4
2
B
8
7
5
E
2
C
3
2
1
9
D
8
F
3
LGA
5
HNL
Shortest Paths
A subpath of a shortest path is itself a shortest path
There is a tree of shortest paths from a start vertex to all the other
vertices
Shortest path between Providence and Honolulu
Example:
Internet packet routing
Flight reservations
Driving directions
Tree of shortest paths from Providence
PVD
ORD
SFO
LGA
HNL
LAX
DFW
Shortest Paths
the graph is connected
the edges are
undirected
the edge weights are
nonnegative
3
Shortest Paths
DFW
MIA
Shortest Paths
4
Edge Relaxation
We grow a “cloud” of vertices,
beginning with s and eventually
covering all the vertices
We store with each vertex v a
label d(v) representing the
distance of v from s in the
subgraph consisting of the cloud
and its adjacent vertices
At each step
LAX
MIA
Dijkstra’s Algorithm
The distance of a vertex
v from a vertex s is the
length of a shortest path
between s and v
Dijkstra’s algorithm
computes the distances
of all the vertices from a
given start vertex s
Assumptions:
PVD
ORD
SFO
LGA
HNL
2
Property 2:
Applications
MIA
Shortest Paths
Property 1:
Length of a path is the sum of the weights of its edges.
Example:
DFW
Shortest Path Properties
Given a weighted graph and two vertices u and v, we want to
find a path of minimum total weight between u and v.
LAX
1
Shortest Path Problem
PVD
ORD
SFO
We add to the cloud the vertex
u outside the cloud with the
smallest distance label, d(u)
We update the labels of the
vertices adjacent to u
5
Consider an edge e (u,z)
such that
d(u) 50
u is the vertex most recently
added to the cloud
z is not in the cloud
s
The relaxation of edge e
updates distance d(z) as
follows:
u
e
z
d(u) 50
d(z) min{d(z),d(u) weight(e)}
s
Shortest Paths
u
d(z) 75
d(z) 60
e
z
6
1
Shortest Path
4/14/2015 2:57 PM
Example
A
8
Example (cont.)
0
4
A
8
2
B
8
7
2
C
1
D
9
E
F
A
8
4
B
5
8
7
5
E
2
C
4
8
7
5
E
2
1
D
8
A
2
1
9
D
11
F
3
B
5
A priority queue stores
the vertices outside the
cloud
Key: distance
Element: vertex
Locator-based methods
insert(k,e) returns the
index of e
replaceKey(l,k) changes
the key of an item
We store two labels
with each vertex:
7
7
C
3
5
2
1
9
E
D
8
F
3
5
2
7
7
5
E
C
3
Distance (d(v) label)
index in priority queue
2
1
9
D
8
F
parent edge in the
shortest path tree
In the edge relaxation
step, we update the
parent label
0
4
2
B
3
7
7
7
C
2
3
5
2
1
9
D
8
E
5
3
5
F
Shortest Paths
8
Analysis
Graph operations
Algorithm DijkstraDistances(G, s)
Q new heap-based priority queue
for all v G.vertices()
if v s
setDistance(v, 0)
else
setDistance(v, )
l Q.insert(getDistance(v), v)
setLocator(v,l)
while Q.isEmpty()
u Q.removeMin()
for all e G.incidentEdges(u)
// relax edge e
z G.opposite(u,e)
r getDistance(u) weight(e)
if r getDistance(z)
setDistance(z,r)
Q.replaceKey(getLocator(z),r)
Method incidentEdges is called once for each vertex
Label operations
We set/get the distance and locator labels of vertex z O(deg(z)) times
Setting/getting a label takes O(1) time
Priority queue operations
Each vertex is inserted once into and removed once from the priority
queue, where each insertion or removal takes O(log n) time
The key of a vertex in the priority queue is modified at most deg(w)
times, where each key change takes O(log n) time
Dijkstra’s algorithm runs in O((n m) log n) time provided the
graph is represented by the adjacency list structure
Recall that
v deg(v) 2m
The running time can also be expressed as O(m log n) since the
graph is connected
9
Shortest Paths
10
Why Dijkstra’s Algorithm
Works
Extension
Using the template
method pattern, we
can extend Dijkstra’s
algorithm to return a
tree of shortest paths
from the start vertex
to all other vertices
We store with each
vertex a third label:
A
8
4
Shortest Paths
4
0
Dijkstra’s Algorithm
B
2
Shortest Paths
0
2
5
F
8
3
2
C
3
2
9
0
A
8
3
2
B
4
2
2
3
0
Algorithm DijkstraShortestPathsTree(G, s)
…
Dijkstra’s algorithm is based on the greedy
method. It adds vertices by increasing distance.
for all v G.vertices()
…
setParent(v, )
…
for all e G.incidentEdges(u)
// relax edge e
z G.opposite(u,e)
r getDistance(u) weight(e)
if r getDistance(z)
setDistance(z,r)
setParent(z,e)
Q.replaceKey(getLocator(z),r)
Shortest Paths
11
Suppose it didn’t find all shortest
distances. Let F be the first wrong
vertex the algorithm processed.
When the previous node, D, on the
true shortest path was considered,
its distance was correct.
But the edge (D,F) was relaxed at
that time!
Thus, so long as d(F)>d(D), F’s
distance cannot be wrong. That is,
there is no wrong vertex.
Shortest Paths
A
8
0
4
2
B
2
7
7
C
3
5
E
2
1
9
D
8
F
3
5
12
2
Shortest Path
4/14/2015 2:57 PM
Shortest Paths
Shortest Paths: Failed Attempts
Shortest path problem. Given a directed graph G = (V, E), with edge
weights cvw, find shortest path from node s to node t.
Dijkstra. Can fail if negative edge costs.
allow negative weights
2
u
3
s
v
1
Ex. Nodes represent agents in a financial setting and cvw is cost of
transaction in which we buy from agent v and sell immediately to w.
-6
t
Re-weighting. Adding a constant to every edge weight can fail.
s
10
2
9
3
18
6
30
15
11
5
-8
7
19
4
s
6
16
20
5
2
6
-16
6
t
44
6
3
5
2
0
-3
6
3
t
13
14
Shortest Paths: Negative Cost Cycles
Dynamic Programming
Negative cost cycle.
-6
-4
Subproblem Property: The problem can be recursively defined by the
subproblem of the same kind.
7
Trade space for time: A table is used to store the solutions of the
subproblems (the meaning of “programming” before the age of
computers is “table”).
Observation. If some path from s to t contains a negative cost cycle,
there does not exist a shortest s-t path; otherwise, there exists one
that is simple.
s
W
t
c(W) < 0
15
Designing a DP solution
How are the subproblems defined?
Where are the solutions stored?
How are the base values computed?
How do we compute each entry from other entries in the table?
What is the order in which we fill in the table?
Two DP algorithms for All-pairs shortest paths
Both are correct. Both produce correct values for all-pairs
shortest paths.
The difference is the subproblem formulation, and hence in the
running time.
The reason both algorithms are given is to remind you how to do
DP algorithms!
But, be prepared to provide one or both of these algorithms,
and to be able to apply it to an input (on some exam, for
example).
3
Shortest Path
4/14/2015 2:57 PM
Dynamic Programming
First attempt: let {1,2,…,n} denote the set of vertices.
Subproblem formulation:
M[i,j,k] = min length of any path from i to j that uses at most
k edges.
All paths have at most n-1 edges, so 1 ≤ k ≤ n-1.
When k=1, M[i,j,1] = w[i,j], the edge weight from i to j.
Minimum paths from i to j are found in M[i,j,n-1]
How to set M[i,j,k] from other entries, for k>1?
Consider a minimum weight path from i to j that has at most k edges.
Case 1: The minimum weight path has at most k-1 edges.
– M[i,j,k] = M[i,j,k-1]
Case 2: The minimum weight path has exactly k edges.
– M[i,j,k] = min{ M[i,x,k-1] + w(x,j) : x in V}
Combining the two cases:
M[i,j,k] = min{min{M[i,x,k-1] + w(x,j) : x in V}, M[i,j,k-1]}
Question: How to set M[i,j,k] from other entries?
Finishing the design
Where is the answer stored?
Pseudo-Code and Running time analysis
How are the base values computed?
How do we compute each entry from other entries?
What is the order in which we fill in the matrix?
Running time?
for j = 1 to n for i = 1 to n
M[i,j,1] = W[i,j]
for k = 2 to n-1
for j = 1 to n
for i = 1 to n
M[i,j,k] = min{min{M[i,x,k-1] + w(x,j): x in V}, M[i,j,k-1]}
How many entries do we need to compute? O(n3)
1 ≤ i ≤ n; 1 ≤ j ≤ n; 1 ≤ k ≤ n-1
How much time does it take to compute each entry? O(n)
Total time: O(n4)
Next DP approach
Try a new subproblem formulation!
Q[i,j,k] = minimum weight of any path from i to j that uses
internal vertices drawn from {1,2,…,k}.
Designing a DP solution
How are the subproblems defined?
Where is the answer stored?
How are the base values computed?
How do we compute each entry from other entries?
What is the order in which we fill in the matrix?
4
Shortest Path
4/14/2015 2:57 PM
Solving subproblems
Q[i,j,k] = minimum weight of any path from i to j that uses
internal vertices (other than i and j) drawn from {1,2,…,k}.
Base cases: Q[i,j,0] = w[i,j] for all i,j
Minimum paths from i to j are found in Q[i,j,n]
Once again, O(n3) entries in the matrix
Q[i,j,k] = minimum weight of any path from i to j that uses
internal vertices drawn from {1,2,…,k}.
Such minimum cost path either includes vertex k or does not
include vertex k.
If the minimum cost path P includes vertex k, then you can
divide P into the path P1 from i to k, and P2 from k to j.
What is the weight of P1?
What is the weight of P2?
New DP algorithm
Solving subproblems
Q[i,j,k] = minimum weight of any path from i to j that uses
internal vertices drawn from {1,2,…,k}.
P is a minimum cost path from i to j that uses vertex k, and has
all internal vertices from {1,2,…k}.
Path P1 from i to k, and P2 from k to j.
The weight of P1 is Q[i,k,k-1] (why??).
The weight of P2 is Q[k,j,k-1] (why??).
for j = 1 to n
for i = 1 to n
Q[i,j,0] = w[i,j]
for k= 1 to n
for j = 1 to n
for i = 1 to n
Q[i,j,k] = min{Q[i,j,k-1],
Q[i,k,k-1] + Q[k,j,k-1]}
Each entry only takes O(1) time to compute
There are O(n3) entries
Hence, O(n3) time.
Thus the weight of P is Q[i,k,k-1] + Q[k,j,k-1].
Reusing the space
// Use R[i,j] for Q[i,j,0], Q[i,j,1], …, Q[i,j,n].
for j = 1 to n
for i = 1 to n
R[i,j] = W[i,j];
for k= 1 to n
for j = 1 to n
for i = 1 to n
R[i,j] = min{R[i,j], R[i,k] + R[k,j]}
How to check negative cycles
// Use R[i,j] for Q[i,j,0], Q[i,j,1], …, Q[i,j,n].
for j = 1 to n
for i = 1 to n
R[i,j] = W[i,j];
for k= 1 to n
for j = 1 to n
for i = 1 to n
R[i,j] = min{R[i,j], R[i,k] + R[k,j]};
for i = 1 to n
if (R[i,i] < 0) print(“There is a negative cycle”);
5
Shortest Path
4/14/2015 2:57 PM
Detecting Negative Cycles: Application
Currency conversion. Given n currencies and exchange rates between
pairs of currencies, is there an arbitrage opportunity?
Remark. Fastest algorithm very valuable!
8
$
F
1/7
800
4/3
2/3
3/10
2
3/50
IBM
1/10000
£
170
DM
56
¥
31
6
© Copyright 2026 Paperzz