EMIS 8374 Shortest Path Problems: Introduction Updated 9 February 2008
Assumptions All arc costs (cij) are integers. The network contains a directed path from the source node to every other node in the network. No negative (directed) cycles
A Network with Negative Cycles 3 2 4 1 1 2 1 6 3 -3 -3 3 3 5 1 7
Shortest Path Problems Acyclic Networks
Topological Ordering Renumber the nodes so that for every arc (i,j), i < j Let k := 1 Find a node i with in-degree = 0 If no such node then quit (no T.O.) Else let order(i) := k, k := k+1 Remove i and all incident arcs (i,j) Goto Step 2
A Network with no Topological Ordering 1 2 3
Theorem A network is acyclic if and only if it has a topological ordering of its nodes. Complexity of T.O. Algorithm: O(m)
T.O. Algorithm 2 4 1 6 1 3 5
T.O. Algorithm 2 4 3 5 6 2
T.O. Algorithm 2 4 6 5 3
T.O. Algorithm 4 5 2 4 6 6
Topological Ordering 4 5 2 4 1 6 1 6 3 5 2 3
The Reaching Algorithm: Initialization Number the nodes by a topological ordering. Let d(s) := 0; d(i) := for all other nodes. d(i) indicates the length of the shortest path found so far from s to i. When d(i) = , it means we have not yet found a path from s to i. Pred(s) := 0;
The Reaching Algorithm: Main Loop for i = 1 to n do for (i,j) in A(i) do if d(j) > d(i) + cij then begin let d(j) := d(i) + cij pred(j) := i end
Reaching Algorithm Example 2 4 5 7 1 1 2 6 1 6 5 2 3 -2
Examine A(1) 7 2 4 5 7 1 1 2 6 1 6 5 2 3 -2 5
Examine A(2) 7 6 2 4 5 7 1 1 2 6 1 6 5 2 3 -2 5 3
Examine A(3) 6 5 2 4 5 7 1 1 2 6 9 1 6 5 2 3 -2 5 3
Examine A(4) 5 7 2 4 5 7 1 1 2 6 9 1 6 5 2 3 -2 5 3
Examine A(5) 5 7 2 4 5 7 1 1 2 6 8 9 1 6 5 2 3 -2 5 3
Examine A(5) 5 7 2 4 5 7 1 1 2 6 8 1 6 5 2 3 -2 5 3
Complexity of the Reaching Algorithm for i = 1 to n do for (i,j) in A(i) do if d(j) > d(i) + cij then begin let d(j) := d(i) + cij pred(j) := i; end Outer loop (for i := i+1) executes n times. Examining an arc is O(1). Comparison, updates of d(j) and pred(j) are O(1). Each arc is examined once. O(n+m) = O(m)
Shortest Path Tree We can store a set of shortest paths from s to all other nodes in a tree Use the pred(j) label to keep track of the last arc (i,j) that sets d(j) = d(i) + cij
Shortest Path Tree 7 6 7 5 2 4 5 1 1 2 6 8 5 2 3 -2 5 3