alixaxel/pagerank

Non deterministic result for same input

zeslava opened this issue · 0 comments

In example below order of ranks is depends on order of internal map(map of edges).
But for same input i expect deterministic output.
How do i reach this?

package main

import (
	"fmt"

	"github.com/alixaxel/pagerank"
)

const (
	damping   = 0.85
	tolerance = 0.0001
)

type Rank struct {
	idx   int
	score float64
}

func main() {
	g := pagerank.NewGraph()

	g.Link(0, 1, 0.33333)
	g.Link(1, 2, 0.33333)
	g.Link(2, 0, 0.33333)

	var ranks []int
	g.Rank(damping, tolerance, func(sentenceIndex uint32, rank float64) {
		ranks = append(ranks, int(sentenceIndex))
	})

	var ranks2 []int
	g.Rank(damping, tolerance, func(sentenceIndex uint32, rank float64) {
		ranks2 = append(ranks2, int(sentenceIndex))
	})

	fmt.Printf("%v\n", eq(ranks, ranks2)) // ! sometimes false !
}

func eq(l, r []int) bool {
	if len(l) != len(r) {
		return false
	}

	for i := range l {
		if l[i] != r[i] {
			return false
		}
	}

	return true
}