230 likes | 245 Views
Learn about finding the shortest path in directed networks, applications, types of problems, algorithms, and optimizations. Explore Label-setting and Label-correcting algorithms with examples.
E N D
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 • 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 c t Key observation: any subpath of a shortest path is also a shortest path 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. a s b
i 1 2 3 4 5 6 p(i) - d(i) 0 Example 2 5 4 2 4 3 2 4 2 2 1 -1 6 1 0 6 8 1 • 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 5 3 5 3 5 3 5 4 3 4
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 4 2 4 2 2 1 3 6 1 2 4 1 3 5 3 move to S 2 4 2 2 0 1 3 6 1 2 4 1 3 5 3 move to S
Example 2 4 2 4 2 2 0 1 3 6 1 2 4 1 3 5 3 move to S 2 4 2 4 2 2 0 1 3 6 1 2 4 1 3 5 3 3 move to S
Example 6 2 4 2 4 2 2 0 1 3 6 1 2 4 1 3 5 3 3 move to S 6 2 4 2 4 2 2 0 1 3 6 1 2 4 1 3 5 6 3 3 move to S
i 1 2 3 4 5 6 p(i) 0 1 2 2 3 5 D(i) 0 2 3 6 6 7 Example 2 6 4 2 4 2 2 7 0 1 3 6 1 2 4 1 3 5 3 6 3
i 1 2 3 4 5 6 D(i) 0 ∞ ∞ ∞ ∞ ∞ D(i) 0 D(i) 0 D(i) 0 D(i) 0 D(i) 0 Example – Topological 7 5 2 4 3 3 6 8 4 1 6 2 6 4 3 3 5 6
i 1 2 3 4 5 6 D(i) 0 ∞ ∞ ∞ ∞ ∞ D(i) 0 D(i) 0 D(i) 0 D(i) 0 D(i) 0 Example – Dijkstra’s 7 5 2 4 4 3 1 8 4 1 6 2 6 4 3 3 5 6
Example 6 3 5 1 6 12 1 1 2 4 6 6 4 2 3 10 5 6 0 3 3 1 1 1 4 s 2 7 5 11 t 2 1 2 2 3 6 6 1 9 5 4 3 1 3 5 3 8 13 4 7 5
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 leave • Queue • Stack • Dequeue (Pape) enter leave enter leave enter re-enter
3 2 4 queue i 1 2 3 4 5 6 3 2 D(i) 0 ∞ ∞ ∞ ∞ -2 -1 6 {1} ∞ 1 3 2 3 1 {2, 3} D(i) 0 3 2 3 ∞ ∞ ∞ 5 4 =List D(i) 0 D(i) 0 =List D(i) 0 =List D(i) 0 =List D(i) 0 =List 0 D(i) =List D(i) 0 =List D(i) 0 =List D(i) 0
3 2 4 i 1 2 3 4 5 6 dequeue 3 2 D(i) 0 ∞ ∞ ∞ ∞ -2 -1 6 {1} ∞ 1 3 2 3 1 {2, 3} D(i) 0 ∞ 3 2 3 ∞ ∞ 5 4 =List D(i) 0 D(i) 0 =List D(i) 0 =List D(i) 0 =List D(i) 0 =List D(i) 0
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. -3 2 4 3 2 -2 -1 1 6 3 2 3 1 3 5 4
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; fork = 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 k = 0 k = 1 4 2 4 1 D = pred= 2 1 1 1 3 3 5 2 k = 2 D = pred=
Example 4 2 4 1 2 1 1 1 3 3 5 2 k = 5 D = pred= What is shortest path from 1 to 4? What is shortest path from 5 to 3?