{"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":"school-run","name":"School run","description":"Six places on the morning drive to school. Most streets go both ways, and each edge is the travel time in minutes.","family":"road","directed":true,"weighted":true,"acyclic":false,"vertices":[{"id":"Home","x":128,"y":320},{"id":"Bakery","x":352,"y":160},{"id":"Bridge","x":352,"y":480},{"id":"Park","x":608,"y":320},{"id":"Library","x":832,"y":160},{"id":"School","x":864,"y":448}],"edges":[{"from":"Home","to":"Bakery","weight":4},{"from":"Bakery","to":"Home","weight":4},{"from":"Home","to":"Bridge","weight":3},{"from":"Bridge","to":"Home","weight":3},{"from":"Bakery","to":"Park","weight":3},{"from":"Park","to":"Bakery","weight":3},{"from":"Bridge","to":"Park","weight":2},{"from":"Park","to":"Bridge","weight":2},{"from":"Park","to":"Library","weight":4},{"from":"Library","to":"Park","weight":4},{"from":"Park","to":"School","weight":7},{"from":"Library","to":"School","weight":2},{"from":"School","to":"Library","weight":2}],"details":{"vertices":6,"edges":13,"directed":true,"weighted":true,"acyclic":false,"weightMin":2,"weightMax":7,"negativeWeights":false,"density":0.43333333333333335,"inDegree":{"min":2,"avg":2.1666666666666665,"max":3},"outDegree":{"min":1,"avg":2.1666666666666665,"max":4},"selfLoops":0,"components":1}},"problem":"How many minutes is the quickest drive from home to each place, including the school?","params":{"start":"Home"},"result":{"kind":"distances","values":{"Bakery":4,"Bridge":3,"Home":0,"Library":9,"Park":5,"School":11}},"answer":"From Home, the shortest distances are 3 to Bridge, 4 to Bakery, 5 to Park, 9 to Library and 11 to School.","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 Home, which is 0. Bellman-Ford checks all 13 edges in each of 5 passes, one fewer than the 6 vertices, then once more to look for a negative cycle.","ops":[{"v":"Home","state":"done"},{"panel":"dist","set":{"Home":"0"}},{"panel":"dist","set":{"Bakery":"∞"}},{"panel":"dist","set":{"Bridge":"∞"}},{"panel":"dist","set":{"Park":"∞"}},{"panel":"dist","set":{"Library":"∞"}},{"panel":"dist","set":{"School":"∞"}}]},{"say":"Pass 1. Home to Bakery: 0 + 4 = 4, so Bakery's distance becomes 4.","ops":[{"v":"Bakery","state":"current"},{"e":["Home","Bakery"],"state":"tree"},{"panel":"dist","set":{"Bakery":"4"}},{"panel":"changed","list":[{"key":"Bakery","note":"4"}]}]},{"say":"Pass 1. Home to Bridge: 0 + 3 = 3, so Bridge's distance becomes 3.","ops":[{"v":"Bridge","state":"current"},{"e":["Home","Bridge"],"state":"tree"},{"panel":"dist","set":{"Bridge":"3"}},{"panel":"changed","list":[{"key":"Bakery","note":"4"},{"key":"Bridge","note":"3"}]},{"v":"Bakery","state":"waiting"}]},{"say":"Pass 1. Bakery to Park: 4 + 3 = 7, so Park's distance becomes 7.","ops":[{"v":"Park","state":"current"},{"e":["Bakery","Park"],"state":"tree"},{"panel":"dist","set":{"Park":"7"}},{"panel":"changed","list":[{"key":"Bakery","note":"4"},{"key":"Bridge","note":"3"},{"key":"Park","note":"7"}]},{"v":"Bridge","state":"waiting"}]},{"say":"Pass 1. Bridge to Park: 3 + 2 = 5, shorter than 7, so Park's distance becomes 5.","ops":[{"v":"Park","state":"current"},{"e":["Bridge","Park"],"state":"tree"},{"panel":"dist","set":{"Park":"5"}},{"panel":"changed","list":[{"key":"Bakery","note":"4"},{"key":"Bridge","note":"3"},{"key":"Park","note":"5"}]},{"e":["Bakery","Park"],"state":"idle"}]},{"say":"Pass 1. Park to Library: 5 + 4 = 9, so Library's distance becomes 9.","ops":[{"v":"Library","state":"current"},{"e":["Park","Library"],"state":"tree"},{"panel":"dist","set":{"Library":"9"}},{"panel":"changed","list":[{"key":"Bakery","note":"4"},{"key":"Bridge","note":"3"},{"key":"Park","note":"5"},{"key":"Library","note":"9"}]},{"v":"Park","state":"waiting"}]},{"say":"Pass 1. Park to School: 5 + 7 = 12, so School's distance becomes 12.","ops":[{"v":"School","state":"current"},{"e":["Park","School"],"state":"tree"},{"panel":"dist","set":{"School":"12"}},{"panel":"changed","list":[{"key":"Bakery","note":"4"},{"key":"Bridge","note":"3"},{"key":"Park","note":"5"},{"key":"Library","note":"9"},{"key":"School","note":"12"}]},{"v":"Library","state":"waiting"}]},{"say":"Pass 1. Library to School: 9 + 2 = 11, shorter than 12, so School's distance becomes 11.","ops":[{"v":"School","state":"current"},{"e":["Library","School"],"state":"tree"},{"panel":"dist","set":{"School":"11"}},{"panel":"changed","list":[{"key":"Bakery","note":"4"},{"key":"Bridge","note":"3"},{"key":"Park","note":"5"},{"key":"Library","note":"9"},{"key":"School","note":"11"}]},{"e":["Park","School"],"state":"idle"}]},{"say":"Pass 1 done. 5 distances changed.","ops":[{"v":"Bakery","state":"done"},{"v":"Bridge","state":"done"},{"v":"Park","state":"done"},{"v":"Library","state":"done"},{"v":"School","state":"done"}]},{"say":"Pass 2 done. No distance changed, so later passes won't change any either, but gograph runs them all.","ops":[{"panel":"changed","list":[]}]},{"say":"Pass 3 done. No distance changed."},{"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 Home 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, \"Home\")\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(\"School\")\nif !ok {\n\treturn nil // out of reach\n}\nroute, _ := paths.PathTo(\"School\")\nfmt.Println(dist)\nfor _, v := range route {\n\tfmt.Println(v.Label())\n}"}
