// BellmanFord implements bellman-ford algorithm for single-source shortest path problem
BellmanFord implements bellman-ford algorithm for single-source shortest path problem
// BellmanFord implements bellman - ford algorithm for single - source shortest path problem
func BellmanFord(g *weightedgraph.WeightedGraph, s graph.NodeID) (bool, *ShortestPaths) {
	sp := &ShortestPaths{}
	sp.initializeSingleSource(g, s)
	v := g.NodeCount()
	for i := 0; i < v-1; i++ { 
		g.IterateEdges(func(e graph.EdgeID) {
			w, _ := g.Weight(e.From, e.To)
			sp.relax(e.From, e.To, w)
		})
	}
	negativeCycle := false
	g.IterateEdges(func(e graph.EdgeID) {
		w, _ := g.Weight(e.From, e.To)
		if sp.relaxable(e.From, e.To, w) {
			negativeCycle = true
		}
	})
	if negativeCycle {
		return false, nil
	}
	return true, sp
}
func BellmanFord ( g * weightedgraph . WeightedGraph , s graph . NodeID ) ( bool , * ShortestPaths ) { sp := & ShortestPaths { } sp . initializeSingleSource ( g , s ) v := g . NodeCount ( ) for i := 0 ; i < v - 1 ; i ++ { g . IterateEdges ( func ( e graph . EdgeID ) { w , _ := g . Weight ( e . From , e . To ) sp . relax ( e . From , e . To , w ) } ) } negativeCycle := false g . IterateEdges ( func ( e graph . EdgeID ) { w , _ := g . Weight ( e . From , e . To ) if sp . relaxable ( e . From , e . To , w ) { negativeCycle = true } } ) if negativeCycle { return false , nil } return true , sp }
BellmanFord implements bellman-ford algorithm for single-source shortest path problem
BellmanFord implements bellman - ford algorithm for single - source shortest path problem
// iterate v-1 times //TODO: improvable - if negative cycle found, then can return directly.