{"algorithm":"bellman-ford","about":["Bellman-Ford starts like Dijkstra, with 0 at the start and infinity everywhere else, but it doesn't take vertices in order of distance. It goes through every edge, and when an edge gives its end a shorter distance, it lowers that distance. It repeats this pass one time fewer than there are vertices. A shortest path that doesn't repeat a vertex has at most that many edges, so after the last pass every distance is final.","Then it makes one more pass. If an edge can still shorten a distance, the start can reach a cycle whose weights add up to less than zero. Such a graph has no shortest path, since each trip around the cycle makes the path shorter, and path.BellmanFordPaths returns ErrNegativeWeightCycle. It needs a directed, weighted graph and costs O(V·E). path.NegativeCycle then returns one such cycle. It searches the whole graph rather than what one start reaches, so on a graph with more than one it can return a cycle other than the one the start runs into.","When there is no negative cycle, Distance and PathTo on the result give each distance and the route there, start first, and report false for a vertex out of reach. Of two routes that are equally short, PathTo returns the one with the fewest edges.","Use it when some weights are negative, such as refunds, rewards or the negative logarithms of exchange rates, where a negative cycle is a chance for arbitrage. When no weight is negative, Dijkstra gives the same distances faster."],"template":{"id":"fx-majors","name":"Major currencies","description":"Six major currencies and the rates a dealer quotes between them. Each edge is a trade, and its weight is minus the natural log of the exchange rate, so the shortest path is the best rate. Dollars buy more francs through EUR than directly. The rates are illustrative and close to market rates, and every loop of trades loses a little to the spread.","family":"currency","directed":true,"weighted":true,"acyclic":false,"vertices":[{"id":"CAD","x":736,"y":592},{"id":"CHF","x":152,"y":256},{"id":"EUR","x":500,"y":96},{"id":"GBP","x":848,"y":256},{"id":"JPY","x":264,"y":592},{"id":"USD","x":500,"y":352}],"edges":[{"from":"CAD","to":"USD","weight":0.319819664},{"from":"CAD","to":"GBP","weight":0.579175845},{"from":"CAD","to":"JPY","weight":-4.691806493},{"from":"CHF","to":"USD","weight":-0.156490797},{"from":"CHF","to":"JPY","weight":-5.170143028},{"from":"CHF","to":"EUR","weight":-0.065225785},{"from":"EUR","to":"USD","weight":-0.094309679},{"from":"EUR","to":"GBP","weight":0.168560674},{"from":"EUR","to":"CHF","weight":0.069210752},{"from":"GBP","to":"USD","weight":-0.260901657},{"from":"GBP","to":"EUR","weight":-0.165514438},{"from":"GBP","to":"CAD","weight":-0.575151622},{"from":"JPY","to":"USD","weight":5.011635794},{"from":"JPY","to":"CAD","weight":4.699832576},{"from":"JPY","to":"CHF","weight":5.176152833},{"from":"USD","to":"EUR","weight":0.096312682},{"from":"USD","to":"GBP","weight":0.263861385},{"from":"USD","to":"JPY","weight":-5.009634794},{"from":"USD","to":"CHF","weight":0.168537002},{"from":"USD","to":"CAD","weight":-0.309834877}],"details":{"vertices":6,"edges":20,"directed":true,"weighted":true,"acyclic":false,"weightMin":-5.170143028,"weightMax":5.176152833,"negativeWeights":true,"density":0.6666666666666666,"inDegree":{"min":3,"avg":3.3333333333333335,"max":5},"outDegree":{"min":3,"avg":3.3333333333333335,"max":5},"selfLoops":0,"components":1}},"problem":"How much of each currency can a dollar buy at the best rate, trading through other currencies when that pays more?","params":{"start":"USD"},"result":{"kind":"distances","values":{"CAD":-0.311290237,"CHF":0.165523434,"EUR":0.096312682,"GBP":0.263861385,"JPY":-5.009634794,"USD":0}},"answer":"At the best rates, 1 USD buys 149.85 JPY, 1.3652 CAD, 0.90818 EUR, 0.84745 CHF and 0.76808 GBP.","legend":{"vertex":[{"id":"idle","label":"Not reached","color":"#64748B","shape":"hollow"},{"id":"waiting","label":"Changed in this pass","color":"#E69F00","shape":"dashed-square"},{"id":"current","label":"Latest change","color":"#56B4E9","shape":"ring"},{"id":"done","label":"Reached","color":"#009E73","shape":"filled"}],"edge":[{"id":"idle","label":"Edge","color":"#475569","shape":"line"},{"id":"tree","label":"Best way in so far","color":"#009E73","shape":"thick"},{"id":"error","label":"Negative cycle","color":"#D55E00","shape":"dashed"}],"panels":[{"id":"dist","title":"Distances","kind":"table"},{"id":"changed","title":"Changed in this pass","kind":"list"}]},"steps":[{"say":"Every distance starts at ∞, except USD, which is 0. Bellman-Ford checks all 20 edges in each of 5 passes, one fewer than the 6 vertices, then once more to look for a negative cycle.","ops":[{"v":"USD","state":"done"},{"panel":"dist","set":{"CAD":"∞"}},{"panel":"dist","set":{"CHF":"∞"}},{"panel":"dist","set":{"EUR":"∞"}},{"panel":"dist","set":{"GBP":"∞"}},{"panel":"dist","set":{"JPY":"∞"}},{"panel":"dist","set":{"USD":"0"}}]},{"say":"Pass 1. USD to EUR at 0.90818: 0 + 0.0963 = 0.0963, so EUR's distance becomes 0.0963: 1 USD buys 0.90818 EUR this way.","ops":[{"v":"EUR","state":"current"},{"e":["USD","EUR"],"state":"tree"},{"panel":"dist","set":{"EUR":"0.0963"}},{"panel":"changed","list":[{"key":"EUR","note":"0.0963"}]}]},{"say":"Pass 1. USD to GBP at 0.76808: 0 + 0.2639 = 0.2639, so GBP's distance becomes 0.2639: 1 USD buys 0.76808 GBP this way.","ops":[{"v":"GBP","state":"current"},{"e":["USD","GBP"],"state":"tree"},{"panel":"dist","set":{"GBP":"0.2639"}},{"panel":"changed","list":[{"key":"EUR","note":"0.0963"},{"key":"GBP","note":"0.2639"}]},{"v":"EUR","state":"waiting"}]},{"say":"Pass 1. USD to JPY at 149.85: 0 - 5.0096 = -5.0096, so JPY's distance becomes -5.0096: 1 USD buys 149.85 JPY this way.","ops":[{"v":"JPY","state":"current"},{"e":["USD","JPY"],"state":"tree"},{"panel":"dist","set":{"JPY":"-5.0096"}},{"panel":"changed","list":[{"key":"EUR","note":"0.0963"},{"key":"GBP","note":"0.2639"},{"key":"JPY","note":"-5.0096"}]},{"v":"GBP","state":"waiting"}]},{"say":"Pass 1. USD to CHF at 0.8449: 0 + 0.1685 = 0.1685, so CHF's distance becomes 0.1685: 1 USD buys 0.8449 CHF this way.","ops":[{"v":"CHF","state":"current"},{"e":["USD","CHF"],"state":"tree"},{"panel":"dist","set":{"CHF":"0.1685"}},{"panel":"changed","list":[{"key":"EUR","note":"0.0963"},{"key":"GBP","note":"0.2639"},{"key":"JPY","note":"-5.0096"},{"key":"CHF","note":"0.1685"}]},{"v":"JPY","state":"waiting"}]},{"say":"Pass 1. USD to CAD at 1.3632: 0 - 0.3098 = -0.3098, so CAD's distance becomes -0.3098: 1 USD buys 1.3632 CAD this way.","ops":[{"v":"CAD","state":"current"},{"e":["USD","CAD"],"state":"tree"},{"panel":"dist","set":{"CAD":"-0.3098"}},{"panel":"changed","list":[{"key":"EUR","note":"0.0963"},{"key":"GBP","note":"0.2639"},{"key":"JPY","note":"-5.0096"},{"key":"CHF","note":"0.1685"},{"key":"CAD","note":"-0.3098"}]},{"v":"CHF","state":"waiting"}]},{"say":"Pass 1 done. 5 distances changed.","ops":[{"v":"EUR","state":"done"},{"v":"GBP","state":"done"},{"v":"JPY","state":"done"},{"v":"CHF","state":"done"},{"v":"CAD","state":"done"}]},{"say":"Pass 2. EUR to CHF at 0.93313: 0.0963 + 0.0692 = 0.1655, shorter than 0.1685, so CHF's distance becomes 0.1655: 1 USD buys 0.84745 CHF this way.","ops":[{"v":"CHF","state":"current"},{"e":["EUR","CHF"],"state":"tree"},{"panel":"dist","set":{"CHF":"0.1655"}},{"panel":"changed","list":[{"key":"CHF","note":"0.1655"}]},{"e":["USD","CHF"],"state":"idle"}]},{"say":"Pass 2. GBP to CAD at 1.7774: 0.2639 - 0.5752 = -0.3113, shorter than -0.3098, so CAD's distance becomes -0.3113: 1 USD buys 1.3652 CAD this way.","ops":[{"v":"CAD","state":"current"},{"e":["GBP","CAD"],"state":"tree"},{"panel":"dist","set":{"CAD":"-0.3113"}},{"panel":"changed","list":[{"key":"CHF","note":"0.1655"},{"key":"CAD","note":"-0.3113"}]},{"e":["USD","CAD"],"state":"idle"},{"v":"CHF","state":"waiting"}]},{"say":"Pass 2 done. The distances of CHF and CAD changed.","ops":[{"v":"CHF","state":"done"},{"v":"CAD","state":"done"}]},{"say":"Pass 3 done. No distance changed, so later passes won't change any either, but gograph runs them all.","ops":[{"panel":"changed","list":[]}]},{"say":"Pass 4 done. No distance changed."},{"say":"Pass 5 done. No distance changed."},{"say":"Extra pass. No edge gives a shorter distance, so there is no negative cycle. Every distance from USD is final."}],"traceNote":"","verification":"The steps come from a step-by-step copy of the algorithm. Its distances and the route it draws to each vertex are checked against Distance and PathTo of path.BellmanFordPaths on every start. Of two routes that are equally short, BellmanFordPaths takes the one with the fewest edges, and the steps keep the last edge that lowered each distance, so the drawn route can be another one as short. On a negative cycle, path.NegativeCycle searches the whole graph, so the check takes any loop whose weights add up to less than zero.","snippet":"paths, err := path.BellmanFordPaths(g, \"USD\")\nif errors.Is(err, path.ErrNegativeWeightCycle) {\n\tloop, _ := path.NegativeCycle(g)\n\tfor _, v := range loop {\n\t\tfmt.Println(v.Label())\n\t}\n\treturn err\n}\nif err != nil {\n\treturn err\n}\ndist, ok := paths.Distance(\"GBP\")\nif !ok {\n\treturn nil // out of reach\n}\nroute, _ := paths.PathTo(\"GBP\")\nfmt.Println(dist)\nfor _, v := range route {\n\tfmt.Println(v.Label())\n}"}
