File: hits_test.go

package info (click to toggle)
golang-gonum-v1-gonum 0.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,792 kB
  • sloc: asm: 6,252; fortran: 5,271; sh: 377; ruby: 211; makefile: 98
file content (93 lines) | stat: -rw-r--r-- 2,324 bytes parent folder | download
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 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 network

import (
	"cmp"
	"fmt"
	"math"
	"slices"
	"testing"

	"gonum.org/v1/gonum/floats/scalar"
	"gonum.org/v1/gonum/graph/simple"
)

var hitsTests = []struct {
	g   []set
	tol float64

	wantTol float64
	want    map[int64]HubAuthority
}{
	{
		// Example graph from http://www.cis.hut.fi/Opinnot/T-61.6020/2008/pagerank_hits.pdf page 8.
		g: []set{
			A: linksTo(B, C, D),
			B: linksTo(C, D),
			C: linksTo(B),
			D: nil,
		},
		tol: 1e-4,

		wantTol: 1e-4,
		want: map[int64]HubAuthority{
			A: {Hub: 0.7887, Authority: 0},
			B: {Hub: 0.5774, Authority: 0.4597},
			C: {Hub: 0.2113, Authority: 0.6280},
			D: {Hub: 0, Authority: 0.6280},
		},
	},
}

func TestHITS(t *testing.T) {
	for i, test := range hitsTests {
		g := simple.NewDirectedGraph()
		for u, e := range test.g {
			// Add nodes that are not defined by an edge.
			if g.Node(int64(u)) == nil {
				g.AddNode(simple.Node(u))
			}
			for v := range e {
				g.SetEdge(simple.Edge{F: simple.Node(u), T: simple.Node(v)})
			}
		}
		got := HITS(g, test.tol)
		prec := 1 - int(math.Log10(test.wantTol))
		for n := range test.g {
			if !scalar.EqualWithinAbsOrRel(got[int64(n)].Hub, test.want[int64(n)].Hub, test.wantTol, test.wantTol) {
				t.Errorf("unexpected HITS result for test %d:\ngot: %v\nwant:%v",
					i, orderedHubAuth(got, prec), orderedHubAuth(test.want, prec))
				break
			}
			if !scalar.EqualWithinAbsOrRel(got[int64(n)].Authority, test.want[int64(n)].Authority, test.wantTol, test.wantTol) {
				t.Errorf("unexpected HITS result for test %d:\ngot: %v\nwant:%v",
					i, orderedHubAuth(got, prec), orderedHubAuth(test.want, prec))
				break
			}
		}
	}
}

func orderedHubAuth(w map[int64]HubAuthority, prec int) []keyHubAuthVal {
	o := make([]keyHubAuthVal, 0, len(w))
	for k, v := range w {
		o = append(o, keyHubAuthVal{prec: prec, key: k, val: v})
	}
	slices.SortFunc(o, func(a, b keyHubAuthVal) int { return cmp.Compare(a.key, b.key) })
	return o
}

type keyHubAuthVal struct {
	prec int
	key  int64
	val  HubAuthority
}

func (kv keyHubAuthVal) String() string {
	return fmt.Sprintf("%d:{H:%.*f, A:%.*f}",
		kv.key, kv.prec, kv.val.Hub, kv.prec, kv.val.Authority,
	)
}