1
Alan G. Labouseur, Ph.D. [email protected]
Bellman-Ford SSSP
2
Bellman-Ford SSSP
TheBellman-FordSingle-SourceShortestPath
Algorithm
CLRSchapter24.1
GraphGaweighted,directedgraphwithnegativeedgeweights
TheBellman-FordSingle-SourceShortestPathAlgorithm
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
GraphGaweighted,directedgraphwithnegativeedgeweights
TheBellman-FordSingle-SourceShortestPathAlgorithm
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
GraphGaweighted,directedgraphwithnegativeedgeweights
TheBellman-FordSingle-SourceShortestPathAlgorithm
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
GraphGaweighted,directedgraphwithnegativeedgeweights
TheBellman-FordSingle-SourceShortestPathAlgorithm
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
GraphGaweighted,directedgraphwithnegativeedgeweights
TheBellman-FordSingle-SourceShortestPathAlgorithm
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
GraphGaweighted,directedgraphwithnegativeedgeweights
TheBellman-FordSingle-SourceShortestPathAlgorithm
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
GraphGaweighted,directedgraphwithnegativeedgeweights
TheBellman-FordSingle-SourceShortestPathAlgorithm
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
GraphGaweighted,directedgraphwithnegativeedgeweights
TheBellman-FordSingle-SourceShortestPathAlgorithm
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
GraphGaweighted,directedgraphwithnegativeedgeweights
TheBellman-FordSingle-SourceShortestPathAlgorithm
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
GraphGaweighted,directedgraphwithnegativeedgeweights
TheBellman-FordSingle-SourceShortestPathAlgorithm
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
GraphGaweighted,directedgraphwithnegativeedgeweights
TheBellman-FordSingle-SourceShortestPathAlgorithm
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
GraphGaweighted,directedgraphwithnegativeedgeweights
TheBellman-FordSingle-SourceShortestPathAlgorithm
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
GraphGaweighted,directedgraphwithnegativeedgeweights
TheBellman-FordSingle-SourceShortestPathAlgorithm
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
Ready?
Let’sgo!
GraphGaweighted,directedgraphwithnegativeedgeweights
TheBellman-FordSingle-SourceShortestPathAlgorithm
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
Graph Sourcevertex
Weightfunction
TheBellman-FordSingle-SourceShortestPathAlgorithm
// estimate of shortest path distance // predecessor vertex
0
∞ ∞
∞ ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
∞ ∞
∞ ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
∞ ∞
∞ ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
∞ ∞
∞ ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
// if x.d (∞) > t.d (∞) + w(t,x) (5) then // set x.d = t.d + w(t,x) // set predecessor vertex to t
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
Assume for any real number a: a + ∞ = ∞ + a = ∞
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
∞ ∞
∞ ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
// if y.d (∞) > t.d (∞) + w(t,y) (8) then // set y.d = t.d + w(t,y) // set predecessor vertex to t
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
∞ ∞
∞ ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
∞ ∞
∞ ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
∞ ∞
∞ ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
∞ ∞
∞ ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
∞ ∞
∞ ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
∞ ∞
∞ ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
∞ ∞
∞ ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
// if t.d (∞) > s.d (0) + w(s,t) (6) then // set t.d = s.d + w(s,t) = 6 // set predecessor vertex to s
Finally!
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 ∞
∞ ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
// if t.d (∞) > s.d (0) + w(s,t) (6) then // set t.d = s.d + w(s,t) = 6 // set predecessor vertex to s
Finally!
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 ∞
7 ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
// if y.d (∞) > s.d (0) + w(s,t) (7) then // set y.d = s.d + w(s,y) = 7 // set predecessor vertex to s
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 ∞
7 ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
2
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 ∞
7 ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
2
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 11
7 ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
2
Remember,theseareestimatesuntilweXinish.Canwedobetter?
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 11
7 ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
2
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 11
7 ∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
2
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 11
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
2
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 11
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
2
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 11
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
2
Remember,theseareestimatesuntilweXinish.WeCANdobetter!
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 4
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
2
Remember,theseareestimatesuntilweXinish.WeCANdobetter!
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 4
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
2
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 4
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
2
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 4
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
2
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 4
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
2
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 4
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
2
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 4
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
3
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 4
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
3
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 4
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
3
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 4
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
3
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
6 4
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
3
Remember,theseareestimatesuntilweXinish.WeCANdobetter!
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
3
Remember,theseareestimatesuntilweXinish.WeCANdobetter!
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
3
Therearenochangesafterthispointfortherestofthispass.Butwe’renotyetdone.
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
Ooohhh....
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
Ooohhh....
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
Therearenochangesafterthispointfortherestofthispass.Andnowwe’redone.
Almost.
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
Whatdoesthisdo?
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
no
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
no
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
no
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
no
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
no
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
no
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
no
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
no
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
no
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
no
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
Whatdoesthisdo?
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=1)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
1
Whatdoesthisdo?Whatifw(t,x)=1?
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=1)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
Whatdoesthisdo?Whatifw(t,x)=1?
1
yes
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -∞
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=1)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
Whatdoesthisdo?Whatifw(t,x)=1?We’dhaveanegativeweightcycle.That’s…interesting.Noww(s,z)=-∞.
1
yes
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
4
Whatdoesthisdo?Itdetectsnegative-weightcycles...andreturnsfalseisanyarefound.
TheBellman-FordSingle-SourceShortestPathAlgorithm
0
2 4
7 -2
GraphGaweighted,directedgraphwithnegativeedgeweights
G.V=s,t,x,y,zG.E=(t,x:w=5)
(t,y:w=8)(t,z:w=-4)(x,t:w=-2)(y,x:w=-3)(y,z:w=9)(z,x:w=7)(z,s:w=2)(s,t:w=6)(s,y:w=7)
Nowwe’redone.
Thevalueineachvertexisthelengthoftheshortestpathtothatvertexfromstartingvertexs.
s⇾s=0 shortestpath:ss⇾t=2 shortestpath:s⇾y⇾x⇾ts⇾x=4 shortestpath:s⇾y⇾xs⇾y=7 shortestpath:s⇾ys⇾z=-2 shortestpath:s⇾y⇾x⇾t⇾z
TheBellman-FordSingle-SourceShortestPathAlgorithm
new graphadd vertex 1add vertex 2add vertex 3add vertex 4add vertex 5add edge 2 - 3 5add edge 2 - 4 8add edge 2 - 5 -4add edge 3 - 2 -2add edge 4 - 3 -3add edge 4 - 5 9add edge 5 - 3 7add edge 5 - 1 2add edge 1 - 2 6add edge 1 - 4 7
v1
v2 v3
v4 v5
GraphGAssignment5versionwithintegervertexIDs