Dijkstra 4 Named after its inventor Edsger Dijkstra

  • Slides: 55
Download presentation

Dijkstra算法 4 Named after its inventor Edsger Dijkstra (1930 -2002) Truly one of the

Dijkstra算法 4 Named after its inventor Edsger Dijkstra (1930 -2002) Truly one of the "founders" of computer science This is just one of his many contributions

求最短路的一个例子 2 b 7 8 a 2 1 3 2 7 c s 0

求最短路的一个例子 2 b 7 8 a 2 1 3 2 7 c s 0 4 d e 4 f 3 5 1 4 h 3 4 g 6

1, c U 1 2 b 7 8 a 8, c 2 1 s

1, c U 1 2 b 7 8 a 8, c 2 1 s 0 4 d 4, c e 3 2 4 3 5 1 4 7, cf 7 c 2, c h 3 4 g 6

1, c S 2 b 7 1 8 a c 8, c 2 3

1, c S 2 b 7 1 8 a c 8, c 2 3 2 4 d 4, c 4 4 1 7, cf 3 5 U 2 e 7 0 2, c h 3 4 g 6 4, b

S 1, c 2 b 7 8 a 8, c 2 1 s 0

S 1, c 2 b 7 8 a 8, c 2 1 s 0 4 d 4, c e 3 2 4 3 5 1 4 6, e 7, cf 7 c 2, c h 3 4 g U 3 6 4, b 3, e

S 1, c 2 b 7 8 a 8, c 2 1 s 0

S 1, c 2 b 7 8 a 8, c 2 1 s 0 4 d U 4 4, c e 3 2 4 3 5 1 4 6, ef 7 c 2, c h 3 4 g 6 9, h 3, e

S 1, c 2 b 7 8 a 8, c 6, d 2 1

S 1, c 2 b 7 8 a 8, c 6, d 2 1 s 0 4 d 4, c e 3 2 4 3 5 1 4 6, ef 7 c 2, c U 5 h 3 4 g 6 9, h 3, e

1 7 8 6 2 1 2 2 7 5 3 6 2 s

1 7 8 6 2 1 2 2 7 5 3 6 2 s 0 4 4 4 3 5 4 2 1 2 2 1 3 2 6 7 s 0 4 4 3 3 4 5 4 1 5 6 9 3 3 1 6 7 9 8 6 2 1 2 2 5 2 6 7 s 0 4 4 3 5 1 3 3 3 4 9 6

Floyd-Warshall算法 动态规划方法:Use optimal substructure of shortest paths: Any subpath of a shortest path is

Floyd-Warshall算法 动态规划方法:Use optimal substructure of shortest paths: Any subpath of a shortest path is a shortest path. 构建一个三维矩阵: � 定义dij(k):中间节点在{1, 2, 节点j的最短距离 � 最终目的是要计算dij(n) …, k}中的从节点i到

Step 1

Step 1

Step 2

Step 2

Step 3

Step 3

Step 4

Step 4

Step 5

Step 5

传输网 37 The unique node with out-degree 0 The sink 4 1 4 2

传输网 37 The unique node with out-degree 0 The sink 4 1 4 2 5 2 3 5 4 2 3 6 3 3 Capacity of edge, Ci, j The unique node with in-degree 0 The source It is assumed that all edges are in one direction.

流(Flow) 38 (Ci, j, Fi, j), For any edge, Fi, j Ci, j 4

流(Flow) 38 (Ci, j, Fi, j), For any edge, Fi, j Ci, j 4 (4, 2) 1 (5, 3) (3, 3) 5 (4, 3) (2, 0) (2, 1) 2 (3, 2) 6 3 (3, 2) Conservation of flow: Here: 3=1+0+2 Value of the flow is 5

流(Flow) 39 Let (G, k) be a transport network with source S and sink

流(Flow) 39 Let (G, k) be a transport network with source S and sink D. Assume the capacity function k is defined on the edges of G. A flow in G is a nonnegative realvalued function F defined on the edges of G such that: � [Capacity constraint] 0 F(e) k(e) for each edge e E(G) � [Conversation equation] The value of a flow F (denoted as |F|) is defined as the value of F(S, VG) (or, F(VG, D))

例 40 The actual transported amount on any road cannot exceed the edge capacity.

例 40 The actual transported amount on any road cannot exceed the edge capacity. For any vertex, the total input amount must be equal to the output total. a (20, 20) S (60, 26) (12, 4) c (20, 13) b (16, 16) f (12, 0) (10, 10) g (20, 16) (17, 0) e (7, 7) (13, 13) (18, 16) (15, 10) h (20, 0) d (30, 20) The problem: Can we send more on the network? (30, 10) D (20, 20) i Total capacity: 70 Actual receipt: 46

最大流 41 A flow F in a network (G, k) is a maximum flow

最大流 41 A flow F in a network (G, k) is a maximum flow if |F| |F ’| for every flow F ’ in (G, k) (6, 4) 2 (6, 6) (2, 2) 1 (4, 4) 3 (6, 6) 4 (4, 2) Value of flow: 8 2 (6, 6) (2, 0) 1 (4, 4) 3 4 (4, 4) Value of flow: 10 Basic Problems: (1) Largest value of flow? (2) A flow with the largest value?

