Presentation is loading. Please wait.

Presentation is loading. Please wait.

Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated ‘length’ c ij (cost, time, distance, …). Determine a path of shortest.

Similar presentations


Presentation on theme: "Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated ‘length’ c ij (cost, time, distance, …). Determine a path of shortest."— Presentation transcript:

1 Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated ‘length’ c ij (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) = Σc ij Applications: transportation planning (shortest route) telecommunications (most reliable connection) scheduling (critical path) pattern recognition, computer graphics subproblem to a larger optimization problem Shortest Paths

2 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 Different Types of SPP

3 1.Integer data 2.Directed network 3.There is a path from s to all other vertices 4.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 Assumptions & Algorithms

4 Let d j correspond to the distance of the shortest path from vertex s to vertex j. Certainly d s =0. Moreover, the condition d j ≤ d i + c ij (***) 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. Optimality Conditions

5 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 P 1. Repeat with another vertex t not on path P 1. 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. s b a ct Shortest Path Trees

6 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 6 1 2 3 5 4 2 1 4 35 3 4 2 5 6 1 3 5 2 0 3 5 8 4 24 Example -p(i) 654321i 0d(i)

7 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 k th iteration, the k th closest vertex to s will be permanently labeled. S S ^ s b a ct Label Setting Algorithms

8 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) + c ij then D(j) = D(i) + c ij and pred(j) = i; Topological(Acyclic) or Not Topological

9 6 1 2 3 5 4 2 1 4 41 2 3 2 i123456 p(i)000000 D(i)0 ∞∞∞∞∞ 6 1 2 3 5 4 2 1 41 2 3 2 i123456 p(i)0 D(i)0 0 3 3 Example move to S

10 6 1 2 3 5 4 2 1 4 41 2 3 2 i123456 p(i)01 D(i)02 6 1 2 3 5 4 2 1 41 2 3 2 i123456 p(i)012 D(i)023 0 0 2 2 4 3 3 3 Example move to S

11 6 1 2 3 5 4 2 1 4 41 2 3 2 i123456 p(i)0122 D(i)0236 6 1 2 3 5 4 2 1 41 2 3 2 i123456 p(i)01223 D(i)02366 0 0 2 24 33 3 6 6 3 6 Example move to S

12 6 1 2 3 5 4 2 1 41 2 3 2 532210p(i) 654321i 766320D(i) 0 2 4 3 3 6 6 Example 7

13 0D(i) ∞∞∞∞∞ 0 0 0 0 0 654321i Example – Topological 61 2 3 5 4 3 4 5 2 6 3 8 6 3 6 7 4

14 Example – Dijkstra’s 0D(i) ∞∞∞∞∞ 0 0 0 0 0 654321i 61 2 35 4 3 4 5 2 6 4 8 1 3 6 7 4

15 s 3 6 5 5 3 4 2 4 3 4 3 2 3 2 1 5 4 6 7 8 9 10 12 11 13 t 6 1 6 6 6 7 4 5 1 1 1 1 1 1 2 2 2 3 3 5 3 0 6 1 5 Example

16 Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated ‘length’ c ij = 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) + c ij for all (i,j) in E(i) and D(s) = 0 Label Correcting 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

17 Queue Stack Dequeue (Pape) List Structures enter leave enter leave enter re-enter leave

18 6 1 2 3 5 4 2 -2 3 31 3 2 3 4 ∞∞ ∞ ∞∞ 0D(i) 0 2 0 0 0 0 0 654321i =List 0D(i) 0 0 0 =List ∞∞∞3 {1} {2, 3} queue

19 6 1 2 3 5 4 2 -2 3 31 3 2 3 4 ∞∞ ∞ ∞∞ 0D(i) 0 2 0 0 0 0 0 654321i =List D(i) 0 =List ∞ ∞∞3 {1} {2, 3} dequeue

20 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 |c ij |, 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. 61 2 3 5 4 2 -2 -3 31 3 2 3 4 Negative cycle detection

21 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) = c ij, pred(i, j) = i for each (i, j) in E; set D(i, i) = c ii, pred(i, i) = i for each i; for k = 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 All Pairs Shortest Path

22 1 2 3 5 4 1 1 4 3 1 2 Example 013∞∞ 201∞∞ ∞∞0∞2 ∞4∞0∞ ∞∞∞10 111-- 222-- --3-3 -4-4- ---55 2 k = 0 k = 1 D =pred= 01 201∞∞ ∞0 40 ∞0 11 222-- -3 44 -5 k = 2 D =pred=

23 Example 01254 20143 97032 64507 75610 11253 22253 24353 24243 24255 k = 5 D =pred= 1 2 3 5 4 1 1 3 1 2 2 What is shortest path from 1 to 4? What is shortest path from 5 to 3? 4


Download ppt "Suppose G = (V, E) is a directed network. Each edge (i,j) in E has an associated ‘length’ c ij (cost, time, distance, …). Determine a path of shortest."

Similar presentations


Ads by Google