diff --git a/network/betweenness.go b/network/betweenness.go index 412d9910..3493e04b 100644 --- a/network/betweenness.go +++ b/network/betweenness.go @@ -102,7 +102,7 @@ func Betweenness(g graph.Graph) map[int]float64 { // // where \sigma_{st} and \sigma_{st}(v) are number of the shortest paths from s to t. // and the subset of those paths containing v respectively. -func BetweennessWeighted(g graph.CostGraph, p search.ShortestPaths) map[int]float64 { +func BetweennessWeighted(g graph.CostGraph, p search.AllShortest) map[int]float64 { cb := make(map[int]float64) nodes := g.NodeList() diff --git a/network/distance.go b/network/distance.go index 0cd10139..ee50b075 100644 --- a/network/distance.go +++ b/network/distance.go @@ -18,7 +18,7 @@ import ( // // For directed graphs the incoming paths are used. Infinite distances are // not considered. -func Closeness(g graph.Graph, p search.ShortestPaths) map[int]float64 { +func Closeness(g graph.Graph, p search.AllShortest) map[int]float64 { nodes := g.NodeList() c := make(map[int]float64, len(nodes)) for _, u := range nodes { @@ -45,7 +45,7 @@ func Closeness(g graph.Graph, p search.ShortestPaths) map[int]float64 { // // For directed graphs the incoming paths are used. Infinite distances are // not considered. -func Farness(g graph.Graph, p search.ShortestPaths) map[int]float64 { +func Farness(g graph.Graph, p search.AllShortest) map[int]float64 { nodes := g.NodeList() f := make(map[int]float64, len(nodes)) for _, u := range nodes { @@ -72,7 +72,7 @@ func Farness(g graph.Graph, p search.ShortestPaths) map[int]float64 { // // For directed graphs the incoming paths are used. Infinite distances are // not considered. -func Harmonic(g graph.Graph, p search.ShortestPaths) map[int]float64 { +func Harmonic(g graph.Graph, p search.AllShortest) map[int]float64 { nodes := g.NodeList() h := make(map[int]float64, len(nodes)) for i, u := range nodes { @@ -101,7 +101,7 @@ func Harmonic(g graph.Graph, p search.ShortestPaths) map[int]float64 { // // For directed graphs the incoming paths are used. Infinite distances are // not considered. -func Residual(g graph.Graph, p search.ShortestPaths) map[int]float64 { +func Residual(g graph.Graph, p search.AllShortest) map[int]float64 { nodes := g.NodeList() r := make(map[int]float64, len(nodes)) for i, u := range nodes { diff --git a/search/dijkstra.go b/search/dijkstra.go index a738a6cf..2b789bc9 100644 --- a/search/dijkstra.go +++ b/search/dijkstra.go @@ -77,7 +77,7 @@ func DijkstraFrom(u graph.Node, g graph.Graph, weight graph.CostFunc) Shortest { // DijkstraAllPaths will panic if g has a negative edge weight. // // The time complexity of DijkstrAllPaths is O(|V|.|E|+|V|^2.log|V|). -func DijkstraAllPaths(g graph.Graph, weight graph.CostFunc) (paths ShortestPaths) { +func DijkstraAllPaths(g graph.Graph, weight graph.CostFunc) (paths AllShortest) { var ( from = g.Neighbors edgeTo func(graph.Node, graph.Node) graph.Edge @@ -108,7 +108,7 @@ func DijkstraAllPaths(g graph.Graph, weight graph.CostFunc) (paths ShortestPaths for i := range dist { dist[i] = math.Inf(1) } - paths = ShortestPaths{ + paths = AllShortest{ nodes: nodes, indexOf: indexOf, diff --git a/search/dijkstra_test.go b/search/dijkstra_test.go index 48dec7ca..0fdeb8bc 100644 --- a/search/dijkstra_test.go +++ b/search/dijkstra_test.go @@ -103,7 +103,7 @@ func TestDijkstraAllPaths(t *testing.T) { } var ( - pt search.ShortestPaths + pt search.AllShortest panicked bool ) diff --git a/search/floydwarshall.go b/search/floydwarshall.go index deaab2b6..a5a07e42 100644 --- a/search/floydwarshall.go +++ b/search/floydwarshall.go @@ -16,7 +16,7 @@ import ( // implement graph.Coster, UniformCost is used. // // The time complexity of FloydWarshall is O(|V|^3). -func FloydWarshall(g graph.Graph, weight graph.CostFunc) (paths ShortestPaths, ok bool) { +func FloydWarshall(g graph.Graph, weight graph.CostFunc) (paths AllShortest, ok bool) { var ( from = g.Neighbors edgeTo func(graph.Node, graph.Node) graph.Edge @@ -47,7 +47,7 @@ func FloydWarshall(g graph.Graph, weight graph.CostFunc) (paths ShortestPaths, o for i := range dist { dist[i] = math.Inf(1) } - paths = ShortestPaths{ + paths = AllShortest{ nodes: nodes, indexOf: indexOf, diff --git a/search/johnson_apsp.go b/search/johnson_apsp.go index 78280ea0..dafac225 100644 --- a/search/johnson_apsp.go +++ b/search/johnson_apsp.go @@ -16,7 +16,7 @@ import ( // If weight is nil and the graph does not implement graph.Coster, UniformCost is used. // // The time complexity of JonsonAllPaths is O(|V|.|E|+|V|^2.log|V|). -func JohnsonAllPaths(g graph.Graph, weight graph.CostFunc) (paths ShortestPaths, ok bool) { +func JohnsonAllPaths(g graph.Graph, weight graph.CostFunc) (paths AllShortest, ok bool) { jg := johnsonWeightAdjuster{ g: g, from: g.Neighbors, diff --git a/search/shortest.go b/search/shortest.go index ae40b7e9..2ba2561b 100644 --- a/search/shortest.go +++ b/search/shortest.go @@ -102,8 +102,8 @@ func (p Shortest) To(v graph.Node) (path []graph.Node, weight float64) { return path, p.dist[p.indexOf[v.ID()]] } -// ShortestPaths is a shortest-path tree created by the FloydWarshall function. -type ShortestPaths struct { +// AllShortest is a shortest-path tree created by the FloydWarshall function. +type AllShortest struct { // nodes hold the nodes of the analysed // graph. nodes []graph.Node @@ -138,16 +138,16 @@ type ShortestPaths struct { forward bool } -func (p ShortestPaths) at(from, to int) (mid []int) { +func (p AllShortest) at(from, to int) (mid []int) { return p.next[from+to*len(p.nodes)] } -func (p ShortestPaths) set(from, to int, weight float64, mid ...int) { +func (p AllShortest) set(from, to int, weight float64, mid ...int) { p.dist.Set(from, to, weight) p.next[from+to*len(p.nodes)] = append(p.next[from+to*len(p.nodes)][:0], mid...) } -func (p ShortestPaths) add(from, to int, mid ...int) { +func (p AllShortest) add(from, to int, mid ...int) { loop: // These are likely to be rare, so just loop over collisions. for _, k := range mid { for _, v := range p.next[from+to*len(p.nodes)] { @@ -160,7 +160,7 @@ loop: // These are likely to be rare, so just loop over collisions. } // Weight returns the weight of the minimum path between u and v. -func (p ShortestPaths) Weight(u, v graph.Node) float64 { +func (p AllShortest) Weight(u, v graph.Node) float64 { from, fromOK := p.indexOf[u.ID()] to, toOK := p.indexOf[v.ID()] if !fromOK || !toOK { @@ -173,7 +173,7 @@ func (p ShortestPaths) Weight(u, v graph.Node) float64 { // one shortest path exists between u and v, a randomly chosen path will be returned and // unique is returned false. If a cycle with zero weight exists in the path, it will not // be included, but unique will be returned false. -func (p ShortestPaths) Between(u, v graph.Node) (path []graph.Node, weight float64, unique bool) { +func (p AllShortest) Between(u, v graph.Node) (path []graph.Node, weight float64, unique bool) { from, fromOK := p.indexOf[u.ID()] to, toOK := p.indexOf[v.ID()] if !fromOK || !toOK || len(p.at(from, to)) == 0 { @@ -226,7 +226,7 @@ func (p ShortestPaths) Between(u, v graph.Node) (path []graph.Node, weight float // AllBetween returns all shortest paths from u to v and the weight of the paths. Paths // containing zero-weight cycles are not returned. -func (p ShortestPaths) AllBetween(u, v graph.Node) (paths [][]graph.Node, weight float64) { +func (p AllShortest) AllBetween(u, v graph.Node) (paths [][]graph.Node, weight float64) { from, fromOK := p.indexOf[u.ID()] to, toOK := p.indexOf[v.ID()] if !fromOK || !toOK || len(p.at(from, to)) == 0 { @@ -245,7 +245,7 @@ func (p ShortestPaths) AllBetween(u, v graph.Node) (paths [][]graph.Node, weight return paths, p.dist.At(from, to) } -func (p ShortestPaths) allBetween(from, to int, seen []bool, path []graph.Node, paths [][]graph.Node) [][]graph.Node { +func (p AllShortest) allBetween(from, to int, seen []bool, path []graph.Node, paths [][]graph.Node) [][]graph.Node { if p.forward { seen[from] = true } else {