Download presentation
Presentation is loading. Please wait.
Published byAlfred Payne Modified over 9 years ago
1
1 Network Optimization Chapter 3 Shortest Path Problems
2
2 In a weighted digraph, a path of minimum weight from vertex v to vertex w is called a shortest path (SP) from v to w, and its length is called the shortest distance (SD) from v to w. For undirected graph, we can define SP and SD between two vertices. The shortest path problem can be treated as a transshipment problem. 3.1 Shortest paths from a single source
3
3 (a) If we want to find SP and SD from v to w , then: let v be the only source with a supply of 1 unit; let w be the only sink with a demand of 1 unit; let other vertices be intermediate vertices; let the cost of sending one unit of the commodity from i to j be the weight of the arc (i, j); we now use the network simplex method to solve this transshipment problem. A 0-1 solution x* will be obtained, and the arcs (i, j) with =1 form a shortest path from v to w.
4
4 3.1 Shortest paths from a single source (b) if we want to find shortest paths from a given vertex v to each of the other n-1 vertices in the digraph, then: let v be the only source with a supply of n-1 units; let every other vertex be a sink with a demand of 1 unit; let the cost of sending one unit of commodity from i to j be the weight of the arc (i, j); then the shortest path problem is transformed to a transshipment problem, and hence can be solved by the network simplex method.
5
5 3.1 Shortest paths from a single source We study other two algorithms: Dijkstra ’ s algorithm to find a SP and the SD from a specified vertex to every other vertex; Floyd and Warshall algorithm for all-pairs shortest path problem.
6
6 Main idea about the Dijkstra ’ s method Suppose the 5 nearest vertices to v1 are v1,v3,v5,v7 and v9. Then finding the sixth nearest vertex is easy. Assume the sixth nearest vertex is v6 and the shortest path is (v1, … v?, v6). Then v? must be one the 5 nearest vertices. Can you see why?
7
7 Another important idea!! Suppose 1356 is the SP from 1 to 6. Then, for sure, 135 is the SP from 1 to 5. Can you see why? As a result, to save the SP from 1 to 6, I just need to write down 5 and the SP from 1 to 5.
8
8 3.1 Shortest paths from a single source Dijkstra ’ s algorithm Let the network G = (V, E), V = {1, 2, …, n}, and the weight of the arc (i, j) be a(i, j). If there is no arc from i to j (i j), then a(i, j) is taken as a large positive number M. We want to find the SD and SP from vertex 1 to all other vertices.
9
9 3.1 Shortest paths from a single source In the Dijkstra ’ s algorithm, each vertex i is assigned a label which is either permanent, or tentative. The permanent label L(i) of i is the SD from 1 to i; The tentative label L ’ (i) of i is an upper bound for the SD from 1 to i. At each stage of the procedure, V is partitioned to two sets: P and T, where P is the set of vertices with permanent labels, and T = V \ P is the set of vertices with tentative labels.
10
10 3.1 Shortest paths from a single source We also need to use an index V(i) to record the vertex immediately before i. This index may be updated after each iteration, and when we complete computation, it shows the vertex immediately before vertex i in the shortest path from vertex 1 to i. Dijkstra ’ s algorithm Step 0 (initial step) Set L(1) = 0. Set L ’ (j) = a(1, j) and V(j)=1 for j = 2, 3, …, n. Set P = {1}, T = {2, 3, …, n}.
11
11 3.1 Shortest paths from a single source Step 1 (Designation of a permanent label) Find k T such that Declare vertex k to be permanently labeled: Set T = T - k, P = P + k, L(k) = L ’ (k). If T = Ø (i.e. P = V), stop; the computation is completed.
12
12 3.1 Shortest paths from a single source Step 2(Revision of tentative labels) Set L ’ (j) = min {L ’ (j), L(k) + a(k, j)} for all j T, and if now L ’ (j) = L(k) + a(k, j), then update V(j) as V(j)=k. Go to step 1.
13
13 Informal steps L ’ (1)=0, V ’ (1)=1, P = {1}, T = {2, … } For all v in T, L(v)=M, V(v)=1, For any v is still in T and adjacent to v*, Update L ’ (v), V ’ (v) Find v*, s.t. L ’ (v*) L(v), v in T. L(v*) = L ’ (v*) and V(v*) = V ’ (v*) Move v* from T to P.
14
14 3.1 Shortest paths from a single source In each step 1 → step 2 → step 1 iteration, a vertex is moved from T to P. So, we need to have n-1 iterations to complete computation, and for all j=2,3, … n, the indexes V(2), …, V(n) gives us n-1 arcs which together with all n vertices form a subgraph H of G(V, E).
15
15 3.1 Shortest paths from a single source If there exists a path from vertex 1 to any other vertex, then H must be connected. H is acyclic because if V(k)=i, arc (i, k) is in the shortest path from 1 to k, and i must enter P earlier than k does. So, H is a spanning tree rooted at vertex 1. And H is a shortest distance tree which includes the shortest paths from vertex 1 to other vertices.
16
16 3.1 Shortest paths from a single source Example 3.1 Obtain the SD from 1 to the remaining vertices in the directed network shown below, using Dijkstra ’ s algorithm.
17
17 3.1 Shortest paths from a single source Iteration 1 Step 1. P = {1} L(1)=0 T = {2, 3, 4, 5, 6, 7} L’(2) = 4, L’(3) = 6, L’(4) = 8, L’(5) = L’(6) = L’(7) = M. V(2)=V(3)=V(4)=V(5)=V(6)= V(7)=1. Vertex 2 is assigned a permanent label.
18
18 3.1 Shortest paths from a single source Step 2. P = {1, 2} L(2) = 4 Record arc (1,2) T = {3, 4, 5, 6, 7} L’(3) = min {6, L(2) + a(2, 3)}=5 L’(4) = min {8, L(2) + a(2, 4)}=8 L’(5) = min {M, L(2) + a(2, 5)}=11 L’(6) = min {M, L(2) + a(2, 6)}=M L’(7) = min {M, L(2) + a(2, 7)}=M V(3)=2, V(5)=2.
19
19 3.1 Shortest paths from a single source Iteration 2 Step 1. P={1,2} L(1) = 0 L(2) = 4 T = {3, 4, 5, 6, 7} Min{L’(i) | i in T}= L’(3) Vertex 3 is assigned a permanent label.
20
20 3.1 Shortest paths from a single source Step 2 P = {1, 2, 3} L(3) = 5 T = {4, 5, 6, 7} L’(4) = min {8, L(3) + a(3, 4)}=7 L’(5) = min {11, L(3) + a(3, 5)}=10 L’(6) = min {M, L(3) + a(3, 6)}=9 L’(7) = min {M, L(3) + a(3, 7)}=M V(4)=3, V(5)=3, V(6)=3.
21
21 3.1 Shortest paths from a single source Iteration 3 Step 1. P = {1, 2, 3} L(1) = 0 L(2) = 4 L(3) = 5 T = {4, 5, 6, 7} Min {L’(i) | i in T} = L’(4) Vertex 4 is assigned a permanent label.
22
22 3.1 Shortest paths from a single source Step 2 P = {1, 2, 3, 4} L(4) = 7 T = {5, 6, 7} L’(5) = min {10, L(4) + a(4, 5)}=10 L’(6) = min {9, L(4) + a(4, 6)}=9 L’(7) = min {M, L(4) + a(4, 7)}=M
23
23 3.1 Shortest paths from a single source Iteration 4 Step 1 P = {1, 2, 3, 4} L(1) = 0, L(2) = 4, L(3) = 5, L(4) = 7 T = {5, 6, 7} Min {L’(i) | i in T} =L’(6) Vertex 6 is assigned a permanent label.
24
24 3.1 Shortest paths from a single source Step 2. P = {1, 2, 3, 4, 6} L(6) = 9 T = {5, 7} L’(5) = min {10, L(6) + a(6, 5)}=10 L’(7) = min {M, L(6) + a(6, 7)}=17 V(7)=6.
25
25 3.1 Shortest paths from a single source Iteration 5 Step 1 P = {1, 2, 3, 4, 6} L(1) = 0, L(2) = 4, L(3) = 5, L(4) = 7, L(6) = 9 T = {5,7} Min {L’(i) | i in T} = L’(5) Vertex 5 is assigned a permanent label.
26
26 3.1 Shortest paths from a single source Step 2 P = {1, 2, 3, 4, 6, 5} L(5) = 10 T = {7} L ’ (7) = min {17, L(5) + a(5, 7)}=16 V(7)=5
27
27 3.1 Shortest paths from a single source Iteration 6 Step 1. P = {1, 2, 3, 4, 6, 5} L(1) = 0, L(2) = 4, L(3) = 5, L(4) = 7, L(6) = 9, L(5) = 10. T = {7} L ’ (7) = 16 Vertex 7 gets a permanent label.
28
28 3.1 Shortest paths from a single source Step 2. P = {1, 2, 3, 4, 6, 5, 7} L(7) = 16 T is empty
29
29 3.1 Shortest paths from a single source Thus L(1) = 0, L(2) = 4, L(3) = 5, L(4) = 7, L(6) = 9, L(5) = 10 and L(7) = 16, giving the SD from 1 to each vertex. The indexes V(2)=1, V(3)=2, V(4)=3, V(5)=3, V(6)=3, V(7)=5 show that arcs (1, 2), (2, 3), (3, 4), (3, 5), (3, 6) and (5, 7) constitute a shortest distance tree in the given network as shown below, giving the SP from vertex 1 to every other vertex.
30
30 3.1 Shortest paths from a single source The shortest distance tree: Question: is this a minimum weight spanning tree? No, in MST, (6,5) replaces (3,6)
31
31 You answer should consist Sequence of arcs. (according to the order the arcs are moved to P) Tree (Draw it!!). Total weight.
32
32 3.1 Shortest paths from a single source Theorem 3.1 Dijkstra ’ s algorithm finds the SD from vertex 1 to every other vertex i (i = 2, …, n). Proof.We prove the theorem by induction on the cardinality of P. We will show that for each P generated in the algorithm, (1) for every i P, L(i) is the SD from 1 to i. (2) for every j T, L ’ (j) is the length of an SP from 1 to j under the restriction that every intermediate vertex is in P.
33
33 3.1 Shortest paths from a single source First, when |P| = 1, i.e. P = {1}, T = {2, 3, …, n}, the two conclusions hold obviously. We now show that if conclusions (i) and (ii) are true when |P| = k-1, then they also hold if |P| = k. shore
34
34 3.1 Shortest paths from a single source Without loss of generality, assume P = {1, 2, …, k-1}, T = {k, …, n}. By the assumption, (i) for i P, L(i) is the SD from 1 to i; (ii) for j T, L ’ (j) is the SD from 1 to j under the restriction that every intermediate vertex is in P.
35
35 3.1 Shortest paths from a single source Also assume that in the current iteration, vertex k moves to P, i.e. So, L(k) = L ’ (k), and ( △ )
36
36 3.1 Shortest paths from a single source We need to show that (i) L(k) is the SD from 1 to k. If it is not true, let d be the SD from 1 to k. So, d < L(k) = L ’ (k). As L ’ (k) is the SD from 1 to k provided that every intermediate vertex of the SP is in P, it means that along any SP from 1 to k, there must be at least one vertex in T. Let v be the first vertex in T along the SP from 1 to k.
37
37 3.1 Shortest paths from a single source Let the SD from 1 to v be d ’. Then d ’ = L ’ (v) and d ’ d < L ’ (k). So, L ’ (v) < L ’ (k), which contradicts (*). Therefore, L(k) must be the SD from 1 to k.
38
38 3.1 Shortest paths from a single source (ii) We need to show that for each j = {k+1, …, n}, ’ (j) is the SD from 1 to j under the restriction that all intermediate vertices are in. Let be the SD from 1 to j when all intermediate vertices are in. The corresponding SP may have two possibilities:
39
39 3.1 Shortest paths from a single source (a) The SP does not go through vertex k. In this case, is the SD from 1 to j under the restriction that every intermediate vertex is in P = {1, …, k-1}. So, = L ’ (j).
40
40 (b) The SP includes vertex k. In this case, k must be the last vertex before j in the SP. If not, the SP arrives at k, then reaches a q P, and at last comes to j. It means the shortest path from 1 to q must pass through k, which is impossible because q enters P before k does, i.e. the SP from 1 to q need not to pass k. Now since k is the last vertex in the SP before reaching j, the SD from 1 to j must be the SD from 1 to k plus a(k, j): L ’ (k) + a(k, j). 3.1 Shortest paths from a single source
41
41 3.1 Shortest paths from a single source Combining the two cases (a) and (b), = min {L ’ (j), L ’ (k) +a(k, j)}. So, by the formula ( △ ), ’ (j) =, i.e. ’ (j) is the SD from 1 to j under the restriction that every intermediate vertex of SP is in. So, the proof is completed. (△) (△)
42
42 3.2 All shortest path algorithm Let G = (V, A) be a directed network. V = {1, 2, …, n}. Let a uv be the weight of the arc (u, v). (in the method we allow some negative a uv ) We want to calculate the SD from every vertex u to every other vertex v. We will use The Floyd - Warshall Method
43
43 The main idea used Suppose v5 is the second vertex in the shortest path from v1 to v9. That is, the shortest path from v1 to v9 is (v1, v5, ???, v9). Besides, suppose (v5, v6, v7, v8, v9) is the shortest path from v5 to v9. Then, we can assure the shortest path from v1 to v9 is (v1, v5, v6, v7, v8, v9).
44
44 3.2 All shortest path algorithm Let A = [a uv ] be the n n weight matrix; P = [P uv ] be the n n matrix with P uv = v, i.e. The F&W method is an iterative method which needs to have n iterations. In the j-th iteration, we will have two n n matrices A(j) = [a uv (j)] and P(j)=[P uv (j)] (j = 1, …, n). A(0)=A and P(0)=P.
45
45 3.2 All shortest path algorithm In A(j), a uv (j) shall be the SD from u to v with intermediate vertices in the vertex set {1, 2, …, j}, and the corresponding element p uv (j) in P(j) gives the vertex immediately after u in the path to attain the above SD a uv (j).
46
46 3.2 All shortest path algorithm The algorithm is as follows. Step 0Let j = 1, A(0) = A and P(0) = P. Step 1for all (u,v), If a uv (j-1) < a uj (j-1) + a jv (j-1), then a uv (j) = a uv (j-1) and p uv (j) = p uv (j-1), otherwise a uv (j) = a uj (j-1) + a jv (j-1) and p uv (j) = p uj (j-1).
47
47 3.2 All shortest path algorithm Step 2 If in matrix A(j), one diagonal element is negative, stop, the problem has a negative cycle, and some SD from a vertex to another vertex are unbounded. If j = n, go to step 3; otherwise let j ← j+1 and return to Step 1.
48
48 3.2 All shortest path algorithm Step 3Each element a uv (n) of the matrix A(n) gives the SD from vertex u to vertex v. To find the SP from u to v, if p uv (n) = j 1, then the first arc of the SP is (u, j 1 ). If j 1 v, we continue by checking p j 1 v (n). If p j 1 v (n)=j 2, then the second arc is (j 1, j 2 ). Repeat the procedure until we reach vertex v. Then the SP is u → j 1 → j 2 → …. → v.
49
49 3.2 All shortest path algorithm We use an example to explain the algorithm. Example 3.2 Obtain the SD matrix and the SP matrix in the directed network as shown below.
50
50 3.2 All shortest path algorithm We begin with the following matrices:
51
51 3.2 All shortest path algorithm Iteration 1, based on vertex 1 (j = 1) at the end of the first iteration we have the following matrices:
52
52 3.2 All shortest path algorithm Matrix A(1) is obtained from A(0) by the following “ triangle ” operation: Draw two lines on row k and column k respectively (here k=1, and when we calculate A(2), k=2, …… ). In A(0), for each element which is neither in row k, nor in column k, find its two projections on the two lines. For example for element, its two projections are and respectively. Compare the sum of the two values at the projections with the value at the element. (e.g., compare with ). Then we use the rule in the F-W method to obtain A(1) and P(1).
53
53 3.2 All shortest path algorithm Iteration 2, based on vertex 2 (j=2) It begins with A(1) and P(1). The triangle operations are carried out as in the previous operation (but now k=2) – at this stage the a uv (2) = min{a uv (1), a u2 (1) + a 2v (1)}. At the end of this iteration we have the following matrices:
54
54 3.2 All shortest path algorithm Iteration 2, based on vertex 2 (j=2)
55
55 3.2 All shortest path algorithm Iteration 3, based at vertex 3 (j=3) It begins with A(2) and P(2). Applying the triangle operation (let k=3), we obtain the following matrices:
56
56 3.2 All shortest path algorithm Iteration 4, based on vertex 4 (j=4) It begins with A(3) and P(3). Applying the triangle operation (let k=4), we get the following matrices:
57
57 3.2 All shortest path algorithm At this stage we have the SD from every vertex to every other vertex in the network, which can be obtained readily from A(4). The various shortest paths are obtained as follows: From 1 to 2: p 12 (4)=2. So the SP is 1 → 2, i.e., the arc (1, 2). From 1 to 3: p 13 (4) = 3. So the SP from 1 to 3 is the arc (1, 3).
58
58 3.2 All shortest path algorithm From 1 to 4: p 14 (4) = 3. So take the arc (1, 3) and join it to the SP from 3 to 4. We have p 34 (4) = 4. So the SP from 3 to 4 is the arc (3, 4). Thus the SP is 1 → 3 → 4. From 2 to 1: the SP is the arc (2, 1). From 2 to 3: the SP is the arc (2, 3). From 2 to 4: the SP is 2 → 3 → 4. From 3 to 1: the SP is 3 → 4 → 2 → 1. From 3 to 2: the SP is 3 → 4 → 2. From 3 to 4: the SP is the arc (3, 4). From 4 to 1: the SP is 4 → 2 → 1. From 4 to 2: the SP is the arc (4, 2). From 4 to 3: the SP is 4 → 2 → 3.
59
59 3.2 All shortest path algorithm Locating negative cycles Consider a network for which the weight matrix is as follows:
60
60 3.2 All shortest path algorithm At the end of the second iteration we have the following matrices:
61
61 3.2 All shortest path algorithm In A(2), the diagonal element a 44 (2)= -1 <0, indicating the presence of a negative cycle in the network. The negative cycle can be located by using P(2) matrix, which is 4 → 2 → 4. So, the F&W algorithm will stop calculation after obtaining A(2) and P(2).
62
62 3.2 All shortest path algorithm Theorem 3.2 If the network contains no negative cycle, then in using the Floyd-Warshall algorithm, a uv (n), the (u, v)th element in A(n), is equal to the SD from u to v. Proof Suppose the algorithm works until obtaining A(n) and P(n), and there is no negative cycle.
63
63 3.2 All shortest path algorithm We use the induction method to show that for j =1, …, n, a uv (j) is the SD from u to v with each intermediate vertex w j. For j=1, as a uv (1)=, obviously a uv (1) is the SD from u to v if only vertex 1 can be a possible intermediate vertex.
64
64 3.2 All shortest path algorithm Suppose a uv (j-1) is the SD from u to v with each intermediate vertex w j-1. Let Q uv (j) be the SP from u to v with every intermediate vertex w j. It has two possibilities: (1) Q uv (j) does not take j as an intermediate vertex. Then Q uv (j) = Q uv (j-1) and hence a uv (j) = a uv (j-1).(a)
65
65 3.2 All shortest path algorithm (2) Q uv (j) has vertex j as an intermediate vertex. Then the part of Q uv (j) from u to j must be the SP from u to j with every intermediate vertex w j-1, i.e., it is Q uj (j-1), and the part of Q uv (j) from j to v must be Q jv (j-1). So, a uv (j) = a uj (j-1) + a jv (j-1).(b)
66
66 3.2 All shortest path algorithm Combining (a) and (b), we know that the SD from u to v with each intermediate vertex w j is a uv (j) = min{a uv (j-1), a uj (j-1) + a jv (j-1)}, which is just the formula in step 1. Therefore we proved that the conclusion is true for j, and the induction proof is completed.
67
67 3.2 All shortest path algorithm An exercise for F-W method. For the network shown on the next page, when we use F-W method to obtain all pairs of shortest distances, the final matrices A(6) and P(6) are:
68
68 3.2 All shortest path algorithm Graph of the exercise
69
69 3.2 All shortest path algorithm a. Using matrix P(6), show the shortest paths from vertex 3 to all other vertices and the shortest paths from vertex 4 to all other vertices. b. Using matrix A(6) to verify your answer for question a.
70
70 3.3 Medians and centers Location Problem In a network, we need to find a vertex such that the distances to other vertices meet some optimality criterion. Suppose G=(V,E), V = {1, 2, … n}. The following two criteria (Minsum Problems & Minmax Problems) are often used.
71
71 3.3 Medians and centers 1. Minsum Problems Find a vertex j such that the sum of the distances from j to all other vertices is as small as possible. Let d(j, k) be the shortest distance from j to k ( d( j, j )=0 ), then the Minsum problem is
72
72 3.3 Medians and centers This problem is also called l 1 norm location problem as for the non- negative vector d j = (d (j,1), d (j,2), …, d (j,n)), the l 1 norm ||d j || 1 =, (if all d(j,k) are non-negative) and the location problem is to find
73
73 3.3 Medians and centers Once we obtain the n n shortest distance matrix D = [d(i, k)] (for example by the F-W method), it is easy to solve the minsum location problem: let then the best location is the vertex j such that. The vertex j is also called a median (vertex).
74
74 3.3 Medians and centers For example: S 1 =21 S 2 =19 S 3 =15 S 4 =15 S 5 =11 S 6 =17 median vertex
75
75 3.3 Medians and centers 2. Minmax problems Find a vertex j such that the distance from j to the farthest vertex is minimized, i.e. to solve the problem: The vertex j is also called a center (vertex).
76
76 3.3 Medians and centers This is also called the norm location problem, because = Max (d (j, 1), d (j, 2), …, d (j, n)) (if all d(j,k) are non-negative) and we want to find
77
77 3.3 Medians and centers The Minmax location problem can also be solved easily once we obtain the all pair SD matrix D=[d(i, k)]. For each row i, let Then the best location for the minmax problem is vertex j such that
78
78 3.3 Medians and centers For example: e 1 =6 e 2 =6 e 3 =4 e 4 =5 e 5 =4 e 6 =5 center vertex center vertex
79
79 3.3 Medians and centers It depends on different purposes to choose Minsum or Minmax criterion. For example, a delivery company which transports goods to all vertices may use the Minsum criterion. A fire station may use the Minmax criterion to decide its place.
80
80 Use NETSOLVE to calculate shortest paths Specify directed or undirected graph by typing D or U to answer the question by NETSOLVE, depending on the particular problem. No node data required. Need to enter edge (arc) data. Enter the two node names and the weight of the edge (arc).
81
81 Use NETSOLVE to calculate shortest paths NETSOLVE can calculate 4 types of shortest paths. Type 1: from a given source vertex (say 2) to a given destination vertex (say 5). Command: SPATH 2 5 Type 2: from a given source vertex (say 1) to all other vertices. Command: SPATH 1 *
82
82 Use NETSOLVE to calculate shortest paths Type 3: from all vertices to a given destination vertex (say 3). Command: SPATH * 3 Type 2: from any vertex to all other vertices. Command: SPATH * *
83
83 Use NETSOLVE to calculate shortest paths Example. For the network shown below, calculate all pairs of shortest paths
84
84 Use NETSOLVE to calculate shortest paths Need to enter edge data: 1 2 2 4 5 3 | Then type: 1 4 2 5 4 1 | SPATH * * 1 6 6 5 6 1 | You may see the 2 1 4 6 3 3 | result of SD & SP 2 3 4 6 5 2 | from every vertex 3 2 3 | i to any other 3 6 1 | vertex j 4 2 1 |
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.