Download presentation
Presentation is loading. Please wait.
Published byHelen Göransson Modified over 6 years ago
1
EMIS 8374 Shortest Path Problems: Introduction Updated 9 February 2008
2
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
3
A Network with Negative Cycles
3 2 4 1 1 2 1 6 3 -3 -3 3 3 5 1 7
4
Shortest Path Problems
Acyclic Networks
5
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
6
A Network with no Topological Ordering
1 2 3
7
Theorem A network is acyclic if and only if it has a topological ordering of its nodes. Complexity of T.O. Algorithm: O(m)
8
T.O. Algorithm 2 4 1 6 1 3 5
9
T.O. Algorithm 2 4 3 5 6 2
10
T.O. Algorithm 2 4 6 5 3
11
T.O. Algorithm 4 5 2 4 6 6
12
Topological Ordering 4 5 2 4 1 6 1 6 3 5 2 3
13
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;
14
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
15
Reaching Algorithm Example
2 4 5 7 1 1 2 6 1 6 5 2 3 -2
16
Examine A(1) 7 2 4 5 7 1 1 2 6 1 6 5 2 3 -2 5
17
Examine A(2) 7 6 2 4 5 7 1 1 2 6 1 6 5 2 3 -2 5 3
18
Examine A(3) 6 5 2 4 5 7 1 1 2 6 9 1 6 5 2 3 -2 5 3
19
Examine A(4) 5 7 2 4 5 7 1 1 2 6 9 1 6 5 2 3 -2 5 3
20
Examine A(5) 5 7 2 4 5 7 1 1 2 6 8 9 1 6 5 2 3 -2 5 3
21
Examine A(5) 5 7 2 4 5 7 1 1 2 6 8 1 6 5 2 3 -2 5 3
22
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)
23
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
24
Shortest Path Tree 7 6 7 5 2 4 5 1 1 2 6 8 5 2 3 -2 5 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.