Files
gonum/graph/path/dijkstra_test.go
Dan Kortschak 91a06ac64c graph/path: reduce allocations in path reconstructions
│   old.bench   │               new.bench               │
                                              │    sec/op     │    sec/op      vs base                │
ShortestAlts/AllTo_100×2|0.5(17)-8              12.380µ ±  9%   8.577µ ±   6%  -30.72% (p=0.000 n=10)
ShortestAlts/AllTo_1000×2|0.5(51)-8              74.87µ ± 17%   47.66µ ±   1%  -36.35% (p=0.000 n=10)
ShortestAlts/AllTo_100×4|0.25(53)-8              25.50µ ±  3%   15.35µ ±   3%  -39.82% (p=0.000 n=10)
ShortestAlts/AllTo_1000×4|0.25(574)-8            556.5µ ±  5%   279.2µ ±   4%  -49.83% (p=0.000 n=10)
ShortestAlts/AllTo_100×16|0.1(76)-8              35.79µ ±  9%   21.71µ ±   3%  -39.35% (p=0.000 n=10)
ShortestAlts/AllTo_1000×16|0.1(822)-8            851.8µ ±  6%   400.5µ ±   6%  -52.98% (p=0.000 n=10)
AllShortest/AllBetween_100×2|0.5(17)-8          12.482µ ±  6%   8.573µ ±   3%  -31.32% (p=0.000 n=10)
AllShortest/AllBetween_1000×2|0.5(51)-8          80.74µ ±  2%   52.90µ ±   4%  -34.48% (p=0.000 n=10)
AllShortest/AllBetween_100×4|0.25(53)-8          22.54µ ±  4%   16.17µ ±   4%  -28.26% (p=0.000 n=10)
AllShortest/AllBetween_1000×4|0.25(574)-8        543.8µ ±  3%   326.7µ ±   5%  -39.93% (p=0.000 n=10)
AllShortest/AllBetween_100×16|0.1(76)-8          31.21µ ±  1%   22.45µ ±   3%  -28.07% (p=0.000 n=10)
AllShortest/AllBetween_1000×16|0.1(822)-8        678.4µ ±  1%   402.5µ ±   3%  -40.67% (p=0.000 n=10)
ShortestAlts/AllToFunc_100×2|0.5(17)-8                          5.892µ ±   2%
ShortestAlts/AllToFunc_1000×2|0.5(51)-8                         40.81µ ±   2%
ShortestAlts/AllToFunc_100×4|0.25(53)-8                         9.178µ ±   2%
ShortestAlts/AllToFunc_1000×4|0.25(574)-8                       214.6µ ±   2%
ShortestAlts/AllToFunc_100×16|0.1(76)-8                         12.62µ ±   3%
ShortestAlts/AllToFunc_1000×16|0.1(822)-8                       310.7µ ±   3%
AllShortest/AllBetweenFunc_100×2|0.5(17)-8                      5.713µ ±   1%
AllShortest/AllBetweenFunc_1000×2|0.5(51)-8                     45.68µ ±   3%
AllShortest/AllBetweenFunc_100×4|0.25(53)-8                     9.775µ ±   1%
AllShortest/AllBetweenFunc_1000×4|0.25(574)-8                   245.2µ ±   5%
AllShortest/AllBetweenFunc_100×16|0.1(76)-8                     13.76µ ±   1%
AllShortest/AllBetweenFunc_1000×16|0.1(822)-8                   314.7µ ±   1%
geomean                                          82.87µ         43.03µ         -38.14%

                                              │   old.bench   │              new.bench               │
                                              │     B/op      │     B/op      vs base                │
