Download presentation
Presentation is loading. Please wait.
1
Shortest Path Problems
Bellman-Ford Algorithm for the Single-Source Shortest Path Problem with Arbitrary Arc Costs Updated 18 February 2008
2
Bellman-Ford Algorithm
begin d(i) := for each node i in N d(s) := 0 and pred(s) := 0; for k = 1 to n for each (i, j) in A do if d(j) > d(i) + cij then d(j) := d(i) + cij and pred(j) := i end;
3
Bellman-Ford Example 1 2 2 1 4 3 -2
4
dk(i) denotes d(i) after iteration k
Bellman-Ford Example 1 -1 2 k =1 1 2 d1(1) = 0 d1(2) = -1 d1(3) = 4 d1(4) = 1 -2 1 1 3 4 3 4 1 dk(i) denotes d(i) after iteration k
5
Bellman-Ford Example 1 -1 k = 2 2 1 2 d2(1)=0 d2(2)=-1 d2(3)=0 d2(4)=1
k = 2 2 1 2 d2(1)=0 d2(2)=-1 d2(3)=0 d2(4)=1 -2 1 1 3 4 3 1 4
6
Bellman-Ford Example 1 -1 k =3 2 1 2 d3(1)=0 d3(2)=-1 d3(3)=0 d3(4)=1
k =3 2 1 2 d3(1)=0 d3(2)=-1 d3(3)=0 d3(4)=1 -2 1 1 3 4 3 d3(i)= d2(i) 1
7
Shortest Path Tree d(j) d(i) + cij and d(j) = d(i) + cij
-1 2 1 2 d(j) d(i) + cij and d(j) = d(i) + cij for all predecessor arcs -2 1 1 3 4 3 1
8
Complexity of Bellman-Ford
There are n iterations Each iteration inspects each arc once Inspecting an arc is O(1) Complexity is O(mn)
9
Bellman-Ford Example 2 100 1 2 10 200 -150 -150 4 3
10
Bellman-Ford Example 2 100 100 k = 1 1 2 d1(1) = 0 d1(2) = 100
100 k = 1 1 2 d1(1) = 0 d1(2) = 100 d1(3) = d1(4) = 200 10 -150 200 -150 4 3 200
11
Bellman-Ford Example 2 -100 100 100 100 k = 2 1 2 d2(1) = 0
100 k = 2 1 2 d2(1) = 0 d2(2) = -100 d2(3) = 50 d2(4) = -90 10 -150 200 -150 4 3 100 200 -90 50 100
12
Bellman-Ford Example 2 -100 100 k = 3 1 2 d3(1) = 0 d3(2) = -100
100 k = 3 1 2 d3(1) = 0 d3(2) = -100 d3(3) = -240 d3(4) = -90 10 -150 200 -150 4 3 -90 -240 100 50
13
Bellman-Ford Example 2 -100 -390 100 k = 4 1 2 10 d4(1) = 0 -150 200
-390 100 k = 4 1 2 10 d4(1) = 0 -150 200 d4(2) = -390 d4(3) = -240 -150 4 3 d4(4) = -90 -90 -240 100 d3(2) = -100
14
Detecting Negative-Cost Cycles
If there is a negative-cost cycle in G, then dn(i) < dn-1(i) for some node i.
15
Detecting Negative-Cost Cycles
dn-1(1) dn-1(2) 1 2 dn(4) dn-1(1) + c14 dn(1) dn-1(2) + c21 dn(2) dn-1(3) + c32 4 3 dn(3) dn-1(4) + c43 dn-1(4) dn-1(3)
16
Detecting Negative-Cost Cycles
If the length of the cycle < 0 then dn(i) < dn-1(i) for at least one node i in the cycle.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.