W Bellman Ford Algorithm 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 e ists. 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-F o r d s A lg o r i t h m The id ea: There is a shortest p ath f rom s to any other verte that d oes not contain a non-negative cy cle ( can b e eliminated to p rod uce a lighter p ath. The ma imal numb er of ed ges in such a p ath w ith no cy cles is V -, b ecause it can have at most V nod es on the p ath if there is no cy cle. it is enough to chec p aths of up to V - ed ges
for Bellman-F o r d s A lg o r i t h m Initialize( G, s for i to V for each edge if 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 dist ( v, w E[ G] [ 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 ( 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 - passes, al l possib l e paths are c hec ed. W e c l aim : w e n eed to u pdate an y verte 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.
$ % ( & & # Bellman Ford Algorithm # "! #,... v v v, v = * % 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(v dist(v... dist(v 3 2 dist(v dist(v dist(v 2 - + c + c + ( v, v ( v, v c 3 2 2 ( v, v i 2 dist( v i dist( v 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. i + c( v, v
Bellman-F o r d R u n T i me The algorithm run time is: G oes ov er v - v erte es, O ( V F or eac h v erte rela ation ov er E, O ( E A ltogether O ( V 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, li e the f ollow ing set of linear ineq ualities : 3 3 4 5 4 5 3 5 3 4 4 3 5 2 5 2
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.
W A p p li c at i o n o f Bellman-F o r d 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 ellman- F ord algorithm. The result of the B ellman-f ord algorithm is the v ec tor that solv es the set of ineq ualities.
Application of Bellman-F or d 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 i E a c h c o ns tr a int j i ij c o r r es p o nds to a n edge f r o m v to v w ith w eight b i j ij b v i W e a dd a s p ec ia l no de v 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.
Application of Bellman-F or d T he c o ns tr a int gr a p h: v - v 2 v v 5 4 5-3 v 4-3 - v 3
W 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. e now ap p ly the B ellman F ord alg orithm on the g rap h, with the sou rce being the e tra nod e v o
Application of Bellman-F or d T he s o lutio n: v -5 - -3 v 2 v v 5-4 -3 v 4 4-5 -3 - v 3
! #! " Application of Bellman-F or d ( i i v = dist "
Suppose Bellman Ford did not find a cycle, and output a solution dist( v,dist( v 2. So at th e final iteration of th e alg orith m, all edg es satisfied dist(w dist(v + ( v w c, So our ch oice = dist( v means th at all constraints are satisfied. i i 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, w h ich satisfies th e set of constraints, w e h av e,... n b for all th e constraints. A mong all th ese ineq ualities, let us j ust pic 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 li e b efore th at th e cycle is non neg ativ e, so w e g et a contradiction as b efore. j i ij
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 e p ec ted p lac e than we saw b ef ore.