Shortest Paths
Input: weighted, directed graph G = (V, E), with weight function w :
E R.
The weight of path p =< v0, v1, . . . , vk > is the sum of the weights of its
constituent edges:
w(p) =
k
X
i=1
w(vi1, vi) .
The shortest-path weight from u to v is
(u, v) = {
min{w(p)} if there is a path p from u to v ,
otherwise .
A shortest path from vertex u to vertex v is then defined as any path
p with weight w(p) = (u, v).
Example
2
c
8
3
7
4
5
b
Solution
3
5
2
c
8
3
7
4
5
b
10
Shortest Paths
Shortest Path Variants
Single Source-Single Sink
Single Source (all destinations from a source s)
All Pairs
Defs:
Let (v) be the real shortest path distance from s to v
Let d(v) be a value computed by an algorithm
Edge Weights
All non-negative
Arbitrary
Note:
Must have no negative cost cycles
Single Source Shortest Paths
Key Property: Subpaths of shortest paths are shortest paths Given a
weighted, directed graph G = (V, E) with weight function w : E R, let
p =< v1, v2, . . . , vk > be a shortest path from vertex v1 to vertex vk and, for
any i and j such that 1 i j k, let pij =< vi, vi+1, . . . , vj > be the subpath
of p from vertex vi to vertex vj . Then, pij is a shortest path from vi to vj .
Note: this is optimal substructure
Corollary 1 For all edges (u, v) E,
(v) (u) + w(u, v)
Corollary 2 Shortest paths follow a tree of edges for which
(v) = (u) + w(u, v)
More precisely, any edge in a shortest path must satisfy
(v) = (u) + w(u, v)
Relaxation
Relax(u, v, w)
1
2
3
if d[v] > d[u] + w(u, v)
then d[v] d[u] + w(u, v)
[v] u (keep track of actual path)
Lemma: Assume that we initialize all d(v) to , d(s) = 0 and execute a
series of Relax operations. Then for all v, d(v) (v).
Lemma: Let P = e1, . . . , ek be a shortest path from s to v. After initialization, suppose that we relax the edges of P in order (but not necessarily
consecutively). Then d(v) = (v).
Example
3
6
5
2
c
1
d
Algorithms
Goal of an algorithm: Relax the edges in a shortest path in order (but
not necessarily consecutively).
Algorithms
Goal of an algorithm: Relax the edges in a shortest path in order (but
not necessarily consecutively).
Bellman-Ford(G, w, s)
1
2
3
4
5
6
7
8
Initialize-Single-Source(G, s)
for i 1 to |V [G]| 1
do for each edge (u, v) E[G]
do Relax(u, v, w)
for each edge (u, v) E[G]
do if d[v] > d[u] + w(u, v)
then return false
return true
Initialize Single Source(G, s)
1
2
3
4
for each vertex v V [G]
do d[v]
[v] nil
d[s] 0
Example
3
6
5
2
c
1
d
Correctness of Bellman Ford
Every shortest path must be relaxed in order
If there are negative weight cycles, the algorithm will return false
Running Time O(V E)
All edges non-negative
Dijkstras algorithm, a greedy algorithm
Similar in spirit to Prims algorithm
Idea: Run a discrete event simulation of breadth-first-search. Figure
out how to implement it efficiently
Can relax edges out of each vertex exactly once.
Dijkstra(G, w, s)
1
2
3
4
5
6
7
8
Initialize-Single-Source(G, s)
S
Q V [G]
while Q 6=
do u Extract-Min(Q)
S S {u}
for each vertex v Adj [u]
do Relax(u, v, w)
Example
2
c
8
3
7
4
5
b
Running Time and Correctness
Correctness of Dijkstras algorithm Dijkstras algorithm, run on a weighted,
directed graph G = (V, E) with nonnegative weight function w and source
s, terminates with d[u] = (s, u) for all vertices u V .
E decrease keys and V delete-mins
O(E log V ) using a heap
O(E + V log V ) using a Fibonacci heap
We will see algorithm using a Relaxed Heap
Shortest Path in a DAG
Dag-Shortest-Paths(G, w, s)
1
2
3
4
5
topologically sort the vertices of G
Initialize-Single-Source0(G, s)
for each u taken in topological order
do for each v Adj[u]
do Relax(u, v, w)
Example
2
c
8
3
7
4
5
b
Correctness and Running Time
Correctness If a weighted, directed graph G = (V, E) has source vertex s and
no cycles, then at the termination of the Dag-Shortest-Paths procedure,
d[v] = (s, v) for all vertices v V , and the predecessor subgraph G is a
shortest-paths tree.
Running Time
Topological sort is linear time
Each edge is relaxed once
No additional data structure overhead
O(V + E) time.