shortest path in graph
Download
Report
Transcript shortest path in graph
Chapter 6
Dynamic Programming
Slides by Kevin Wayne.
Copyright © 2005 Pearson-Addison Wesley.
All rights reserved.
1
6.8 Shortest Paths
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.
allow negative weights
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.
s
10
2
9
18
6
6
30
15
-8
5
16
44
6
-16
11
20
7
3
19
4
6
t
3
Shortest Paths: Failed Attempts
Dijkstra. Can fail if negative edge costs.
2
u
3
s
v
1
-6
t
Re-weighting. Adding a constant to every edge weight can fail.
5
2
s
6
3
5
2
0
-3
6
3
t
4
Shortest Paths: Negative Cost Cycles
Negative cost cycle.
-6
-4
7
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.
s
W
t
c(W) < 0
5
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
0
OPT(i, v)
min
OPT(i 1, v) ,
if i 0
OPT(i
1,
w)
c
min
vw otherwise
(v, w) E
Remark. By previous observation, if no negative cycles, then
OPT(n-1, v) = length of shortest v-t path.
6
Shortest Paths: Implementation
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]
}
Analysis. (mn) time, (n2) space.
n: # of nodes; m: # of edges
Finding the shortest paths. Maintain a "successor" for each table
entry.
7
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.
}
}
8
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
Distance Vector Protocol
Communication network.
Node router.
Edge direct communication link.
naturally nonnegative, but Bellman-Ford used anyway!
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.
10
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
4-11
Bellman-Ford example
How about du(z)?
Suppose we know that
dv(z) = 5, dx(z) = 3, dw(z) = 3
5
2
u
v
2
1
x
3
w
3
1
5
1
y
2
z
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
4-12
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
4-13
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
4-14
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
Each node:
wait for (change in local link,
cost of msg from neighbor)
recompute estimates
neighbors then notify their
neighbors if necessary
if DV to any dest has
changed, notify neighbors
Network Layer
4-15
Dx(y) = min{c(x,y) + Dy(y), c(x,z) + Dz(y)}
= min{2+0 , 7+1} = 2
node x table
cost to
x y z
cost to
x y z
from
from
x 0 2 7
y ∞∞ ∞
z ∞∞ ∞
node y table
cost to
x y z
Dx(z) = min{c(x,y) +
Dy(z), c(x,z) + Dz(z)}
= min{2+1 , 7+0} = 3
x 0 2 3
y 2 0 1
z 7 1 0
x ∞ ∞ ∞
y 2 0 1
z ∞∞ ∞
node z table
cost to
x y z
from
from
x
x ∞∞ ∞
y ∞∞ ∞
z 71 0
2
y
7
1
z
time
Network Layer
4-16
Dx(y) = min{c(x,y) + Dy(y), c(x,z) + Dz(y)}
= min{2+0 , 7+1} = 2
node x table
cost to
x y z
x ∞∞ ∞
y ∞∞ ∞
z 71 0
from
from
from
from
x 0 2 7
y 2 0 1
z 7 1 0
cost to
x y z
x 0 2 7
y 2 0 1
z 3 1 0
x 0 2 3
y 2 0 1
z 3 1 0
cost to
x y z
x 0 2 3
y 2 0 1
z 3 1 0
x
2
y
7
1
z
cost to
x y z
from
from
from
x ∞ ∞ ∞
y 2 0 1
z ∞∞ ∞
node z table
cost to
x y z
x 0 2 3
y 2 0 1
z 7 1 0
cost to
x y z
cost to
x y z
from
from
x 0 2 7
y ∞∞ ∞
z ∞∞ ∞
node y table
cost to
x y z
cost to
x y z
Dx(z) = min{c(x,y) +
Dy(z), c(x,z) + Dz(z)}
= min{2+1 , 7+0} = 3
x 0 2 3
y 2 0 1
z 3 1 0
time
Network Layer
4-17