Shortest Path
Shortest Path
Introduction
• Given a directed graph G with edge weights,
find
– The shortest path from a given vertex s to all other
vertices (Single Source Shortest Paths)
– The shortest paths between all pairs of vertices (All
Pairs Shortest Paths)
• where the length of a path is the sum of its
edge weights.
Shortest Paths: Applications
• Flying times between cities
• Distance between street corners
• Cost of doing an activity
– Vertices are states
– Edge weights are costs of moving between
states
Shortest Paths: Algorithms
• Single-Source Shortest Paths (SSSP)
– Dijkstra’s
– Bellman-Ford
– DAG Shortest Paths
• All-Pairs Shortest Paths (APSP)
– Floyd-Warshall
– Johnson’s
A Fact About Shortest Paths
u x y v
Example
• Imagine you are
9
D 4 planning a road
E C
trip from one
2 6 another city.
14 F 15 • Your task is to find
9 8 the shortest path
7
A B distance to reach
your destination.
E C
2 6
14 F 15
9 8
7
A B
• Clearly, calculating the shortest path does not always result in the
shortest distance.
• Unless the distance between all cities is the same, BFS will not
always compute the shortest path.
Analysis of previous approaches
• Depth First Search (DFS):
– We could perform a DFS on the map by using a
stack.
– Similar to BFS we accumulate the distances as we
visit each city in depth first order.
– Once we reach the distances we know the total
distance and we can reconstruct the path.
Example
D 4
9
E C
2 6
15
14 F
9 8
7
A B
E C
2 6
14 F 15
9 8
7
A B
E C
2 6
14 F 15
9 8
7
A B
D 4
9
E C
2 6
14 F 15
9 8
7
A B
E C
2 6
14 F 15
9 8
7
A B
E C
2 6
14 F 15
9 8
7
A B
E C
2 6
14 F 15
9 8
7
A B
21
12/21/2021
Example
D 4
9
E C
2 6
14 F 15
9 8
7
A B
Example
D 4
(14, A) 9
E C
2 6
14 F 15
9 8
7
A B
• Current vertex is A.
• A’s neighbours are updated.
• A is finalized.
Example
D 4
(14, A) 9
E C (22, B)
2 6
14 F 15
9 8
7
A B
• Current vertex is B.
• B’s neighbours are updated.
• B is finalized.
Example
D 4
(11, F) 9
E C (15, F)
2 6
14 F 15
9 8
7
A B
• Current vertex is F.
• F’s neighbours are updated.
• F is finalized.
Example
(20, F)
D 4
(11, F) 9
E C (15, F)
2 6
14 F 15
9 8
7
A B
• Current vertex is E.
• E’s neighbours are updated.
• E is finalized.
Example
(19, C)
D 4
(11, F) 9
E C (15, F)
2 6
14 F 15
9 8
7
A B
• Current vertex is C.
• C’s neighbours are updated.
• C is finalized.
Example
(19, C)
D 4
(11, F) 9
E C (15, F)
2 6
14 F 15
9 8
7
A B
• Current vertex is D.
• D’s neighbours are updated.
• D is finalized. Algorithm done.
Example
(19, C)
D 4
(11, F) 9
E C (15, F)
2 6
14 F 15
9 8
7
A B
30
12/21/2021
Time Complexity: Using List
The simplest implementation of the Dijkstra's
algorithm stores vertices in an ordinary linked list
or array
– Good for dense graphs (many edges)
• |V| vertices and |E| edges
• Initialization O(|V|)
• While loop O(|V|)
– Find and remove min distance vertices O(|V|)
– Potentially |E| updates
• Update costs O(1)
• Reconstruct path O(|E|)
Total time O(|V2| + |E|) = O(|V2| )
31
Time Complexity: Priority Queue
For sparse graphs, (i.e. graphs with much less than |V2|
edges) Dijkstra's implemented more efficiently by
priority queue
• Initialization O(|V|) using O(|V|) build Heap
• While loop O(|V|)
• Find and remove min distance vertices O(log |V|)
using O(log |V|) deleteMin
• Potentially |E| updates
• Update costs O(log |V|) using decreaseKey
Total time O(|V|log|V| + |E|log|V|) = O(|E|log|V|)
|V| = O(|E|) assuming a connected graph
32
Correctness of Dijkstra's Alg.
P, path in Ti+1
Ti Ti+1
s from s to u
x u
a P', another
path from s to u
b
(a,b) is first edge in P' that
leaves Ti
35
Correctness of Dijkstra's Alg
• Otherwise, it is fine
Cycles
• Shortest path cannot have cycles inside
– Negative cycles : already eliminated
– Positive cycles: can be removed
– 0-weight cycles: can be removed
40
The Bellman-Ford Algorithm
41
Bellman-Ford: Algorithm
BELLMAN-FORD(G, w, s)
1 For each vertex v V[G] do //INIT_SINGLE_SOURCE
2 d[v]
3 p[v] NIL
4 d[s] 0
5 for i 1 to |V[G]|-1 do > each iteration is a “pass”
6 for each edge (u,v) in E[G] do
7 if d[u] + w(u,v) < d[v] then
8 d[v] := d[u] + w(u,v)
9 parent[v] := u
10 for each edge (u,v) in E[G] do > check for negative cycles
11 if d[v] > d[u] + w(u,v) then
11 return FALSE
12 return TRUE
Running Time of Bellman-Ford
• O(V) iterations of outer for loop
• O(E) iterations of inner for loop
• O(VE) time total
43
Example
Correctness of Bellman-Ford
• First prove that if G = (V, E) contains no negative-
weight cycles, then after the Bellman-Ford
algorithm executes, d[v] = d(s, v) for all v V.
• Let v be reachable from s, and let p = < v0 , v1 , . . .
, vk> be a shortest path from s to v, where v0 = s
and vk = v.
• Since p is acyclic, so k≤ |V| − 1 edges.
• Each iteration of the for loop relaxes all edges:
• First iteration relaxes (v0, v1).
• Second iteration relaxes (v1, v2).
• kth iteration relaxes (vk-1, vk).
By the path-relaxation property (if d[u] + w(u,v) <
d[v] then d[v] := d[u] + w(u,v)),
d[v] = d[vk ] = δ(s, vk ) = δ(s, v)
• Suppose there is no negative-weight cycle
reachable from s.
• At termination, for all (u, v) ∈ E,
d[v] = δ(s, v)
≤ δ(s, u) + w(u, v) (triangle inequality)
= d[u] + w(u, v) .
• So BELLMAN-FORD returns TRUE.
• Now suppose there exists negative-weight cycle c =
v0, v1, . . . , vk, where v0= vk , reachable from s.
• Then
• So
=
• Hence
55
Dynamic Programming
(1) Characterize the structure of an optimal solution.
(2) Recursively define the value of an optimal
solution.
(3) Compute the value of an optimal solution in a
bottom-up manner.
(4) Construct an optimal solution from information
constructed in (3).
56
Floyd-Warshall
•We will now investigate a dynamic
programming solution that solved the problem in
O(n3) time for a graph with n vertices.
This algorithm is known as the Floyd-Warshall
algorithm.
57
The structure of a shortest path:
• Given a path p=(v1, v2,…, vm) in the graph, we will
call the vertices vk with index k in {2,…,m-1} the
intermediate vertices of p.
• For any pair of vertices i,j V, consider all paths
from i to j whose intermediate vertices are all drawn
from {1,2,…,k},and let p be a minimum-weight path
from among them.
• The Floyd-Warshall algorithm exploits a relationship
between path p and shortest paths from i to j with all
intermediate vertices in the set {1,2,…,k-1}.
58
Cont.
• Consider k is not an intermediate vertex of
path p.
• All intermediate vertices of path p are in the
set {1,2,…,k-1}. Thus dij(k) = dij(k-1) .
59
All intermediate vertices in {1,2,…,k-1}
p1 p2
k
i j
61
Computing the shortest-path
Floyd//Computes shortest distance between all pairs of
//nodes, and saves P to enable finding shortest paths
1. D0 W // initialize D array to W [ ]
2. P 0 // initialize P array to [0]
3. for k 1 to n
4. do for i 1 to n
5. do for j 1 to n
6. if (Dk-1[ i, j ] > Dk-1 [ i, k ] + Dk-1 [ k, j ] )
7. then Dk[ i, j ] Dk-1 [ i, k ] + Dk-1 [ k, j ]
8. P[ i, j ] k;
9. else Dk[ i, j ] Dk-1 [ i, j ]
62
Example
1 2 3
1 0 4 5
W= D0 =
2 2 0
1 5
3 -3 0
4 2 3
1 2 3
-3 1 0 0 0
2
P= 2 0 0 0
3 0 0 0
1 1 2 3
5 D0 =
1 0 4 5 k=1
4 3 Vertex 1 can be
2 2 2 0
2
-3
3 -3 0
intermediate
1 2 3
node
1 0 4 5 D1[2,3] = min( D0[2,3], D0[2,1]+D0[1,3] )
D1 =
2 2 0 7
= min (, 7)
=7
3 -3 0
1 2 3
D1[3,2] = min( D0[3,2], D0[3,1]+D0[1,2] )
1 0 0 0 = min (-3,)
P= 2 0 0 1 = -3
3 0 0 0
1 2 3
1 5 D1 = 1
0 4 5
4 3 2 2 0 7
k=2
2
-3 3
Vertices 1, 2 can
2 -3 0
be intermediate
1 2 3
1 0 4 5 D2[1,3] = min( D1[1,3], D1[1,2]+D1[2,3] )
D2 =
2 2 0 7
= min (5, 4+7)
=5
3 -1 -3 0
1 2 3
1 0 0 0 D2[3,1] = min( D1[3,1], D1[3,2]+D1[2,1] )
P= 2 0 0 1 = min (, -3+2)
3 2 0 0 = -1
1 1 2 3
5 D2 =
1 0 4 5
4 3
2 2 2 0 7 k=3
2
-3 Vertices 1, 2, 3 can
3 -1 -3 0
be intermediate
1 2 3
1 0 2 5
D3 = D3[1,2] = min(D2[1,2], D2[1,3]+D2[3,2] )
2 2 0 7 = min (4, 5+(-3))
3 -1 -3 0 =2
1 2 3
1 0 3 0 D3[2,1] = min(D2[2,1], D2[2,3]+D2[3,1] )
= min (2, 7+ (-1))
P= 2 0 0 1
=2
3 2 0 0
Johnsons Algorithm
• Let the graph be sparse.
• Makes clever use of Bellman-Ford and Dijkstra
to do All-Pairs-Shortest-Paths efficiently on
sparse graphs.
• It is a better idea to run Dijkstras algorithm V
times
• However, Dijkstras algorithm operates only for
edges with nonnegative weights
• Can we change the edge weights to make
them nonnegative?
• Reweight the edges so that:
1. No edge weight is negative.
2. Shortest paths are preserved. (A shortest path
in the original graph is still one in the new,
reweighted graph.)