You are on page 1of 17

This is an algorithm that solves the single source shortest p ath p rob lem ( sssp ) ( f ind s the d istances

and shortest p aths f rom a source to all other nod es) f or the case w here the w eights can b e negative. I f there is a negative cy cle, the shortest p ath and the d istance are not w ell d ef ined . The algorithm returns a negative cy cle ex ists. W e w ill see an ap p lication f or S olving a System of D i ffer en c e C on str a i n ts

Bellman Ford Algorithm

The id ea: There is a shortest p ath f rom s to any other vertex that d oes not contain a non-negative cy cle ( can b e eliminated to p rod uce a lighter p ath) .

Bellman-F o r d s A lg o r i t h m

The max imal numb er of w ith no cy cles is |V|-1 , b most |V | nod es on the p it is enough to check ed ges

ed ges in such a p ath ecause it can have at ath if there is no cy cle. p aths of up to |V|-1

Bellman-F o r d s A lg o r i t h m
Initialize(G, s) for i 1 to V 1 for each edge (v, w) E [G ]

do if dist(w) > dist(v) + c(v, w) dist ( w) = dist (v) + c(v, w) Parent ( w) = v

for each edge (v, w) E [G ]

if dist [w] > d [v ] + c(v, w) return " negative cycle"

Bellman-F o r d s A lg o r i t h m

The first pass over the edges on l y n eighb ors of s are affec ted ( 1 edge paths) . A l l shortest paths w ith on e edge are fou n d. The sec on d pass shortest paths of 2 edges are fou n d A fter |V|-1 passes, al l possib l e paths are c hec k ed. W e c l aim : w e n eed to u pdate an y vertex in the l ast pass if an d on l y if there is a n egative c y c l e reac hab l e from s in G .

 7 #  #  ) #     ! 7   $     %  $ #  5 8   #   A $  @   &   # ) #  (   (     #  $ %  !  $ % %  $  $  $  $     #    (   ) #  ( ! #  & ' !  $ %   $ # & ' # & '   5 '   & '$    %  4 $     B $ )  10 2 30   !    % $ ! #  $   5  #  #          #    #      $ 9  # !  & ) $        # #  $ 6  #    ) 8 

& '$  

v1 ,....vk 1 , vk = v1

Bellman Ford Algorithm


 # ! "

dist(w) dist(v) c(v, w)

W e c an w rite for al l edges in the c y c l e

dist(v3 ) dist(v 2 ) + c(v3 , v2 )

dist(v 2 ) dist(v1 ) + c(v2 , v1 )

... dist(v k ) dist(v k-1 ) + c(vk , vk


k k 1

dist (v ) dist (v ) + c(v , v


k 1 1 i i k i 2

k 1

A fter su m m in g u p over al l edges in the c y c l e, w e disc over that the term on the l eft is eq u al to the first term on the right( j u st a differen t order of su m m ation . ) W e c an su b tru c t them , an d w e get that the c y c l e is ac tu al l y positive, w hic h is a c on tradic tion .

Bellman-F o r d R u n T i me
The algorithm run G oes ov er v -1 v F or eac h v ertex A ltogether O ( V E

time is: ertex es, O ( V ) relax ation ov er E , O ( E ) )

A p p li c at i o n o f Bellman-F o r d

The B ellman-F ord algorithm c an b e used to solv e a set of d if f erenc e c onstraints f or n v ariab les, lik e the f ollow ing set of linear ineq ualities :

x1 x1 x2 x3 x4 x4 x5 x5

x2 x5 x5 x1 x1 x3 x3 x4

0 1 1 5 4 1 3 3

A p p li c at i o n o f Bellman-F o r d

There are many uses f or a set of d if f erenc e c onstraints, f or instanc e: The v ariab les c an rep resent the times of d if f erent ev ents The ineq ualities are the c onstraints ov er there sy nc hroniz ation.

A p p li c at i o n o f Bellman-F o r d
W hat is the c onnec tion b etw een the B ellman-F ord algorithm and a set of linear ineq ualities? W e c an interp ret the p rob lem as a d irec ted grap h. The grap h is c alled the constraint graph of the p rob lem A f ter c onstruc ting the grap h, w e c ould use the B ellmanF ord algorithm. The result of the B ellman-F ord algorithm is the v ec tor x that solv es the set of ineq ualities.

Building the constraint graph: E a c h v a r ia b le c o r r es p o nds to a no de

Application of Bellman-F or d

E a c h c o ns tr a int edge f r o m

x j x i bij c o r r es p o nds vi to v j w ith w eight bij

xi

vi

to a n

W e a dd a s p ec ia l no de a nd w e a dd edges f r o m o this s p ec ia l no de to a ll o ther no des . T he w eights o f thes e edges a r e 0 .

Application of Bellman-F or d
T he c o ns tr a int gr a p h:
0

v1 0 0 -1 v5 1 4 -3 v4 -3 -1 5 v3 v2

v0

0 0 0

Note the similarity between the constraints on the v ariables, and the cond itions f or the p ath to be shortest in the B ellman F ord alg orithm. W e now ap p ly the B ellman F ord alg orithm on the g rap h, with the sou rce being the ex tra nod e

vo

Application of Bellman-F or d
T he s o lutio n:

v1 -5 0 -1 v5 -4 0 -3 -1 v4 1 4 -3 -1 5 0 v3 0 -3 v2

v0

3  '   '    '     '     3  ' 4  ' '   5  ! " # $     $    ! "   #  $ %       2      '    (        '  4 %'           (    ( 0 1  ' (    &     ' #   '  & ('   ' (  )    

&

&   ! ' ' ' 0

  

 '

  



 

Application of Bellman-F or d
xi = dist (vi )
      

Suppose Bellman Ford did not find a cycle, and output a solution dist( v 1 ) ,dist( v 2 ) . So at th e final iteration of th e alg orith m, all edg es satisfied So our ch oice means th at all i i constraints are satisfied. N ow if Bellman-Ford did find a cycle, th en w e claim th ere can not b e a solution. Because if th ere is a solution, x1 ,...xn w h ich satisfies th e set of constraints, w e h av e x x b j i ij for all th e constraints. A mong all th ese ineq ualities, let us j ust pick th ose w h ich correspond to th e edg es in th e neg ativ e cycle in th e g raph . Summing all th ese constraints w e find j ust lik e b efore th at th e cycle is non neg ativ e, so w e g et a contradiction as b efore.

dist(w) dist(v) + c(v, w)

x = dist (v )

Application of B e llm an F or d
This shows that the Bellman Ford algorithm f inds a solu tion f or the set of dif f erenc e c onstraints, and if there is no solu tion, it tells u s ab ou t it. N ote that if we f ou nd a solu tion, adding to all v ariab les a c onstant c still giv es a solu tion. This is an ap p lic ation of the adv antage of the grap h data stru c tu re in a slightly less ex p ec ted p lac e than we saw b ef ore.

You might also like