-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodularity.go
More file actions
313 lines (282 loc) · 7.09 KB
/
modularity.go
File metadata and controls
313 lines (282 loc) · 7.09 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// package algorithm
package algorithm
import (
"container/heap"
"math"
"github.com/elecbug/netkit/graph"
"github.com/elecbug/netkit/graph/node"
)
// Modularity computes Newman-Girvan modularity Q.
// If cfg.Modularity.Partition == nil, it runs greedy CNM to obtain a partition first.
// All computations are performed on an undirected projection (to match NetworkX).
func Modularity(g *graph.Graph, cfg *Config) float64 {
if g == nil || cfg == nil || cfg.Modularity == nil {
return 0.0
}
if cfg.Modularity.Partition == nil {
cfg.Modularity.Partition = GreedyModularityCommunitiesNX(g)
}
part := cfg.Modularity.Partition
if len(part) == 0 {
return 0.0
}
return modularityQNX(g, part)
}
// GreedyModularityCommunitiesNX implements the Clauset–Newman–Moore greedy
// modularity maximization, aligned with NetworkX conventions:
// - work on an undirected projection
// - m = number_of_undirected_edges
// - inv2m = 1/(2m); each undirected edge contributes inv2m twice (symmetrically)
// Returns a partition map: nodeID -> compact community label.
func GreedyModularityCommunitiesNX(g *graph.Graph) map[node.ID]int {
ids := g.Nodes()
n := len(ids)
if n == 0 {
return nil
}
// Build stable indices
idxOf := make(map[node.ID]int, n)
for i, u := range ids {
idxOf[u] = i
}
// Build undirected edge set and degrees for the projection
edges, deg, m := undirectedEdgesAndDegrees(g, ids, idxOf)
if m == 0 {
part := make(map[node.ID]int, n)
for i, u := range ids {
part[u] = i
}
return part
}
inv2m := 1.0 / (2.0 * float64(m))
// Initialize communities: each node is its own community
commOf := make([]int, n)
members := make(map[int][]int, n)
active := make(map[int]bool, n)
version := make(map[int]int, n)
for i := 0; i < n; i++ {
commOf[i] = i
members[i] = []int{i}
active[i] = true
version[i] = 0
}
// e[c][d]: fraction of edges between communities c and d (over 2m), symmetric
// a[c] : sum_j e[c][j]
e := make(map[int]map[int]float64, n)
a := make(map[int]float64, n)
for c := range active {
a[c] = 0
}
// Initialize a from node degrees: a_c += k_i / (2m) for i in c (singleton now)
for i := 0; i < n; i++ {
a[commOf[i]] += float64(deg[i]) * inv2m
}
// Initialize e from undirected edges (each contributes inv2m to both e[c][d] and e[d][c])
for _, pr := range edges {
i, j := pr[0], pr[1] // i<j ensured
ci, cj := commOf[i], commOf[j]
if ci == cj {
// Initially impossible; keep for completeness
continue
}
w := inv2m
if e[ci] == nil {
e[ci] = make(map[int]float64)
}
if e[cj] == nil {
e[cj] = make(map[int]float64)
}
e[ci][cj] += w
e[cj][ci] += w
}
// Priority queue of merges by ΔQ = 2*(e_cd - a_c * a_d), largest first
pq := &candidatePQ{}
heap.Init(pq)
// Seed
for c, nbrs := range e {
for d, w := range nbrs {
if c >= d {
continue
}
delta := 2.0*(w) - 2.0*(a[c]*a[d])
heap.Push(pq, mergeCandidate{c: c, d: d, deltaQ: delta, verC: version[c], verD: version[d]})
}
}
// Greedy loop
for pq.Len() > 0 {
top := heap.Pop(pq).(mergeCandidate)
c, d := top.c, top.d
if !active[c] || !active[d] || top.verC != version[c] || top.verD != version[d] {
continue
}
// Recompute current ΔQ
curW := 0.0
if e[c] != nil {
curW = e[c][d]
}
curDelta := 2.0*(curW) - 2.0*(a[c]*a[d])
if curDelta <= 1e-12 {
// No further positive gain
break
}
// Merge d into c
// 1) membership
members[c] = append(members[c], members[d]...)
delete(members, d)
for _, idx := range members[c] {
commOf[idx] = c
}
// 2) update e and a
neighborsD := e[d]
if e[c] == nil {
e[c] = make(map[int]float64)
}
for x, w := range neighborsD {
if x == c {
continue
}
e[c][x] += w
if e[x] == nil {
e[x] = make(map[int]float64)
}
e[x][c] += w
delete(e[x], d)
}
delete(e[c], d)
delete(e, d)
a[c] += a[d]
delete(a, d)
// 3) deactivate d, bump versions
active[d] = false
version[c]++
version[d]++
// 4) push new candidates (c,x)
for x := range e[c] {
if !active[x] || x == c {
continue
}
delta := 2.0*(e[c][x]) - 2.0*(a[c]*a[x])
cc, dd := minInt(c, x), maxInt(c, x)
heap.Push(pq, mergeCandidate{c: cc, d: dd, deltaQ: delta, verC: version[cc], verD: version[dd]})
}
}
// Compact community labels
labelOf := make(map[int]int, len(members))
next := 0
for commID := range members {
labelOf[commID] = next
next++
}
part := make(map[node.ID]int, n)
for oldComm, label := range labelOf {
for _, idx := range members[oldComm] {
part[ids[idx]] = label
}
}
return part
}
// modularityQNX evaluates Q with the NetworkX-compatible undirected definition:
//
// m = number_of_undirected_edges
// inv2m = 1/(2m)
// Q = (1/2m) * sum_{(i,j) in undirected edges, c(i)=c(j)} [ 1 - (k_i k_j)/(2m) ]
func modularityQNX(g *graph.Graph, partition map[node.ID]int) float64 {
ids := g.Nodes()
n := len(ids)
if n == 0 {
return 0.0
}
idxOf := make(map[node.ID]int, n)
for i, u := range ids {
idxOf[u] = i
}
edges, deg, m := undirectedEdgesAndDegrees(g, ids, idxOf)
if m == 0 {
return 0.0
}
inv2m := 1.0 / (2.0 * float64(m))
var sum float64
for _, pr := range edges {
i, j := pr[0], pr[1] // i<j
ui, uj := ids[i], ids[j]
if partition[ui] != partition[uj] {
continue
}
ki := float64(deg[i])
kj := float64(deg[j])
sum += 1.0 - (ki*kj)*inv2m
}
Q := sum * inv2m
if math.IsNaN(Q) || math.IsInf(Q, 0) {
return 0.0
}
return Q
}
// undirectedEdgesAndDegrees builds the undirected projection (unique i<j pairs),
// returns: list of edges as pairs of indices, degree per node in the projection, and m (=|E|).
// An undirected edge contributes exactly once as (i<j).
func undirectedEdgesAndDegrees(g *graph.Graph, ids []node.ID, idxOf map[node.ID]int) ([][2]int, []int, int) {
n := len(ids)
seen := make(map[int]map[int]bool, n)
edges := make([][2]int, 0)
deg := make([]int, n)
for i := 0; i < n; i++ {
u := ids[i]
for _, v := range g.Neighbors(u) {
j := idxOf[v]
if i == j {
continue
}
a, b := i, j
if a > b {
a, b = b, a
}
if seen[a] == nil {
seen[a] = make(map[int]bool)
}
if seen[a][b] {
continue
}
seen[a][b] = true
edges = append(edges, [2]int{a, b})
// increase degree in projection
deg[a]++
deg[b]++
}
}
m := len(edges)
return edges, deg, m
}
//
// ---------- Helpers & PQ ----------
//
type mergeCandidate struct {
c, d int // community IDs (ensure c<d when pushing to PQ)
deltaQ float64 // modularity gain if merged
verC int // version stamps for staleness check
verD int
}
type candidatePQ []mergeCandidate
func (pq candidatePQ) Len() int { return len(pq) }
func (pq candidatePQ) Less(i, j int) bool { return pq[i].deltaQ > pq[j].deltaQ } // max-heap
func (pq candidatePQ) Swap(i, j int) { pq[i], pq[j] = pq[j], pq[i] }
func (pq *candidatePQ) Push(x interface{}) { *pq = append(*pq, x.(mergeCandidate)) }
func (pq *candidatePQ) Pop() interface{} {
old := *pq
n := len(old)
item := old[n-1]
*pq = old[:n-1]
return item
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}
func maxInt(a, b int) int {
if a > b {
return a
}
return b
}