Shortest Paths
Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated
‘length’ cij (cost, time, distance, …).
Determine a path of shortest length between two specified nodes s and t. The length
c(P) of a path P is the sum of its constituent arc lengths: c(P) = Σcij
Applications:
transportation planning (shortest route)
telecommunications (most reliable connection)
scheduling (critical path)
pattern recognition, computer graphics
subproblem to a larger optimization problem
Different Types of SPP
1. Differentiated by the number of origin/destination(source/sink) vertices:
•
from s to t
•
from s to all other vertices
•
to t from all other vertices
•
from i to j
2. Differentiated by the properties of the edge length:
•
Nonnegative edge lengths
•
Negative edge lengths
Assumptions & Algorithms
1.
2.
3.
4.
Integer data
Directed network
There is a path from s to all other vertices
There is no negative length directed cycle
Two algorithms to be studied:
Label-setting – no negative length edges
Label-correcting – negative length edges allowed
KEY IDEA: Distance labels – D(i) = how far away vertex i is from s
Optimality Conditions
Let dj correspond to the distance of the shortest path from vertex s to vertex j.
Certainly ds=0. Moreover, the condition
dj ≤ di + cij (***)
says that if we have a shortest path its length cannot be improved by means of an
edge (i, j) not currently on the path.
This condition (***) is used in both algorithms to efficiently solve the SPP.
FOCUS: single source problems, first with nonnegative costs, then allow negatives.
Shortest Path Trees
Key observation: any subpath of a shortest path is also a shortest path
c
a
t
s
b
When we find a shortest path from s to t, we also find a shortest path from s
to any intermediate vertices.
This leads to a way of representing all shortest paths from vertex s. Find a
shortest path from s to some t; call it P1. Repeat with another vertex t not on
path P1. Repeat until all vertices are used.
This results in a shortest path tree for G. Namely, a directed tree in G whose
paths s to j are all shortest paths from s to j.
Example
2
2
4
4
2
1
1
-1
5
3
3
3
6
0
5
2
4
6 8
1
5
5
4
2
3
5
4
3
• Use two arrays to represent the shortest path tree.
• d(i) records the shortest distance to vertex i.
• p(i) records the predecessor of vertex i in the shortest path
i
1
p(i)
-
d(i)
0
2
3
4
5
6
Label Setting Algorithms
Nonnegative edge lengths: At each step in the algorithm, temporarily labeled
vertices have a distance label of D(i), which is an upper bound on d(i);
permanently labeled vertices are guaranteed to have D(i) = d(i).
Two sets to keep track of: S is the set of permanently labeled vertices;
Ŝ = V – S is the set of temporarily labeled vertices
Vertices are transferred from Ŝ to S, one at a time. Upon termination, all vertices
are permanently labeled. The algorithm processes vertices in increasing distance
from vertex s. At the kth iteration, the kth closest vertex to s will be permanently
labeled.
a
s
c
t
b
S
S^
Topological(Acyclic) or Not Topological
If network has a topological order, examine vertices from 1 to n (reaching).
If no topological order exists, examine vertices in order of increasing distance
from s (Dijkstra’s Algorithm).
DIJKSTRA’S
start with all vertices in Ŝ;
give vertex s a distance D(s) = 0;
give all other vertices the distance D(j) = ∞;
while set S is not full
pick a vertex, i, in Ŝ with the smallest distance;
move that vertex from Ŝ to S;
update distances for vertices reachable from i;
for each (i,j) in E(i)
if D(j) > D(i) + cij then
D(j) = D(i) + cij and pred(j) = i;
Example
2
2
4
4
1
1
4
2
3
6
2
1
3
5
3
i
p(i)
D(i)
1
0
0
2
0
3
0
4
0
5
0
6
0
∞
∞
∞
∞
∞
2
3
4
5
6
move to S
2
2
0
4
1
1
4
2
3
6
2
3
1
5
3
i
p(i)
D(i)
1
0
0
move to S
Example
2
2
2
0
4
4
1
1
4
2
3
6
2
1
3
5
3
i
p(i)
D(i)
1
0
0
2
1
2
3
4
5
6
4
5
6
move to S
2
2
2
0
4
4
1
1
4
3
6
2
3
3
2
1
5
3
i
p(i)
D(i)
1
0
0
2
1
2
3
2
3
move to S
Example
2
2
2
0
4
1
1
4
2
5
3
4
4
2
6
2
3
1
5
3
1
0
0
2
1
2
3
2
3
4
2
6
6
5
6
move to S
3
3
i
p(i)
D(i)
6
4
1
1
6
1
3
2
2
2
3
2
3
0
6
4
i
p(i)
D(i)
1
0
0
2
1
2
3
2
3
4
2
6
5
3
6
6
move to S
Example
2
2
2
0
6
4
4
7
1
1
4
2
3
6
2
3
3
1
5
3
6
i
p(i)
D(i)
1
0
0
2
1
2
3
2
3
4
2
6
5
3
6
6
5
7
Example – Topological
7
3
1
2
4
3
8
4
6
6
4
3
6
5
6
2
3
5
i
D(i)
1
0
D(i)
D(i)
D(i)
D(i)
D(i)
0
0
0
0
0
2
3
4
5
6
∞
∞
∞
∞
∞
Example – Dijkstra’s
7
2
3
1
2
4
4
8
4
6
6
4
3
6
5
1
3
5
i
D(i)
1
0
D(i)
D(i)
D(i)
D(i)
D(i)
0
0
0
0
0
2
3
4
5
6
∞
∞
∞
∞
∞
Example
6
3
1
6
0
1
2
1
1
s
3
2
2
3
5
4
3
1
4
2
2
6
1
9
3
8
4
10
5
7
3
4
12
1
2
6
5
6
6
5
5
7
6
3
11
4
t
1
1
3
5
13
Label Correcting
Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an
associated ‘length’ cij = positive, zero, negative
d(j) = minimum length of a path from s to j
D(j) = distance label, current length of some path from s to j
D(j) = d(j) for all j in V iff D(j) ≤ D(i) + cij for all (i,j) in E(i) and D(s) = 0
In a network with negative edge lengths, maintain a LIST of vertices with
changed labels (Bellman-Ford Algorithm).
Search edge list and update until no distance is changed.
Maintain LIST as:
• Queue – FIFO
• Stack – LIFO
• Dequeue – vertex selected from top; first time vertex is added, it is placed at
the bottom; and anytime after that, a vertex re-enters at top of list
List Structures
•
Queue
leave
enter
•
Stack
leave
enter
•
Dequeue
(Pape)
leave
re-enter
enter
2
-2
1
3
3
2
4
-1
3
2
3
4
6
1
5
queue
3
i
{1} D(i)
{2, 3} D(i)
1
0
2
3
4
5
6
∞
∞
∞
∞
∞
0
2
3
∞
∞
∞
=List D(i)
=List D(i)
0
0
0
=List D(i)
0
=List D(i)
0
0
=List D(i)
=List D(i)
=List D(i)
=List D(i)
0
0
D(i)
0
2
-2
1
3
3
2
4
3
-1
3
2
3
4
6
1
5
i
{1} D(i)
{2, 3} D(i)
1
0
2
3
4
5
6
∞
∞
∞
∞
∞
0
2
3
∞
∞
∞
=List D(i)
=List D(i)
0
0
0
=List D(i)
0
=List D(i)
0
D(i)
0
dequeue
=List D(i)
Negative cycle detection
A negative length cycle will cause some distance labels to decrease without limit.
With label correcting algorithms, negative length cycles can be detected in
a variety of ways.
1. If C is max of all |cij|, then any D(j) falling below -nC.
2. In the queue(FIFO) implementation, more than n-1 updates for a single vertex.
3. If the graph based on predecessors fails to be a tree.
2
-2
1
3
-3
2
4
3
-1
3
2
3
1
5
4
6
All Pairs Shortest Path
1. Apply Dijkstra’s n times, once for each vertex. Good for sparse network.
2. Floyd-Warshall Algorithm finds best in phases. Good for dense network.
Assume network is strongly connected.
Matrix entry D(i, j) = distance from i to j.
Floyd-Warshall
begin
set D(i, j) = cij, pred(i, j) = i for each (i, j) in E;
set D(i, i) = cii, pred(i, i) = i for each i;
for k = 1 to n do
for all i,j = 1 to n do
if D(i, j) > D(i, k) + D(k, j) then
D(i, j) = D(i, k) + D(k, j) and pred(i, j) = pred(k, j);
end
Example
2
1
2
1
3
4
k=0
k=1
4
1
1
D=
0
1
3
∞ ∞
1
1
1
-
-
2
0
1
∞ ∞
2
2
2
-
-
∞ ∞ 0
∞ 2
pred= -
-
3
-
3
∞ 4
3
2
5
k=2
D=
∞ 0
∞
-
4
-
4
-
∞ ∞ ∞ 1
0
-
-
-
5
5
1
1
2
2
2
-
-
-
3
0
1
2
0
1
∞ ∞
∞ 0
4
∞
pred=
0
4
0
-
4
5
Example
2
1
2
1
3
4
4
1
3
1
5
2
k=5
D=
0
1
2
5
4
1
1
2
5
3
2
0
1
4
3
2
2
2
5
3
9
7
0
3
2
pred= 2
4
3
5
3
6
4
5
0
7
2
4
2
4
3
7
5
6
1
0
2
4
2
5
5
What is shortest path from 1 to 4?
What is shortest path from 5 to 3?
© Copyright 2026 Paperzz