ShortestAlts/AllTo_100×2|0.5(17)-8              14.148Ki ± 0%   9.727Ki ± 0%  -31.25% (p=0.000 n=10)
ShortestAlts/AllTo_1000×2|0.5(51)-8              189.8Ki ± 0%   127.5Ki ± 0%  -32.80% (p=0.000 n=10)
ShortestAlts/AllTo_100×4|0.25(53)-8              22.32Ki ± 0%   14.99Ki ± 0%  -32.83% (p=0.000 n=10)
ShortestAlts/AllTo_1000×4|0.25(574)-8           1272.9Ki ± 0%   681.9Ki ± 0%  -46.42% (p=0.000 n=10)
ShortestAlts/AllTo_100×16|0.1(76)-8              33.23Ki ± 0%   22.66Ki ± 0%  -31.79% (p=0.000 n=10)
ShortestAlts/AllTo_1000×16|0.1(822)-8           1799.9Ki ± 0%   953.2Ki ± 0%  -47.04% (p=0.000 n=10)
AllShortest/AllBetween_100×2|0.5(17)-8          13.039Ki ± 0%   8.617Ki ± 0%  -33.91% (p=0.000 n=10)
AllShortest/AllBetween_1000×2|0.5(51)-8          181.5Ki ± 0%   119.3Ki ± 0%  -34.29% (p=0.000 n=10)
AllShortest/AllBetween_100×4|0.25(53)-8          21.34Ki ± 0%   14.01Ki ± 0%  -34.35% (p=0.000 n=10)
AllShortest/AllBetween_1000×4|0.25(574)-8       1264.8Ki ± 0%   673.8Ki ± 0%  -46.72% (p=0.000 n=10)
AllShortest/AllBetween_100×16|0.1(76)-8          32.24Ki ± 0%   21.68Ki ± 0%  -32.76% (p=0.000 n=10)
AllShortest/AllBetween_1000×16|0.1(822)-8       1791.8Ki ± 0%   945.1Ki ± 0%  -47.25% (p=0.000 n=10)
ShortestAlts/AllToFunc_100×2|0.5(17)-8                          6.922Ki ± 0%
ShortestAlts/AllToFunc_1000×2|0.5(51)-8                         119.8Ki ± 0%
ShortestAlts/AllToFunc_100×4|0.25(53)-8                         9.531Ki ± 0%
ShortestAlts/AllToFunc_1000×4|0.25(574)-8                       611.1Ki ± 0%
ShortestAlts/AllToFunc_100×16|0.1(76)-8                         13.12Ki ± 0%
ShortestAlts/AllToFunc_1000×16|0.1(822)-8                       870.7Ki ± 0%
AllShortest/AllBetweenFunc_100×2|0.5(17)-8                      5.812Ki ± 0%
AllShortest/AllBetweenFunc_1000×2|0.5(51)-8                     111.5Ki ± 0%
AllShortest/AllBetweenFunc_100×4|0.25(53)-8                     8.547Ki ± 0%
AllShortest/AllBetweenFunc_1000×4|0.25(574)-8                   603.0Ki ± 0%
AllShortest/AllBetweenFunc_100×16|0.1(76)-8                     12.14Ki ± 0%
AllShortest/AllBetweenFunc_1000×16|0.1(822)-8                   862.6Ki ± 0%
geomean                                          126.5Ki        68.27Ki       -37.99%

                                              │  old.bench  │              new.bench              │
                                              │  allocs/op  │  allocs/op   vs base                │
ShortestAlts/AllTo_100×2|0.5(17)-8              141.00 ± 0%    94.00 ± 0%  -33.33% (p=0.000 n=10)
ShortestAlts/AllTo_1000×2|0.5(51)-8              420.0 ± 0%    269.0 ± 0%  -35.95% (p=0.000 n=10)
ShortestAlts/AllTo_100×4|0.25(53)-8              279.0 ± 0%    174.0 ± 0%  -37.63% (p=0.000 n=10)
ShortestAlts/AllTo_1000×4|0.25(574)-8           2.888k ± 0%   1.741k ± 0%  -39.72% (p=0.000 n=10)
ShortestAlts/AllTo_100×16|0.1(76)-8              395.0 ± 0%    244.0 ± 0%  -38.23% (p=0.000 n=10)
ShortestAlts/AllTo_1000×16|0.1(822)-8           4.128k ± 0%   2.485k ± 0%  -39.80% (p=0.000 n=10)
AllShortest/AllBetween_100×2|0.5(17)-8          136.00 ± 0%    89.00 ± 0%  -34.56% (p=0.000 n=10)
AllShortest/AllBetween_1000×2|0.5(51)-8          415.0 ± 0%    264.0 ± 0%  -36.39% (p=0.000 n=10)
AllShortest/AllBetween_100×4|0.25(53)-8          275.0 ± 0%    170.0 ± 0%  -38.18% (p=0.000 n=10)
AllShortest/AllBetween_1000×4|0.25(574)-8       2.884k ± 0%   1.737k ± 0%  -39.77% (p=0.000 n=10)
AllShortest/AllBetween_100×16|0.1(76)-8          391.0 ± 0%    240.0 ± 0%  -38.62% (p=0.000 n=10)
AllShortest/AllBetween_1000×16|0.1(822)-8       4.124k ± 0%   2.481k ± 0%  -39.84% (p=0.000 n=10)
ShortestAlts/AllToFunc_100×2|0.5(17)-8                         71.00 ± 0%
ShortestAlts/AllToFunc_1000×2|0.5(51)-8                        211.0 ± 0%
ShortestAlts/AllToFunc_100×4|0.25(53)-8                        114.0 ± 0%
ShortestAlts/AllToFunc_1000×4|0.25(574)-8                     1.156k ± 0%
ShortestAlts/AllToFunc_100×16|0.1(76)-8                        160.0 ± 0%
ShortestAlts/AllToFunc_1000×16|0.1(822)-8                     1.652k ± 0%
AllShortest/AllBetweenFunc_100×2|0.5(17)-8                     66.00 ± 0%
AllShortest/AllBetweenFunc_1000×2|0.5(51)-8                    206.0 ± 0%
AllShortest/AllBetweenFunc_100×4|0.25(53)-8                    110.0 ± 0%
AllShortest/AllBetweenFunc_1000×4|0.25(574)-8                 1.152k ± 0%
AllShortest/AllBetweenFunc_100×16|0.1(76)-8                    156.0 ± 0%
AllShortest/AllBetweenFunc_1000×16|0.1(822)-8                 1.648k ± 0%
geomean                                          649.3         336.5       -37.70%
2023-04-24 20:31:56 +09:30