例 42 (6, 4) 2 (6, 6) (2, 2) 1 (4, 4) 3 4

例 42 (6, 4) 2 (6, 6) (2, 2) 1 (4, 4) 3 4 (4, 4) Value of flow: 8 (6, 4) 1 This edge is not in N, but in N’s symmetric closure 3 3 4 (4, 4) Value of flow: 10 2 (2, 2) (6, 6) (2, 0) 1 (4, 2) 2 2 4 (4, 2)

例:容量过剩 43 : 1 2 3 4 is not a path in N, but

例:容量过剩 43 : 1 2 3 4 is not a path in N, but in G, the symmetric closure. (1, 2) is in N, this edge has an excess capacity 2 (=6 -4) (6, 4) 1 This edge is not in N, but in N’s symmetric closure (2, 3) is not in N, this edge has an excess capacity 2 2 (2, 2) 3 2 4 (4, 2)

再例 44 Excess capacity: e 4, 5 4 e 4, 1 e 1, 4

再例 44 Excess capacity: e 4, 5 4 e 4, 1 e 1, 4 1 e 4, 2 e 2, 4 5 e 4, 5 e 5, 6 e 5, 2 e 2, 5 6 e 3, 6 e 2, 1 2 Ci, j is the capacity of edge (i, j) Fi, j is the flow on edge (i, j) e 6, 5 e 2, 3 e 3, 2 ei, j=Ci, j-Fi, j ej, i=Fi, j if Fi, j>0 3 e 6, 3 edges in N edges in s(N), but not in N

45 Labeling Algorithm (Ford & Fulkson) Initialization: set all flow to 0 N 1,

45 Labeling Algorithm (Ford & Fulkson) Initialization: set all flow to 0 N 1, all nodes connected to the source by an edge with positive excess capacity Step 1: (1) Identify N 1 (2) Label nodes in N 1 as follows [4, 1] Excess capacity of (1, j) [Ej, 1] 1 Denoting node 1 4 e 1, 4= 4 e 4, 1= 0 e 1, 2= 5 e 2, 1= 0 4 2 5 2 [5, 1] 3 5 4 2 3 6 3 3

46 Labeling Algorithm (Ford & Fulkson) N 2(j), all unlabelled nodes connected to node

46 Labeling Algorithm (Ford & Fulkson) N 2(j), all unlabelled nodes connected to node j by an edge with positive excess capacity Also N 2, in this case Step 2: (1) Identify N 2(j), based on the node j, with the smallest number, in N 1 (2) Label nodes in N 2(j) as follows min{Ej, Excess capacity of (j, k)} [Ek, j] Denoting node j (3) Do as above for all j in N 1, and let e 4, 5= 3 e 5, 4= 0 [4, 1] 4 e 1, 4= 4 e 4, 1= 0 1 e 1, 2= 5 e 2, 1= 0 3 4 2 2 5 [5, 1] j 5 4 e 2, 5= 2 e 5, 2= 0 3 2 [2, 2] e 2, 3= 3 e 3, 2= 0 6 3 3 [3, 2]

47 Labeling Algorithm (Ford & Fulkson) Step 3: Continue as in step 2, forming

47 Labeling Algorithm (Ford & Fulkson) Step 3: Continue as in step 2, forming N 3, N 4, N 5, . . . , until: i. the sink has been labeled, (goto step 4) ii. the sink has not been labeled, and no other nodes can be labeled according to the rules (algorithm ends, and the total flow is the maximum flow) (note: the source is not labeled) N 3, the sink has been labeled e 4, 5= 3 e 5, 4= 0 [4, 1] 4 e 1, 4= 4 e 4, 1= 0 1 e 1, 2= 5 e 2, 1= 0 3 4 2 2 5 [5, 1] j 5 e 2, 5= 2 e 5, 2= 0 3 2 [2, 2] e 2, 3= 3 e 3, 2= 0 4 [3, 3] 6 3 3 [3, 2] e 3, 6= 3 e 6, 3= 0

