func TestBetweennessWeighted(t *testing.T) { for i, test := range betweennessTests { g := concrete.NewGraph(0, math.Inf(1)) for u, e := range test.g { // Add nodes that are not defined by an edge. if !g.Has(concrete.Node(u)) { g.AddNode(concrete.Node(u)) } for v := range e { g.SetEdge(concrete.Edge{F: concrete.Node(u), T: concrete.Node(v), W: 1}) } } p, ok := path.FloydWarshall(g) if !ok { t.Errorf("unexpected negative cycle in test %d", i) continue } got := BetweennessWeighted(g, p) prec := 1 - int(math.Log10(test.wantTol)) for n := range test.g { gotN, gotOK := got[n] wantN, wantOK := test.want[n] if gotOK != wantOK { t.Errorf("unexpected betweenness existence for test %d, node %d", i, n) } if !floats.EqualWithinAbsOrRel(gotN, wantN, test.wantTol, test.wantTol) { t.Errorf("unexpected betweenness result for test %d:\ngot: %v\nwant:%v", i, orderedFloats(got, prec), orderedFloats(test.want, prec)) break } } } }
func TestEdgeBetweennessWeighted(t *testing.T) { for i, test := range betweennessTests { g := simple.NewUndirectedGraph(0, math.Inf(1)) for u, e := range test.g { // Add nodes that are not defined by an edge. if !g.Has(simple.Node(u)) { g.AddNode(simple.Node(u)) } for v := range e { g.SetEdge(simple.Edge{F: simple.Node(u), T: simple.Node(v), W: 1}) } } p, ok := path.FloydWarshall(g) if !ok { t.Errorf("unexpected negative cycle in test %d", i) continue } got := EdgeBetweennessWeighted(g, p) prec := 1 - int(math.Log10(test.wantTol)) outer: for u := range test.g { for v := range test.g { wantQ, gotOK := got[[2]int{u, v}] gotQ, wantOK := test.wantEdges[[2]int{u, v}] if gotOK != wantOK { t.Errorf("unexpected betweenness result for test %d, edge (%c,%c)", i, u+'A', v+'A') } if !floats.EqualWithinAbsOrRel(gotQ, wantQ, test.wantTol, test.wantTol) { t.Errorf("unexpected betweenness result for test %d:\ngot: %v\nwant:%v", i, orderedPairFloats(got, prec), orderedPairFloats(test.wantEdges, prec)) break outer } } } } }
func TestDistanceCentralityDirected(t *testing.T) { const tol = 1e-12 prec := 1 - int(math.Log10(tol)) for i, test := range directedCentralityTests { g := concrete.NewDirectedGraph() for u, e := range test.g { // Add nodes that are not defined by an edge. if !g.Has(concrete.Node(u)) { g.AddNode(concrete.Node(u)) } for v := range e { g.SetEdge(concrete.Edge{F: concrete.Node(u), T: concrete.Node(v)}, 1) } } p, ok := path.FloydWarshall(g) if !ok { t.Errorf("unexpected negative cycle in test %d", i) continue } var got map[int]float64 got = Closeness(g, p) for n := range test.g { if !floats.EqualWithinAbsOrRel(got[n], 1/test.farness[n], tol, tol) { want := make(map[int]float64) for n, v := range test.farness { want[n] = 1 / v } t.Errorf("unexpected closeness centrality for test %d:\ngot: %v\nwant:%v", i, orderedFloats(got, prec), orderedFloats(want, prec)) break } } got = Farness(g, p) for n := range test.g { if !floats.EqualWithinAbsOrRel(got[n], test.farness[n], tol, tol) { t.Errorf("unexpected farness for test %d:\ngot: %v\nwant:%v", i, orderedFloats(got, prec), orderedFloats(test.farness, prec)) break } } got = Harmonic(g, p) for n := range test.g { if !floats.EqualWithinAbsOrRel(got[n], test.harmonic[n], tol, tol) { t.Errorf("unexpected harmonic centrality for test %d:\ngot: %v\nwant:%v", i, orderedFloats(got, prec), orderedFloats(test.harmonic, prec)) break } } got = Residual(g, p) for n := range test.g { if !floats.EqualWithinAbsOrRel(got[n], test.residual[n], tol, tol) { t.Errorf("unexpected residual closeness for test %d:\ngot: %v\nwant:%v", i, orderedFloats(got, prec), orderedFloats(test.residual, prec)) break } } } }
func TestFloydWarshall(t *testing.T) { for _, test := range shortestPathTests { g := test.g() for _, e := range test.edges { g.SetEdge(e, e.Cost) } pt, ok := path.FloydWarshall(g.(graph.Graph)) if test.hasNegativeCycle { if ok { t.Errorf("%q: expected negative cycle", test.name) } continue } if !ok { t.Fatalf("%q: unexpected negative cycle", test.name) } // Check all random paths returned are OK. for i := 0; i < 10; i++ { p, weight, unique := pt.Between(test.query.From(), test.query.To()) 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(), test.query.To()); weight != test.weight { t.Errorf("%q: unexpected weight from Weight: got:%f want:%f", test.name, weight, test.weight) } if unique != test.unique { t.Errorf("%q: unexpected number of paths: got: unique=%t want: unique=%t", test.name, unique, test.unique) } var got []int for _, n := range p { got = append(got, n.ID()) } ok := len(got) == 0 && len(test.want) == 0 for _, sp := range test.want { 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.want) } } np, weight, unique := pt.Between(test.none.From(), test.none.To()) if np != nil || !math.IsInf(weight, 1) || unique != false { 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(), test.query.To()) if weight != test.weight { t.Errorf("%q: unexpected weight from Between: got:%f want:%f", test.name, weight, test.weight) } var got [][]int if len(paths) != 0 { got = make([][]int, len(paths)) } for i, p := range paths { for _, v := range p { got[i] = append(got[i], v.ID()) } } sort.Sort(internal.BySliceValues(got)) if !reflect.DeepEqual(got, test.want) { t.Errorf("testing %q: unexpected shortest paths:\ngot: %v\nwant:%v", test.name, got, test.want) } nps, weight := pt.AllBetween(test.none.From(), test.none.To()) 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) } } }