/
analysis.go
249 lines (224 loc) · 6.53 KB
/
analysis.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright ©2017 Dan Kortschak. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package graphprac provides helper routines for short practical in
// graph analysis.
package graphprac
import (
"fmt"
"sort"
"strconv"
"strings"
"gonum.org/v1/gonum/graph"
"gonum.org/v1/gonum/graph/community"
"gonum.org/v1/gonum/graph/encoding"
"gonum.org/v1/gonum/graph/network"
"gonum.org/v1/gonum/graph/path"
"gonum.org/v1/gonum/graph/topo"
)
// PageRank performs a PageRank analysis on g using the provided damping
// and tolerance parameters.
//
// The PageRank value is written into the "rank" attribute of each node.
func PageRank(g *Graph, damp, tol float64) {
rank := network.PageRank(directed{g}, damp, tol)
nodes := g.NodeMap()
for id, w := range rank {
nodes[id].SetAttribute(encoding.Attribute{"rank", fmt.Sprint(w)})
}
}
type directed struct {
*Graph
}
func (g directed) HasEdgeFromTo(uid, vid int64) bool { return g.HasEdgeBetween(uid, vid) }
func (g directed) To(id int64) graph.Nodes { return g.From(id) }
// Closeness performs a closeness centrality analysis on g.
//
// The closeness centrality value is written into the "closeness" attribute of each node.
func Closeness(g *Graph) {
p := path.DijkstraAllPaths(g)
rank := network.Closeness(g, p)
nodes := g.NodeMap()
for id, w := range rank {
nodes[id].SetAttribute(encoding.Attribute{"closeness", fmt.Sprint(w)})
}
}
// Farness performs a farness centrality analysis on g.
//
// The farness centrality value is written into the "farness" attribute of each node.
func Farness(g *Graph) {
p := path.DijkstraAllPaths(g)
rank := network.Farness(g, p)
nodes := g.NodeMap()
for id, w := range rank {
nodes[id].SetAttribute(encoding.Attribute{"farness", fmt.Sprint(w)})
}
}
// Betweenness performs a betweenness centrality analysis on g.
//
// The betweenness centrality value is written into the "betweenness" attribute of each node.
func Betweenness(g *Graph) {
rank := network.Betweenness(g)
nodes := g.NodeMap()
// network.Betweenness does not retain zero
// betweenness values, so fill them in.
for _, n := range nodes {
if _, ok := rank[n.ID()]; !ok {
rank[n.ID()] = 0
}
}
for id, w := range rank {
nodes[id].SetAttribute(encoding.Attribute{"betweenness", fmt.Sprint(w)})
}
}
// EdgeBetweenness performs an edge betweenness centrality analysis on g.
//
// The edge betweenness centrality value is written into the "edge_betweenness" attribute of each edge.
func EdgeBetweenness(g *Graph) {
rank := network.EdgeBetweenness(g)
for ids, w := range rank {
e := g.EdgeBetween(ids[0], ids[1])
e.(*Edge).SetAttribute(encoding.Attribute{"edge_betweenness", fmt.Sprint(w)})
}
}
// Communities performs a community modularisation of the graph g at the
// specified resolution.
//
// The community identity value is written into the "community" attribute of each node.
func Communities(g *Graph, resolution float64) {
r := community.Modularize(g, resolution, nil)
nodes := g.NodeMap()
for i, c := range r.Communities() {
for _, n := range c {
nodes[n.ID()].SetAttribute(encoding.Attribute{"community", fmt.Sprint(i)})
}
}
}
// Clique performs a maximal clique analysis on g where cliques must be
// k-cliques or larger.
//
// The clique membership values are written as a comma-separated list into the
// "clique" attribute of each node and the number of cliques a node is a member of
// is written into "clique_count".
func Clique(g *Graph, k int) {
mc := topo.BronKerbosch(g)
var ck int
for _, c := range mc {
if len(c) >= k {
ck++
}
}
nodes := g.NodeMap()
var i int
for _, c := range mc {
if len(c) < k {
continue
}
for _, n := range c {
found := false
attrs := nodes[n.ID()].Attributes
if attrs.Get("clique_count") == "" {
for j, a := range attrs {
if a.Key == "clique" {
attrs[j] = encoding.Attribute{"clique", fmt.Sprintf("%s,%d", a.Value, i)}
found = true
break
}
}
}
if !found {
nodes[n.ID()].SetAttribute(encoding.Attribute{"clique", fmt.Sprint(i)})
nodes[n.ID()].SetAttribute(encoding.Attribute{"clique_count", ""})
}
}
i++
}
for _, n := range nodes {
for _, a := range n.Attributes {
if a.Key == "clique" {
n.SetAttribute(encoding.Attribute{"clique_count", fmt.Sprint(len(strings.Split(a.Value, ",")))})
break
}
}
}
}
// NodesOf returns the nodes in g asserted to be type graphprac.Node so
// that attributes can be accessed.
//
// nodes := graphprac.NodesOf(g)
// fmt.Println(nodes[i].Attributes.Get(attr))
func NodesOf(g *Graph) []*Node {
var nodes []*Node
for _, n := range graph.NodesOf(g.Nodes()) {
nodes = append(nodes, n.(*Node))
}
return nodes
}
// NodesByAttribute return a slice of nodes sorted descending by the
// given attribute. Only float64 attributes are handled by this function.
func NodesByAttribute(attr string, g *Graph) ([]*Node, error) {
var nodes []*Node
var vals []float64
for _, n := range graph.NodesOf(g.Nodes()) {
n := n.(*Node)
var v float64
var err error
for _, a := range n.Attributes {
if a.Key == attr {
v, err = strconv.ParseFloat(a.Value, 64)
if err != nil {
return nil, err
}
break
}
}
nodes = append(nodes, n)
vals = append(vals, v)
}
sort.Sort(nodesByAttr{vals: vals, nodes: nodes})
return nodes, nil
}
type nodesByAttr struct {
nodes []*Node
vals []float64
}
func (a nodesByAttr) Len() int { return len(a.nodes) }
func (a nodesByAttr) Less(i, j int) bool { return a.vals[i] > a.vals[j] }
func (a nodesByAttr) Swap(i, j int) {
a.nodes[i], a.nodes[j] = a.nodes[j], a.nodes[i]
a.vals[i], a.vals[j] = a.vals[j], a.vals[i]
}
// EdgesByAttribute return a slice of edges sorted descending by the
// given attribute.
func EdgesByAttribute(attr string, g *Graph) ([]*Edge, error) {
var edges []*Edge
var vals []float64
for _, n := range graph.EdgesOf(g.Edges()) {
n := n.(*Edge)
var v float64
var err error
for _, a := range n.Attributes {
if a.Key == attr {
v, err = strconv.ParseFloat(a.Value, 64)
if err != nil {
return nil, err
}
break
}
}
edges = append(edges, n)
vals = append(vals, v)
}
sort.Sort(edgesByAttr{vals: vals, edges: edges})
return edges, nil
}
type edgesByAttr struct {
edges []*Edge
vals []float64
}
func (a edgesByAttr) Len() int { return len(a.edges) }
func (a edgesByAttr) Less(i, j int) bool { return a.vals[i] > a.vals[j] }
func (a edgesByAttr) Swap(i, j int) {
a.edges[i], a.edges[j] = a.edges[j], a.edges[i]
a.vals[i], a.vals[j] = a.vals[j], a.vals[i]
}