48 Labeling Algorithm (Ford & Fulkson) Step 4: The label of sink is [En,

48 Labeling Algorithm (Ford & Fulkson) Step 4: The label of sink is [En, m] (here, [3, 3]), where En is the amount of extra flow that can be made to reach the sink through a path , and the path can be traced backward by node m Update ei, j, ej, i accordingly, and then return to step 1 [4, 1] [2, 2] 4 1 e 1, 2= 2 e 2, 1= 3 3 4 4 6 3 3 2 [5, 1] [3, 3] 2 2 5 5 j e 2, 3= 0 e 3, 2= 3 3 [3, 2] e 3, 6= 0 e 6, 3= 3

算法示例 49 4 e 1, 4= 4 e 4, 1= 0 e 1, 2=

算法示例 49 4 e 1, 4= 4 e 4, 1= 0 e 1, 2= 5 e 2, 1= 0 3 5 4 4 1 e 4, 5= 3 e 5, 4= 0 e 3, 6= 4 e 6, 3= 0 2 e 2, 5= 2 2 e 5, 2= 0 5 2 3 e 2, 3= 3 e 3, 2= 0 3 6 After the first cycle 3 e 3, 6= 3 e 6, 3= 0 e 1, 4= 4 e 4, 1= 0 At the beginning, setting all flow to 0 [4, 1] 4 e 1, 2= 5 e 2, 1= 0 3 5 [2, 2] 4 4 1 e 4, 5= 3 e 5, 4= 0 2 2 e 2, 5= 2 e 5, 2= 0 5 2 [5, 1] e 3, 6= 4 e 6, 3= 0 3 e 2, 3= 3 e 3, 2= 0 3 [3, 2] 3 e 3, 6= 3 e 6, 3= 0 6 [3, 3]

算法示例 50 4 e 1, 4= 4 e 4, 1= 0 5 e 3,

算法示例 50 4 e 1, 4= 4 e 4, 1= 0 5 e 3, 6= 4 e 6, 3= 0 2 e 2, 5= 2 2 e 1, 2= 2 e 2, 1= 3 3 4 4 1 e 4, 5= 3 e 5, 4= 0 e 5, 2= 0 5 2 3 e 2, 3= 0 e 3, 2= 3 3 6 3 After the second cycle e 3, 6= 0 e 6, 3= 3 [4, 1] 4 e 1, 4= 4 After the first cycle e 4, 1= 0 e 1, 2= 2 e 2, 1= 3 3 [2, 2] 5 4 4 1 e 4, 5= 3 e 5, 4= 0 2 e 2, 5= 2 2 6 e 5, 2= 0 5 2 [2, 1] e 3, 6= 4 e 6, 3= 0 3 e 2, 3= 0 e 3, 2= 3 3 3 e 3, 6= 0 e 6, 3= 3 [2, 5]

算法示例 51 4 e 1, 4= 4 e 4, 1= 0 e 1, 2=

算法示例 51 4 e 1, 4= 4 e 4, 1= 0 e 1, 2= 0 e 2, 1= 5 3 5 4 4 1 e 4, 5= 3 e 5, 4= 0 e 3, 6= 2 e 6, 3= 2 2 e 2, 5= 0 2 e 5, 2= 2 5 2 3 e 2, 3= 0 e 3, 2= 3 3 6 3 After the third cycle e 3, 6= 0 e 6, 3= 3 [4, 1] 4 e 1, 4= 4 After the second cycle e 4, 1= 0 e 1, 2= 0 e 2, 1= 5 3 [3, 4] 5 4 4 1 e 4, 5= 3 e 5, 4= 0 2 2 e 2, 5= 0 e 5, 2= 2 5 2 [2, 5] e 3, 6= 2 e 6, 3= 2 3 e 2, 3= 0 e 3, 2= 3 3 3 e 3, 6= 0 e 6, 3= 3 6 [2, 5]

算法示例 52 4 e 1, 4= 2 e 4, 1= 2 e 1, 2=

算法示例 52 4 e 1, 4= 2 e 4, 1= 2 e 1, 2= 0 e 2, 1= 5 5 3 5 e 2, 5= 2 e 5, 2= 0 4 1 e 4, 5= 1 e 5, 4= 2 4 e 3, 6= 0 e 6, 3= 4 2 e 2, 5= 0 2 e 5, 2= 2 2 3 e 2, 3= 0 e 3, 2= 3 3 After the fourth cycle 6 The sink has not been labeled, so the final result reached 3 e 3, 6= 0 e 6, 3= 3 [2, 1] After the third cycle 4 e 1, 4= 2 e 4, 1= 2 4, 2 1 e 1, 2= 0 e 2, 1= 5 2, 0 5, 5 2 [1, 5] e 4, 5= 1 e 5, 4= 2 3, 2 e 2, 5= 2 e 5, 2= 0 [1, 4] 5 4, 4 2, 2 e 2, 5= 0 e 5, 2= 2 3, 3 e 2, 3= 0 e 3, 2= 3 3 e 3, 6= 0 e 6, 3= 4 6 3, 3 e 3, 6= 0 e 6, 3= 3

流与割(Flow and Cut) 53 4 Source 1 4 2 5 Cut K 1 c(K

流与割(Flow and Cut) 53 4 Source 1 4 2 5 Cut K 1 c(K 1)=8 2 3 5 Cut: a set K of edges in a network N, having the property that every path from the source to the sink contains at least one edge from K. 4 2 3 6 3 Sink 3 Cut K 2 c(K 2)=7

Max Flow Min Cut Theorem 54 For any flow F, and any cut K,

Max Flow Min Cut Theorem 54 For any flow F, and any cut K, all parts of F must pass through the edges of K. Since c(K) is the maximum amount that can pass through the edges of K, so, value(F) c(K). If value(F)=c(K), then the flow uses the full capacity of all edges in K, F must be a flow with maximum value, and, on the other hand, K must be a cut with minimum capacity. Theorem A maximum flow F in a network has value equal to the capacity of a minimum cut of the network 4 (4, 2) 1 (3, 2) (2, 0) value(F)=7 5 (4, 4) 6 (2, 2) (5, 5) 2 (3, 3) 3 (3, 3)