385 lines
11 KiB
Go

// 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 (
"math"
"reflect"
"testing"
"gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/internal/ordered"
"gonum.org/v1/gonum/graph/path/internal/testgraphs"
"gonum.org/v1/gonum/graph/simple"
"gonum.org/v1/gonum/graph/traverse"
)
func TestDijkstraFrom(t *testing.T) {
t.Parallel()
for _, test := range testgraphs.ShortestPathTests {
g := test.Graph()
for _, e := range test.Edges {
g.SetWeightedEdge(e)
}
for _, tg := range []struct {
typ string
g traverse.Graph
}{
{"complete", g.(graph.Graph)},
{"incremental", incremental{g.(graph.Weighted)}},
} {
var (
pt Shortest
panicked bool
)
func() {
defer func() {
panicked = recover() != nil
}()
pt = DijkstraFrom(test.Query.From(), tg.g)
}()
if panicked || test.HasNegativeWeight {
if !test.HasNegativeWeight {
t.Errorf("%q %s: unexpected panic", test.Name, tg.typ)
}
if !panicked {
t.Errorf("%q %s: expected panic for negative edge weight", test.Name, tg.typ)
}
continue
}
if pt.From().ID() != test.Query.From().ID() {
t.Fatalf("%q %s: unexpected from node ID: got:%d want:%d", test.Name, tg.typ, pt.From().ID(), test.Query.From().ID())
}
p, weight := pt.To(test.Query.To().ID())
if weight != test.Weight {
t.Errorf("%q %s: unexpected weight from To: got:%f want:%f",
test.Name, tg.typ, weight, test.Weight)
}
if weight := pt.WeightTo(test.Query.To().ID()); weight != test.Weight {
t.Errorf("%q %s: unexpected weight from Weight: got:%f want:%f",
test.Name, tg.typ, weight, test.Weight)
}
var got []int64
for _, n := range p {
got = append(got, n.ID())
}
ok := len(got) == 0 && len(test.WantPaths) == 0
for _, sp := range test.WantPaths {
if reflect.DeepEqual(got, sp) {
ok = true
break
}
}
if !ok {
t.Errorf("%q %s: unexpected shortest path:\ngot: %v\nwant from:%v",
test.Name, tg.typ, p, test.WantPaths)
}
np, weight := pt.To(test.NoPathFor.To().ID())
if pt.From().ID() == test.NoPathFor.From().ID() && (np != nil || !math.IsInf(weight, 1)) {
t.Errorf("%q %s: unexpected path:\ngot: path=%v weight=%f\nwant:path=<nil> weight=+Inf",
test.Name, tg.typ, np, weight)
}
}
}
}
func TestDijkstraAllFrom(t *testing.T) {
t.Parallel()
for _, test := range testgraphs.ShortestPathTests {
g := test.Graph()
for _, e := range test.Edges {
g.SetWeightedEdge(e)
}
for _, tg := range []struct {
typ string
g traverse.Graph
}{
{"complete", g.(graph.Graph)},
{"incremental", incremental{g.(graph.Weighted)}},
} {
var (
pt ShortestAlts
panicked bool
)
func() {
defer func() {
panicked = recover() != nil
}()
pt = DijkstraAllFrom(test.Query.From(), tg.g)
}()
if panicked || test.HasNegativeWeight {
if !test.HasNegativeWeight {
t.Errorf("%q %s: unexpected panic", test.Name, tg.typ)
}
if !panicked {
t.Errorf("%q %s: expected panic for negative edge weight", test.Name, tg.typ)
}
continue
}
if pt.From().ID() != test.Query.From().ID() {
t.Fatalf("%q %s: unexpected from node ID: got:%d want:%d", test.Name, tg.typ, pt.From().ID(), test.Query.From().ID())
}
// Test single path results.
p, weight, unique := pt.To(test.Query.To().ID())
if weight != test.Weight {
t.Errorf("%q %s: unexpected weight from To: got:%f want:%f",
test.Name, tg.typ, weight, test.Weight)
}
if weight := pt.WeightTo(test.Query.To().ID()); weight != test.Weight {
t.Errorf("%q %s: unexpected weight from Weight: got:%f want:%f",
test.Name, tg.typ, weight, test.Weight)
}
var gotPath []int64
for _, n := range p {
gotPath = append(gotPath, n.ID())
}
ok := len(gotPath) == 0 && len(test.WantPaths) == 0
for _, sp := range test.WantPaths {
if reflect.DeepEqual(gotPath, sp) {
ok = true
break
}
}
if !ok {
t.Errorf("%q: unexpected shortest path:\ngot: %v\nwant from:%v",
test.Name, p, test.WantPaths)
}
if unique != test.HasUniquePath {
t.Errorf("%q: unexpected uniqueness from To: got:%t want:%t (%d paths)",
test.Name, unique, test.HasUniquePath, len(test.WantPaths))
}
// Test multiple path results.
paths, weight := pt.AllTo(test.Query.To().ID())
if weight != test.Weight {
t.Errorf("%q: unexpected weight from AllTo: got:%f want:%f",
test.Name, weight, test.Weight)
}
if weight := pt.WeightTo(test.Query.To().ID()); weight != test.Weight {
t.Errorf("%q: unexpected weight from Weight: got:%f want:%f",
test.Name, weight, test.Weight)
}
var gotPaths [][]int64
if len(paths) != 0 {
gotPaths = make([][]int64, len(paths))
}
for i, p := range paths {
for _, v := range p {
gotPaths[i] = append(gotPaths[i], v.ID())
}
}
ordered.BySliceValues(gotPaths)
if !reflect.DeepEqual(gotPaths, test.WantPaths) {
t.Errorf("testing %q: unexpected shortest paths:\ngot: %v\nwant:%v",
test.Name, gotPaths, test.WantPaths)
}
paths = paths[:0]
pt.AllToFunc(test.Query.To().ID(), func(path []graph.Node) {
paths = append(paths, append([]graph.Node(nil), path...))
})
if weight != test.Weight {
t.Errorf("%q %s: unexpected weight from AllTo: got:%f want:%f",
test.Name, tg.typ, weight, test.Weight)
}
if weight := pt.WeightTo(test.Query.To().ID()); !math.IsInf(test.Weight, -1) && weight != test.Weight {
t.Errorf("%q %s: unexpected weight from Weight: got:%f want:%f",
test.Name, tg.typ, weight, test.Weight)
}
gotPaths = nil
if len(paths) != 0 {
gotPaths = make([][]int64, len(paths))
}
for i, p := range paths {
for _, v := range p {
gotPaths[i] = append(gotPaths[i], v.ID())
}
}
ordered.BySliceValues(gotPaths)
if !reflect.DeepEqual(gotPaths, test.WantPaths) {
t.Errorf("testing %q %s: unexpected shortest paths:\ngot: %v\nwant:%v",
test.Name, tg.typ, gotPaths, test.WantPaths)
}
// Test absent paths.
np, weight, unique := pt.To(test.NoPathFor.To().ID())
if pt.From().ID() == test.NoPathFor.From().ID() && !(np == nil && math.IsInf(weight, 1) && !unique) {
t.Errorf("%q: unexpected path:\ngot: path=%v weight=%f unique=%t\nwant:path=<nil> weight=+Inf unique=false",
test.Name, np, weight, unique)
}
}
}
}
type weightedTraverseGraph interface {
traverse.Graph
Weighted
}
type incremental struct {
weightedTraverseGraph
}
func TestDijkstraAllPaths(t *testing.T) {
t.Parallel()
for _, test := range testgraphs.ShortestPathTests {
g := test.Graph()
for _, e := range test.Edges {
g.SetWeightedEdge(e)
}
var (
pt AllShortest
panicked bool
)
func() {
defer func() {
panicked = recover() != nil
}()
pt = DijkstraAllPaths(g.(graph.Graph))
}()
if panicked || test.HasNegativeWeight {
if !test.HasNegativeWeight {
t.Errorf("%q: unexpected panic", test.Name)
}
if !panicked {
t.Errorf("%q: expected panic for negative edge weight", test.Name)
}
continue
}
// Check all random paths returned are OK.
for i := 0; i < 10; i++ {
p, weight, unique := pt.Between(test.Query.From().ID(), test.Query.To().ID())
if weight != test.Weight {
t.Errorf("%q: unexpected weight from Between: got:%f want:%f",
test.Name, weight, test.Weight)
}
if weight := pt.Weight(test.Query.From().ID(), test.Query.To().ID()); weight != test.Weight {
t.Errorf("%q: unexpected weight from Weight: got:%f want:%f",
test.Name, weight, test.Weight)
}
if unique != test.HasUniquePath {
t.Errorf("%q: unexpected number of paths: got: unique=%t want: unique=%t",
test.Name, unique, test.HasUniquePath)
}
var got []int64
for _, n := range p {
got = append(got, n.ID())
}
ok := len(got) == 0 && len(test.WantPaths) == 0
for _, sp := range test.WantPaths {
if reflect.DeepEqual(got, sp) {
ok = true
break
}
}
if !ok {
t.Errorf("%q: unexpected shortest path:\ngot: %v\nwant from:%v",
test.Name, p, test.WantPaths)
}
}
np, weight, unique := pt.Between(test.NoPathFor.From().ID(), test.NoPathFor.To().ID())
if np != nil || !math.IsInf(weight, 1) || unique {
t.Errorf("%q: unexpected path:\ngot: path=%v weight=%f unique=%t\nwant:path=<nil> weight=+Inf unique=false",
test.Name, np, weight, unique)
}
paths, weight := pt.AllBetween(test.Query.From().ID(), test.Query.To().ID())
if weight != test.Weight {
t.Errorf("%q: unexpected weight from Between: got:%f want:%f",
test.Name, weight, test.Weight)
}
var got [][]int64
if len(paths) != 0 {
got = make([][]int64, len(paths))
}
for i, p := range paths {
for _, v := range p {
got[i] = append(got[i], v.ID())
}
}
ordered.BySliceValues(got)
if !reflect.DeepEqual(got, test.WantPaths) {
t.Errorf("testing %q: unexpected shortest paths:\ngot: %v\nwant:%v",
test.Name, got, test.WantPaths)
}
paths = paths[:0]
pt.AllBetweenFunc(test.Query.From().ID(), test.Query.To().ID(), func(path []graph.Node) {
paths = append(paths, append([]graph.Node(nil), path...))
})
got = nil
if len(paths) != 0 {
got = make([][]int64, len(paths))
}
for i, p := range paths {
for _, v := range p {
got[i] = append(got[i], v.ID())
}
}
ordered.BySliceValues(got)
if !reflect.DeepEqual(got, test.WantPaths) {
t.Errorf("testing %q: unexpected shortest paths:\ngot: %v\nwant:%v",
test.Name, got, test.WantPaths)
}
nps, weight := pt.AllBetween(test.NoPathFor.From().ID(), test.NoPathFor.To().ID())
if nps != nil || !math.IsInf(weight, 1) {
t.Errorf("%q: unexpected path:\ngot: paths=%v weight=%f\nwant:path=<nil> weight=+Inf",
test.Name, nps, weight)
}
}
}
func TestAllShortestAbsentNode(t *testing.T) {
t.Parallel()
g := simple.NewUndirectedGraph()
g.SetEdge(simple.Edge{F: simple.Node(1), T: simple.Node(2)})
paths := DijkstraAllPaths(g)
// Confirm we have a good paths tree.
if _, cost := paths.AllBetween(1, 2); cost != 1 {
t.Errorf("unexpected cost between existing nodes: got:%v want:1", cost)
}
gotPath, cost, unique := paths.Between(0, 0)
if cost != 0 {
t.Errorf("unexpected cost from absent node to itself: got:%v want:0", cost)
}
if !unique {
t.Error("unexpected non-unique path from absent node to itself")
}
wantPath := []graph.Node{node(0)}
if !reflect.DeepEqual(gotPath, wantPath) {
t.Errorf("unexpected path from absent node to itself: got:%#v want:%#v", gotPath, wantPath)
}
gotPaths, cost := paths.AllBetween(0, 0)
if cost != 0 {
t.Errorf("unexpected cost from absent node to itself: got:%v want:0", cost)
}
wantPaths := [][]graph.Node{{node(0)}}
if !reflect.DeepEqual(gotPaths, wantPaths) {
t.Errorf("unexpected paths from absent node to itself: got:%#v want:%#v", gotPaths, wantPaths)
}
}