Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Dynamic Programming

Similar presentations


Presentation on theme: "Chapter 6 Dynamic Programming"— Presentation transcript:

1 Chapter 6 Dynamic Programming
Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.

2 6.8 Shortest Paths

3 Shortest Paths Shortest path problem. Given a directed graph G = (V, E), with edge weights cvw, find shortest path from node s to node t. Ex. Nodes represent agents in a financial setting and cvw is cost of transaction in which we buy from agent v and sell immediately to w. allow negative weights 2 10 3 9 s 18 6 -16 6 6 30 4 19 11 5 15 -8 6 20 16 t 7 44

4 Shortest Paths: Failed Attempts
Dijkstra. Can fail if negative edge costs. Re-weighting. Adding a constant to every edge weight can fail. u 2 3 s v 1 -6 t 5 6 2 2 s t 3 3 -3

5 Shortest Paths: Negative Cost Cycles
Observation. If some path from s to t contains a negative cost cycle, there does not exist a shortest s-t path; If a graph contains no negative cycle, there exists a shortest path that is simple (i.e., does not repeat nodes), and hence has at most n-1 edges. -6 -4 7 s t W c(W) < 0

6 Shortest Paths: Dynamic Programming
Def. OPT(i, v) = length of shortest v-t path P using at most i edges. Case 1: P uses at most i-1 edges. OPT(i, v) = OPT(i-1, v) Case 2: P uses at most i edges. if (v, w) is first edge, then OPT uses (v, w), and then selects best w-t path using at most i-1 edges Remark. By previous observation, if no negative cycles, then OPT(n-1, v) = length of shortest v-t path.

7 Shortest Paths: Implementation
Analysis. (mn) time, (n2) space. Finding the shortest paths. Maintain a "successor" for each table entry. Shortest-Path(G, s, t) { Array M[0…n-1, 1…n] foreach node v  V M[0, v]   M[0, t]  0 for i = 1 to n-1 foreach edge (v, w)  E M[i, v]  min { M[i-1, v], M[i-1, w] + cvw } return M[n-1,s] } Alternative to maintaining predecessor indices: compute optimal distances, then consider only zero reduced cost edges. n: # of nodes; m: # of edges

8 Bellman-Ford: Efficient Implementation
Push-Based-Shortest-Path(G, s, t) { foreach node v  V { M[v]   successor[v]   } M[t] = 0 for i = 1 to n-1 { foreach node w  V { if (M[w] has been updated in previous iteration) { foreach node v such that (v, w)  E { if (M[v] > M[w] + cvw) { M[v]  M[w] + cvw successor[v]  w If no M[w] value changed in iteration i, stop.

9 6.9 Distance Vector Protocol
The following several pages are modified from slides provided by Textbook: Computer Networking: A Top Down Approach Featuring the Internet, J. Kurose & K. Ross, Addison Wesley, 6th ed., 2013

10 Distance Vector Protocol
Communication network. Node  router. Edge  direct communication link. Cost of edge  delay on link. Dijkstra's algorithm. Requires global information of network. Bellman-Ford. Uses only local knowledge of neighboring nodes. Synchronization. We don't expect routers to run in lockstep. The order in which each foreach loop executes in not important. Moreover, algorithm still converges even if updates are asynchronous. naturally nonnegative, but Bellman-Ford used anyway!

11 Distance Vector Algorithm (1)
Bellman-Ford Equation (dynamic programming) Define dx(y) := cost of least-cost path from x to y Then dx(y) = minv {c(x,v) + dv(y) } where min is taken over all neighbors of x dx(y) = minv {c(x,v) + dv(y) } Network Layer

12 du(z) = min { c(u,v) + dv(z), c(u,x) + dx(z), c(u,w) + dw(z) }
Bellman-Ford example How about du(z)? Suppose we know that dv(z) = 5, dx(z) = 3, dw(z) = 3 u y x w v z 2 1 3 5 B-F equation says: du(z) = min { c(u,v) + dv(z), c(u,x) + dx(z), c(u,w) + dw(z) } = min {2 + 5, 1 + 3, 5 + 3} = 4 Network Layer

13 Distance Vector Algorithm (3)
Dx(y) = estimate of least cost from x to y Distance vector: Dx = [Dx(y): y є N ] Node x knows cost to each neighbor v: c(x,v) Node x maintains Dx = [Dx(y): y є N ] Node x also maintains its neighbors’ distance vectors For each neighbor v, x maintains Dv = [Dv(y): y є N ] Network Layer

14 Distance vector algorithm (4)
Basic idea: Each node periodically sends its own distance vector estimate to neighbors When a node x receives new DV estimate from neighbor, it updates its own DV using B-F equation: Dx(y) ← minv{c(x,v) + Dv(y)} for each node y ∊ N Under minor, natural conditions, the estimate Dx(y) converge the actual least cost dx(y) Network Layer

15 Distance Vector Algorithm (5)
Iterative, asynchronous: each local iteration caused by: local link cost change DV update message from neighbor Distributed: each node notifies neighbors only when its DV changes neighbors then notify their neighbors if necessary Each node: wait for (change in local link, cost of msg from neighbor) recompute estimates if DV to any dest has changed, notify neighbors Network Layer

16 z y x Dx(z) = min{c(x,y) + Dy(z), c(x,z) + Dz(z)} = min{2+1 , 7+0} = 3
Dx(y) = min{c(x,y) + Dy(y), c(x,z) + Dz(y)} = min{2+0 , 7+1} = 2 node x table x y z x y z from cost to cost to x y z x 2 3 from y z node y table cost to x z 1 2 7 y x y z x y from z node z table cost to x y z x ∞ ∞ ∞ from y z 7 1 time Network Layer

17 z y x Dx(z) = min{c(x,y) + Dy(z), c(x,z) + Dz(z)} = min{2+1 , 7+0} = 3
Dx(y) = min{c(x,y) + Dy(y), c(x,z) + Dz(y)} = min{2+0 , 7+1} = 2 node x table x y z x y z from cost to cost to cost to x y z x y z x x from y from y z z node y table cost to cost to cost to x z 1 2 7 y x y z x y z x y z x x x y from y from from y z z z node z table cost to cost to cost to x y z x y z x y z x x x ∞ ∞ ∞ from y from y from y z z z 7 1 time Network Layer


Download ppt "Chapter 6 Dynamic Programming"

Similar presentations


Ads by Google