forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bellman_ford_moore.go
59 lines (51 loc) · 1.46 KB
/
bellman_ford_moore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright ©2015 The gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package path
import "github.com/gonum/graph"
// BellmanFordFrom returns a shortest-path tree for a shortest path from u to all nodes in
// the graph g, or false indicating that a negative cycle exists in the graph. If the graph
// does not implement graph.Weighter, graph.UniformCost is used.
//
// The time complexity of BellmanFordFrom is O(|V|.|E|).
func BellmanFordFrom(u graph.Node, g graph.Graph) (path Shortest, ok bool) {
if !g.Has(u) {
return Shortest{from: u}, true
}
var weight graph.WeightFunc
if g, ok := g.(graph.Weighter); ok {
weight = g.Weight
} else {
weight = graph.UniformCost
}
nodes := g.Nodes()
path = newShortestFrom(u, nodes)
path.dist[path.indexOf[u.ID()]] = 0
// TODO(kortschak): Consider adding further optimisations
// from http://arxiv.org/abs/1111.5414.
for i := 1; i < len(nodes); i++ {
changed := false
for j, u := range nodes {
for _, v := range g.From(u) {
k := path.indexOf[v.ID()]
joint := path.dist[j] + weight(g.Edge(u, v))
if joint < path.dist[k] {
path.set(k, joint, j)
changed = true
}
}
}
if !changed {
break
}
}
for j, u := range nodes {
for _, v := range g.From(u) {
k := path.indexOf[v.ID()]
if path.dist[j]+weight(g.Edge(u, v)) < path.dist[k] {
return path, false
}
}
}
return path, true
}