length of a path the weight (length) of a path is the sum of the weights of its edges. adcbe path p:...
TRANSCRIPT
Length of a Path
The weight (length) of a path is the sum of the weights of its edges.
a dcb ePath p:7 2 1 5
Edge weights: w(a, b) = 7, w(b, c) = 2, w(c, d) = 1, w(d, e) = 5
Path weight w(p) = 7+2+1+5 = 15
A shortest path from vertex u to vertex v has the minimum weight among all paths connecting u to v.
Single-Source Shortest Paths
path s s-a s-a-b s-a-b-c s-a-d s-a-b-e s-a-b-e-fweight 0 3 4 6 6 6 9
s a b c d e f
Problem Given a graph G = (V, E), where every edge e has a weight w(e), and a source vertex s in V, find the
shortest path from s to every other vertex.
a
s
b e
f
d
c
3
3
51
2
2
2
4
1
6
35
source
Applications
• Transportation (shortest route from Ames to Phoenix?) • Network routing (how to direct packets to a destination across a
network?)
• Telecommunications• Speech interpretation (best interpretation of a spoken
sentence)
• Robot path planning• Medical imaging• Building block for network algorithms• ...
Shortest-Paths Tree
a
s
b e
f
d
c
3
3
12
23
The unique simple path from s to any vertex v in the tree is a shortest path from s to v.
The vertices in the tree are exactly those reachable from s in G.
Similar to the breadth-first tree (where all edges have weight 1).
Negative Weights
0
3
5 11
-1
-
-
-
3
5
2 3
-6
7
8
4
-4
6
-3
2
-8 3
s
a
c
e
b
d
f
gh
j
i
unreachable from snegative-weight cycle
source
The shortest path may have length either or – .
No Cycle in a Shortest Path
A shortest path cannot contain a negative-weight cycle.
A shortest path cannot contain a positive-weight cycle because otherwise the removal of such a cycle would reduce the path weight.
Any zero-weight cycle in a shortest path can be removed.
We only consider shortest path of at most |V| – 1 edges.
Optimal Substructure
Any subpath of a shortest path is the shortest of all paths betweenthe two intermediate vertices.
u
yx
v
shortest path from u to v:
shortest pathfrom x to y
Greedy algorithm or dynamic programming? (Both are used in solving different versions of the shortest path problem.)
Representing Shortest Paths
d(v) = length of the shortest path from s to v found so far (an upper bound on the weight of the eventual shortest path).
(v) = the predecessor of v in the above path (used for backtracking the shortest path from s to v in the end).
During the execution of a shortest-path algorithm, maintain two arrays:
Initialization
for each vertex v in G do d[v] [v] NILd[s] 0
Relaxation
Test if the shortest path to v found so far can be improved by going through u.
s
w
v
u (v)
s
w
v
u
(v)
d[v] > d[u] + w(u, v)
Relax(u, v) if d[v] > d[u] + w(u, v) then d[v] d[u] + w(u, v) [v] u
Shortest path algorithms differ in the number of times the edges are relaxed and the order in which theyare relaxed.
The Bellman-Ford Algorithm
Edge weights may be negative.
It detects any negative-weight cycle reachable from the source.
Each edge is relaxed |V| – 1 times.
Bellman-Ford(G, s) Initialize(G, s) for i 1 to |V| – 1 do for each edge (u, v) E do Relax(u, v) for each edge (u, v) E do if d[v] > d[u] + w(u, v) then return false // negative-weight cycle found! return true // no negative-weight cycle
Running time (VE)
An Example
0
6
82
7
-3
-47
9
5
-2
s
t
y z
x
Source: s
Order of edge relaxation: (t, x), (t, y), (t, z), (x, t), (y, x), (y, z), (z, x), (z, s), (s, t), (s, y)
Round 1
0
7
6
6
82
7
-3
-47
9
5
-2
s
t
y z
x
d[y] = 7[y] = s
d[t] = 6[t] = s
Round 2
0
27
116
6
82
7
-3
-47
9
5
-2
s
t
y z
x
Relax (t, x), (t, y), (t, z), (x, t).
d[x] = 11[x] = t
d[z] = 2[z] = t
Round 2 (cont’d)
0
27
46
6
82
7
-3
-47
9
5
-2
s
t
y z
x
Relax (y, x), (y, z), (z, x), (z, s), (z, s), (s, t), (s, y).
d[x] = 4[x] = y
Round 3
0
27
42
6
82
7
-3
-47
9
5
-2
s
t
y z
xd[t] = 2[t] = x
Round 4
0
-27
42
6
82
7
-3
-47
9
5
-2
s
t
y z
x
d[z] = -2[z] = t (unchanged)
Detecting Negative-Weight Cycle
0
52
-4-17
s
a
b
c
Order of edge relaxation: (s, a), (b, c), (b, a), (a, b), (s, c)
05
7
7
52
-4-17
s
a
b
c
03
5
6
52
-4-17
s
a
b
c
01
3
4
52
-4-17
s
a
b
c
0-1
1
2
52
-4-17
s
a
b
c
d[a] > d[b] + w(b, a): a negative cycle exists!
Correctness
Assume G has no negative-weight cycles reachable from s. After |V| – 1 rounds of relaxation, d[v] is the length of ashortest path from s to v for every vertex v.
For any vertex v, a path from s to v exists if and only if Bellman-Ford terminates with d[v] < .
If G has no negative-weight cycles reachable from s, then Bellman-Ford returns true, d[v] records the shortest path lengthfor every vertex v, and all [v] induce a shortest-paths tree.Otherwise, the algorithm returns false.