Skip to content

Commit

Permalink
changed data type to uint32
Browse files Browse the repository at this point in the history
  • Loading branch information
alixaxel committed Mar 6, 2016
1 parent 16a01e9 commit 14bfb4c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func main() {
graph.Link(2, 4, 4.0)
graph.Link(3, 1, 5.0)

graph.Rank(0.85, 0.000001, func(node int32, rank float64) {
graph.Rank(0.85, 0.000001, func(node uint32, rank float64) {
fmt.Println("Node", node, "has a rank of", rank)
})
}
Expand Down
20 changes: 10 additions & 10 deletions pagerank.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ type node struct {

// Graph holds node and edge data.
type Graph struct {
edges map[int32](map[int32]float64)
nodes map[int32]*node
edges map[uint32](map[uint32]float64)
nodes map[uint32]*node
}

// NewGraph initializes and returns a new graph.
func NewGraph() *Graph {
return &Graph{
edges: make(map[int32](map[int32]float64)),
nodes: make(map[int32]*node),
edges: make(map[uint32](map[uint32]float64)),
nodes: make(map[uint32]*node),
}
}

// Link creates a weighted edge between a source-target node pair.
// If the edge already exists, the weight is incremented.
func (self *Graph) Link(source, target int32, weight float64) {
func (self *Graph) Link(source, target uint32, weight float64) {
if _, ok := self.nodes[source]; ok == false {
self.nodes[source] = &node{
weight: 0,
Expand All @@ -46,7 +46,7 @@ func (self *Graph) Link(source, target int32, weight float64) {
}

if _, ok := self.edges[source]; ok == false {
self.edges[source] = map[int32]float64{}
self.edges[source] = map[uint32]float64{}
}

self.edges[source][target] += weight
Expand All @@ -57,7 +57,7 @@ func (self *Graph) Link(source, target int32, weight float64) {
// ε (epsilon) is the convergence criteria, usually set to a tiny value.
//
// This method will run as many iterations as needed, until the graph converges.
func (self *Graph) Rank(α, ε float64, callback func(id int32, rank float64)) {
func (self *Graph) Rank(α, ε float64, callback func(id uint32, rank float64)) {
Δ := float64(1.0)
inverse := 1 / float64(len(self.nodes))

Expand All @@ -76,7 +76,7 @@ func (self *Graph) Rank(α, ε float64, callback func(id int32, rank float64)) {

for Δ > ε {
leak := float64(0)
nodes := map[int32]float64{}
nodes := map[uint32]float64{}

for key, value := range self.nodes {
nodes[key] = value.weight
Expand Down Expand Up @@ -112,6 +112,6 @@ func (self *Graph) Rank(α, ε float64, callback func(id int32, rank float64)) {

// Reset clears all the current graph data.
func (self *Graph) Reset() {
self.edges = make(map[int32](map[int32]float64))
self.nodes = make(map[int32]*node)
self.edges = make(map[uint32](map[uint32]float64))
self.nodes = make(map[uint32]*node)
}
30 changes: 15 additions & 15 deletions pagerank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
func TestEmpty(t *testing.T) {
graph := NewGraph()

actual := map[int32]float64{}
expected := map[int32]float64{}
actual := map[uint32]float64{}
expected := map[uint32]float64{}

graph.Rank(0.85, 0.000001, func(node int32, rank float64) {
graph.Rank(0.85, 0.000001, func(node uint32, rank float64) {
actual[node] = rank
})

Expand All @@ -29,15 +29,15 @@ func TestSimple(t *testing.T) {
graph.Link(2, 4, 1.0)
graph.Link(3, 1, 1.0)

actual := map[int32]float64{}
expected := map[int32]float64{
actual := map[uint32]float64{}
expected := map[uint32]float64{
1: 0.32721836185043207,
2: 0.2108699481253495,
3: 0.3004897566512289,
4: 0.16142193337298952,
}

graph.Rank(0.85, 0.000001, func(node int32, rank float64) {
graph.Rank(0.85, 0.000001, func(node uint32, rank float64) {
actual[node] = rank
})

Expand All @@ -55,15 +55,15 @@ func TestWeighted(t *testing.T) {
graph.Link(2, 4, 4.0)
graph.Link(3, 1, 5.0)

actual := map[int32]float64{}
expected := map[int32]float64{
actual := map[uint32]float64{}
expected := map[uint32]float64{
1: 0.34983779905464363,
2: 0.1688733284604475,
3: 0.3295121849483849,
4: 0.15177668753652385,
}

graph.Rank(0.85, 0.000001, func(node int32, rank float64) {
graph.Rank(0.85, 0.000001, func(node uint32, rank float64) {
actual[node] = rank
})

Expand All @@ -84,15 +84,15 @@ func TestDuplicates(t *testing.T) {
graph.Link(1, 2, 6.0)
graph.Link(1, 3, 7.0)

actual := map[int32]float64{}
expected := map[int32]float64{
actual := map[uint32]float64{}
expected := map[uint32]float64{
1: 0.3312334209098247,
2: 0.19655848316544225,
3: 0.3033555769882879,
4: 0.168852518936445,
}

graph.Rank(0.85, 0.000001, func(node int32, rank float64) {
graph.Rank(0.85, 0.000001, func(node uint32, rank float64) {
actual[node] = rank
})

Expand All @@ -115,14 +115,14 @@ func TestDuplicatesAfterReset(t *testing.T) {
graph.Link(1, 2, 6.0)
graph.Link(1, 3, 7.0)

actual := map[int32]float64{}
expected := map[int32]float64{
actual := map[uint32]float64{}
expected := map[uint32]float64{
1: 0.25974019022001016,
2: 0.3616383883769191,
3: 0.3786214214030706,
}

graph.Rank(0.85, 0.000001, func(node int32, rank float64) {
graph.Rank(0.85, 0.000001, func(node uint32, rank float64) {
actual[node] = rank
})

Expand Down

0 comments on commit 14bfb4c

Please sign in to comment.