Title: Negative-Weight edges:
1Negative-Weight edges
- Edge weight may be negative.
- negative-weight cycles the total weight in the
cycle (circuit) is negative. - If no negative-weight cycles reachable from the
source s, then for all v ?V, the shortest-path
weight remains well defined,even if it
has a negative value. - If there is a negative-weight cycle on some path
from s to v, we define - .
2a
b
-4
h
i
-1
3
2
4
3
c
d
6
8
3
-8
5
11
5
g
0
s
-3
e
f
3
j
2
7
-6
Figure1 Negative edge weights in a directed
graph.Shown within each vertex is
its shortest-path weight from source s.Because
vertices e and f form a negative-weight cycle
reachable from s,they have shortest-path weights
of - . Because vertex g is reachable from a
vertex whose shortest path is - ,it,too,has
a shortest-path weight of - .Vertices such
as h, i ,and j are not reachable from s,and so
their shortest-path weights are , even
though they lie on a negative-weight cycle.
3Relaxation
- The process of relaxing an edge (u,v) consists of
testing whether we can improve the shortest path
to v found so far by going through u and,if
so,updating dv and v. - RELAX(u,v,w)
- if dvgtduw(u,v)
- then dv ? duw(u,v) (based on
Lemma 25.3) - v ? u
4u
v
u
v
2
2
5
9
5
6
RELAX(u,v)
RELAX(u,v)
u
v
u
v
2
2
5
7
5
6
(a)
(b)
Figure2 Relaxation of an edge (u,v).The
shortest-path estimate of each vertex is shown
within the vertex. (a)Because dvgtduw(u,v)
prior to relaxation, the value of dv decreases.
(b)Here, dv duw(u,v) before the
relaxation step,so dv is unchanged by
relaxation.
5Bellman-Ford algorithm
- The Bellman-Ford algorithm solves the
single-source shortest-paths problem in the more
general case in which edge weights can be
negative. - It report FALSE if a negative-weight circuit
exists.
6Continue
- BELLMAN-FORD(G,w,s)
- INITIALIZE-SINGLE-SOURCE(G,s)
- for i ? 1 to VG -1
- do for each edge (u,v) EG
- do RELAX(u,v,w)
- for each edge (u,v)? EG
- do if dvgtduw(u,v)
- then return FALSE
- return TRUE
- Time complexity O(VE).
7(a)
85
u
v
6
8
-2
6
-3
8
0
7
z
-4
2
7
7
8
9
x
y
(b)
95
u
v
6
4
-2
6
-3
8
0
7
z
-4
2
7
7
2
9
x
y
(c)
105
u
v
2
4
-2
6
-3
8
0
7
z
-4
2
7
2
7
9
x
y
(d)
115
u
v
2
4
-2
6
-3
8
0
7
z
-4
2
7
7
-2
9
x
y
(e)
12Theorem (hard) after the i-th iteration, the
cost of a shortest path from s to any node v
containing of at most i edges is obtained.
- Proof We prove it by induction on i.
- The theorem is true for i1.
- The shortest path from s to v containing at most
one edge is the the edge (s, v) (if exists). - Assume that the theorem is true for ik. Then we
are going to show that the theorem is true for
ik1. - Let P s-gt v1-gt v2 -gtvm-gt v be the shortest path
from s to v containing at most k1 edges. Then
P1 s-gt v1-gt v2 -gtvm must be the shortest path
from s to vm containing at most k edges. By
assumption, P1 has been obtained after (k)-th
iteration. In the (k1)-th iteration, we tried
to RELAX(vm, v, w) on node vm. Thus, path P is
obtained after the (k1)-th iteration.
13Corollary If negative-weight circuit exists in
the given graph, in the n-th iteration, the cost
of a shortest path from s to some node v will be
further reduced.
- Demonstrated by the following example.
145
1
6
-2
8
7
7
9
2
2
5
-8
An example with negative-weight cycle
15(No Transcript)
16(No Transcript)
17(No Transcript)
18(No Transcript)
195
6
11
1
6
-2
0
8
12
7
7
9
2
6
15
2
5
-8
8
1
20(No Transcript)
215
6
11
1
6
-2
0
8
12
7
7
9
2
5
15
2
5
-8
8
0
x
22All-Pairs Shortest Paths
- Problem definition
- Given a weighted,directed graph G(V,E), for
every pair of vertices u, v ? V, find a shortest
(least weight) path from u to v, where the weight
of a path is the sum of the weights of its
constituent edges.
23Solve the problem by Single-Source shortest paths
algorithm
- If all edge weights are nonnegative, we can use
Dijkstras algorithm. - If negative-weight edges are allowed, then we use
Bellman-Ford algorithm instead. - Can you analysis the complexity of the two
solutions ? Is it possible for us to find a more
efficient algorithm?
24Preliminary knowledge
- We use an adjacency-matrix representation of
graphs - wij
if ij - the weight of directed edge (i,j) if
i j and (i,j) E -
if i j and (i,j) E - The output of the algorithm presented in this
chapter is an n?n matrix D(dij),where entry dij
contains the weight of a shortest path from
vertex i to vertex j. That is , if we let denotes
the shortest-path weight from vertex i to vertex
j, then dij at termination.
25The structure of a shortest path
- Consider a shortest path p from vertex i to
vertex j, and suppose that p contains at most m
edges.Assuming that there are no negative-weight
cycles,m is at most V-1. If ij,then p has
weight 0 and no edges.If vertices i and j are
distinct, then we decompose path p into - i k j,
- where path p now contains at most m-1 edges.
- Moreover, p is a shortest path from i to k.
Thus, we have -
wkj.
26A recursive solution to the all-pairs
shortest-paths problem
- dij(m) ---minimum weight of any path from vertex
i to vertex j that contains at most m edges. - When m0, dij(m) 0 ij and otherwise ? .
- For mgt1, we compute dij(m) as follows
- dij(m)min ( dij(m-1), dik(m-1)wkj)
- dik(m-1)wkj.
- Note that dij(m-1) dik(m-1)wkj for kj.
27Continue
- If the graph contains no negative-weight
cycles,then all shortest paths are simple and
thus contain at most n-1 edges.A path from vertex
i to vertex j with more than n-1 edges cannot be
shorter than a shortest path from i to j.The
actual shortest-path weights are therefore given
by - dij(n-1)dij(n)dij(n1)...
28Computing the shortest-path weights bottom up
- We compute the shortest-path weights by extending
shortest paths edge by edge.Letting AB denote
the matrix product returned by
EXTEND-SHORTEST-PATH(A,B) .We compute the
sequence of n-1 matrices - D(1)D(0)WW,
- D(2)D(1)WW2,
-
- D(n-1)D(n-2)WWn-1.
- As we argued above, the matrix D(n-1)Wn-1
contains the shortest-path weights.So we get the
algorithm.
29Continue
- SLOW-ALL-PAIRS-SHORTEST-PATH(W)
- n rowsW
- D(1) W
- for m 2 to n-1
- do D(m) EXTEND-SHORTEST-PATHS(D
(m-1),W) - return D(n-1)
- Time O(n?ESP), where ESP is the time for
EXTEND-SHORTEST-PATHS(), which is O(n3). Thus,
the total time is O(n4).
30Continue
- EXTEND-SHORTEST-PATH(D,W)
- n rowsD
- for i 1 to n
- do for j 1 to n
- for k 1 to n
- do dij
min(dij,dikwkj) - return D
- Time O(n3).
31Example
2
4
3
1
3
8
1
-5
-4
2
7
5
4
6
32(No Transcript)
33Improving the running time
- Our goal,however, is not to compute all the D(m)
matrices we are interested in matrix
D(n-1).Recall that in the absence of
negative-weight cycles, D(m)D(n-1),for all
integers mgtn-1.So, we can compute D(n-1) with
only matrix products by
computing the sequence - D(1)W,
- D(2)W2WW
- D(4) W2 W2
-
34Continue
- FASTER-ALL-PAIRS-SHORTEST-PATHS(W)
- n rowsW
- D(1) W
- while n-1gtm
- do D(2m) EXTEND-SHORTEST-PATHS(D(m
),D(m)) - m 2m
- return D(m)
- Time complexity O(n3 log n).
35The End
36Theorem (hard) after the i-th iteration, the
cost obtained is at most the cost of a shortest
path from s to any node v containing at most i
edges. (New slide 12 of
lecture 5)
- Proof We prove it by induction on i.
- The theorem is true for i1.
- The shortest path from s to v containing at most
one edge is the the edge (s, v) (if exists). - Assume that the theorem is true for ik. Then we
are going to show that the theorem is true for
ik1. - Let P s-gt v1-gt v2 -gtvm-gt v be the shortest path
from s to v containing at most k1 edges. Then
P1 s-gt v1-gt v2 -gtvm must be the shortest path
from s to vm containing at most k edges. By
assumption, P1 has been obtained after (k)-th
iteration. In the (k1)-th iteration, we tried
to RELAX(vm, v, w) on node vm. Thus, path P is
obtained after the (k1)-th iteration.