Skip to content

Commit b8e2564

Browse files
committed
fix(leiden): handle directed graphs in CPM quality gain calculation
diffCPM was reading neighborEdgeWeightToCommunity which is only populated in the undirected path of accumulateNeighborCommunityEdgeWeights. For directed graphs, use outEdgeWeightToCommunity + inEdgeWeightFromCommunity to correctly account for edge weights in both directions. Impact: 1 functions changed, 4 affected
1 parent e89371b commit b8e2564

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

  • src/graph/algorithms/leiden

src/graph/algorithms/leiden/cpm.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,19 @@
66
export function diffCPM(part, g, v, c, gamma = 1.0) {
77
const oldC = part.nodeCommunity[v];
88
if (c === oldC) return 0;
9-
const w_old = part.getNeighborEdgeWeightToCommunity(oldC) || 0;
10-
const w_new = c < g.n ? part.getNeighborEdgeWeightToCommunity(c) || 0 : 0;
9+
let w_old, w_new;
10+
if (g.directed) {
11+
w_old =
12+
(part.getOutEdgeWeightToCommunity(oldC) || 0) +
13+
(part.getInEdgeWeightFromCommunity(oldC) || 0);
14+
w_new =
15+
c < g.n
16+
? (part.getOutEdgeWeightToCommunity(c) || 0) + (part.getInEdgeWeightFromCommunity(c) || 0)
17+
: 0;
18+
} else {
19+
w_old = part.getNeighborEdgeWeightToCommunity(oldC) || 0;
20+
w_new = c < g.n ? part.getNeighborEdgeWeightToCommunity(c) || 0 : 0;
21+
}
1122
const s_v = g.size[v] || 1;
1223
const S_old = part.communityTotalSize[oldC] || 0;
1324
const S_new = c < part.communityTotalSize.length ? part.communityTotalSize[c] : 0;

0 commit comments

Comments
 (0)