Skip to content

Commit

Permalink
fix: dijkstra centrality return incorrect type.
Browse files Browse the repository at this point in the history
  • Loading branch information
yothgewalt committed Sep 1, 2024
1 parent db89be7 commit ea9c2d7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion centrality.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ func WithDijkstraStandard() options.DijkstraOptionFunc {
}

// DijkstraCentrality return slice of Vertex that has result of centrality.
func DijkstraCentrality[T comparable](g Grafik[T], opts ...options.DijkstraOptionFunc) []Vertex[T] {
func DijkstraCentrality[T comparable](g Grafik[T], opts ...options.DijkstraOptionFunc) []VertexPath[T] {
return DijkstraCentrality(g, opts...)
}
30 changes: 7 additions & 23 deletions centrality/dijkstra_centrality.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,18 @@ import (
"github.com/fitm-elite/grafik/pathfinder"
)

// VertexPath represents path of vertex
type VertexPath[T comparable] struct {
vertexLabel T
averageLength float64
}

// Label returns label of vertex path
func (v VertexPath[T]) Label() T {
return v.vertexLabel
}

// AverageLength returns average length of vertex path
func (v VertexPath[T]) AverageLength() float64 {
return v.averageLength
}

// DijkstraCentrality It's using a dijkstra method to find shortest path in each vertex
// and calculate to find an average value in each path to find a centroid.
//
// Returns []VertexPath[T]
func DijkstraCentrality[T comparable](g entity.Grafik[T], opts ...options.DijkstraOptionFunc) []VertexPath[T] {
func DijkstraCentrality[T comparable](g entity.Grafik[T], opts ...options.DijkstraOptionFunc) []grafik.VertexPath[T] {
vertices := g.GetAllVertices()
vertexPaths := make([]VertexPath[T], 0, len(vertices))
vertexPaths := make([]grafik.VertexPath[T], 0, len(vertices))

var mu sync.Mutex
var wg sync.WaitGroup

results := make(chan VertexPath[T], len(vertices))
results := make(chan grafik.VertexPath[T], len(vertices))

wg.Add(len(vertices))
for _, v := range vertices {
Expand All @@ -72,9 +56,9 @@ func DijkstraCentrality[T comparable](g entity.Grafik[T], opts ...options.Dijkst
}

averageLength := totalLength / float64(len(pathLengths))
result := VertexPath[T]{
vertexLabel: label,
averageLength: averageLength,
result := grafik.VertexPath[T]{
VertexLabel: label,
AverageLength: averageLength,
}

results <- result
Expand All @@ -93,7 +77,7 @@ func DijkstraCentrality[T comparable](g entity.Grafik[T], opts ...options.Dijkst
}

sort.Slice(vertexPaths, func(i, j int) bool {
return vertexPaths[i].averageLength < vertexPaths[j].averageLength
return vertexPaths[i].AverageLength < vertexPaths[j].AverageLength
})

return vertexPaths
Expand Down
8 changes: 4 additions & 4 deletions centrality/dijkstra_centrality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ func TestDijkstraCentrality(t *testing.T) {
t.Errorf("Expected len from paths is %d, got %d", 7, len(paths))
}

if paths[0].Label() == vE.Label() && paths[0].AverageLength() == 2.14 {
t.Errorf("Expected %s (%.2f), got %s (%.2f)", vE.Label(), 2.14, paths[0].Label(), paths[0].AverageLength())
if paths[0].GetLabel() == vE.Label() && paths[0].GetAverageLength() == 2.14 {
t.Errorf("Expected %s (%.2f), got %s (%.2f)", vE.Label(), 2.14, paths[0].GetLabel(), paths[0].GetAverageLength())
}

if paths[1].Label() == vB.Label() && paths[1].AverageLength() == 2.14 {
t.Errorf("Expected %s (%.2f), got %s (%.2f)", vB.Label(), 2.14, paths[0].Label(), paths[0].AverageLength())
if paths[1].GetLabel() == vB.Label() && paths[1].GetAverageLength() == 2.14 {
t.Errorf("Expected %s (%.2f), got %s (%.2f)", vB.Label(), 2.14, paths[0].GetLabel(), paths[0].GetAverageLength())
}
}
16 changes: 16 additions & 0 deletions vertex.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ package grafik

import "github.com/fitm-elite/grafik/options"

// VertexPath represents path of vertex
type VertexPath[T comparable] struct {
VertexLabel T
AverageLength float64
}

// Label returns label of vertex path
func (v VertexPath[T]) GetLabel() T {
return v.VertexLabel
}

// AverageLength returns average length of vertex path
func (v VertexPath[T]) GetAverageLength() float64 {
return v.AverageLength
}

// Vertex represents a node or point in a graph
type Vertex[T comparable] struct {
label T
Expand Down

0 comments on commit ea9c2d7

Please sign in to comment.