-
Notifications
You must be signed in to change notification settings - Fork 1
/
centrality.go
69 lines (51 loc) · 1.21 KB
/
centrality.go
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
package giraffe
import (
"math"
)
func (g *Graph) GetDegree() []int {
if len(g.Vertices) == 0 {
return nil
}
res := make([]int, len(g.Vertices))
for _, v := range g.Vertices {
res[v.Index] = g.GetVertexDegree(v)
}
return res
}
func (g *Graph) GetVertexDegree(v *Vertex) int {
count := 0
for _, e := range g.Edges {
if e.End.Index == v.Index {
count++
}
}
return count
}
func (g *Graph) GetVertexBetweenness(v *Vertex) float64 {
vShortestRoute := 0
for i, vtx := range g.Vertices {
// Skip the current vertex
if v == vtx { continue }
for j := i; j < len(g.Vertices); j++ {
// Skip the current vertex
if v == g.Vertices[j] { continue }
_, visitedVertices := g.FindVertexBFS(vtx.Index, g.Vertices[j].Index)
for _, visitedVtx := range visitedVertices {
// Check if it was involved in the shortest path
if v == visitedVtx {
vShortestRoute++
break
}
}
}
}
betweeness := math.Round(float64(vShortestRoute) / float64(len(g.Vertices)))
return betweeness
}
func (g *Graph) GetBetweenness() []float64 {
betweenness := make([]float64, len(g.Vertices))
for i, vtx := range g.Vertices {
betweenness[i] = g.GetVertexBetweenness(vtx)
}
return betweenness
}