-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhcgc_module.cpp
More file actions
1276 lines (1136 loc) · 53.9 KB
/
Copy pathhcgc_module.cpp
File metadata and controls
1276 lines (1136 loc) · 53.9 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <limits>
#include <numeric>
#include <unordered_map>
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <random>
#include <unordered_set>
#include <vector>
namespace py = pybind11;
// Coarsened graph data produced by CSRGraph::build_coarsened_data()
struct CoarsenedData {
int num_nodes_new = 0;
std::vector<int> old_to_new; // [orig node] -> new supernode ID (-1 if not a root)
std::vector<int> new_to_old; // [new supernode ID] -> representative orig node
std::vector<int> src, dst;
std::vector<float> edge_weights;
std::vector<float> features; // centroid features, contiguous by type
std::vector<int> type_boundaries_new;
std::vector<int> feature_dims_new;
};
// Compressed Sparse Row storage for heterogeneous graphs (HCGC path only)
struct CSRGraph {
// ── Graph structure ───────────────────────────────────────────────────────
std::vector<int> node_ptr;
std::vector<int> edge_dst;
std::vector<float> edge_weight;
int num_nodes;
// ── Feature data (pointers into external or owned arrays) ────────────────
const float *node_features_1d;
const int *feature_dims;
const int *type_boundaries;
int num_types;
std::vector<int> feature_offsets;
// ── Owned storage for coarsened-level graphs ──────────────────────────────
std::vector<float> owned_features;
std::vector<int> owned_type_boundaries;
std::vector<int> owned_feature_dims_vec;
// ── Coalition tracking (union-find) ──────────────────────────────────────
std::vector<int> coalition_map; // parent pointer; root ↔ coalition_map[v]==v
std::vector<int> coalition_size;
std::vector<std::vector<float>> coalition_feat_sum; // sum of features per coalition root
// ── HCGC: per-type feature variance (adaptive merge threshold) ───────────
// feat_var[t] = average squared L2 distance from type mean.
// Used as the merge threshold in coarse_graph_hcgc().
std::vector<float> feat_var;
std::vector<std::vector<float>> type_mean; // [t][d] = centroid of type t
// ── Hub-anchor: top-k% degree nodes per type are frozen ──────────────────
float hub_anchor_percentile = 0.0f; // 0 = disabled; 0.01 = top 1%
std::vector<bool> frozen; // frozen[v] → cannot be a merge candidate
std::vector<bool> finalized; // finalized[v] → merged by Ball Multi-Merge; locked permanently so rebuilt singletons cannot thrash
// ── Per-type merge threshold overrides ───────────────────────────────────
// If non-empty: threshold for type t = feat_var_scale_per_type[t] * feat_var[t]
// Falls back to the global feat_var_scale scalar when empty or out-of-range.
std::vector<float> feat_var_scale_per_type;
// If non-empty: threshold for (source type, mediator type) =
// feat_var_scale_by_src_med[src * num_types + med] * feat_var[src].
// Falls back to per-source-type and then global scale.
std::vector<float> feat_var_scale_by_src_med;
// ── Scalability caps ──────────────────────────────────────────────────────
int max_candidates_per_mediator = 0;
int max_hub_degree = 0;
std::vector<int> hub_degree_caps; // per-type; 0 = unlimited for that type
std::vector<float> type_mean_deg; // per-type mean node degree (parameter-free deg weighting)
// ── Helpers ───────────────────────────────────────────────────────────────
inline int hub_cap_for_type(int t) const {
if (!hub_degree_caps.empty() && t >= 0 && t < (int)hub_degree_caps.size())
return hub_degree_caps[t];
return max_hub_degree;
}
inline float threshold_scale_for_pair(int t_src, int t_med,
float fallback) const {
int idx = t_src * num_types + t_med;
if (!feat_var_scale_by_src_med.empty() &&
idx >= 0 && idx < (int)feat_var_scale_by_src_med.size())
return feat_var_scale_by_src_med[idx];
if (!feat_var_scale_per_type.empty() &&
t_src >= 0 && t_src < (int)feat_var_scale_per_type.size())
return feat_var_scale_per_type[t_src];
return fallback;
}
// ── Auto hub-degree caps (mean + k_sigma * std of total degree per type) ──
void compute_auto_hub_caps(float k_sigma = 3.0f) {
hub_degree_caps.resize(num_types, 0);
std::cout << "[C++] Auto hub caps (mean + " << k_sigma << "*std of total degree):\n";
for (int t = 0; t < num_types; ++t) {
int start = (t > 0) ? type_boundaries[t - 1] : 0;
int end = type_boundaries[t];
int n = end - start;
if (n == 0) continue;
double sum = 0.0, sum_sq = 0.0;
for (int u = start; u < end; ++u) {
int deg = node_ptr[u + 1] - node_ptr[u];
sum += deg;
sum_sq += (double)deg * deg;
}
double mean = sum / n;
double std_ = std::sqrt(std::max(0.0, sum_sq / n - mean * mean));
int cap = std::max(1, static_cast<int>(std::round(mean + k_sigma * std_)));
hub_degree_caps[t] = cap;
std::cout << " type " << t << " deg_mean=" << mean
<< " deg_std=" << std_ << " cap=" << cap << "\n";
}
}
// ── Per-type mean degree (used for parameter-free degree penalty) ─────────
// deg_factor = 1 + log1p(max_deg / mean_deg_type)
// No free hyperparameter — entirely derived from the graph's degree distribution.
// High-degree nodes (hubs) are naturally penalized more without needing role_alpha.
void compute_type_mean_deg() {
type_mean_deg.resize(num_types, 1.0f);
for (int t = 0; t < num_types; ++t) {
int start = (t > 0) ? type_boundaries[t - 1] : 0;
int end = type_boundaries[t];
int n = end - start;
if (n == 0) { type_mean_deg[t] = 1.0f; continue; }
double sum = 0.0;
for (int u = start; u < end; ++u)
sum += (node_ptr[u + 1] - node_ptr[u]);
type_mean_deg[t] = static_cast<float>(std::max(1.0, sum / n));
}
std::cout << "[C++] HCGC type_mean_deg:";
for (int t = 0; t < num_types; ++t)
std::cout << " t" << t << "=" << type_mean_deg[t];
std::cout << "\n";
std::cout.flush();
}
// ── Hub-anchor: freeze top hub_anchor_percentile fraction per type ────────
void compute_frozen_nodes() {
frozen.assign(num_nodes, false);
if (hub_anchor_percentile <= 0.0f) return;
int frozen_total = 0;
for (int t = 0; t < num_types; ++t) {
int start = (t > 0) ? type_boundaries[t - 1] : 0;
int end = type_boundaries[t];
int n = end - start;
if (n == 0) continue;
std::vector<std::pair<int,int>> deg_nodes;
deg_nodes.reserve(n);
for (int u = start; u < end; ++u)
deg_nodes.push_back({node_ptr[u+1] - node_ptr[u], u});
std::sort(deg_nodes.begin(), deg_nodes.end(),
[](const auto &a, const auto &b){ return a.first > b.first; });
int k = std::max(1, static_cast<int>(std::ceil(n * hub_anchor_percentile)));
if (deg_nodes[0].first <= 1) continue; // no meaningful degree variation
for (int i = 0; i < k && i < n; ++i)
frozen[deg_nodes[i].second] = true;
frozen_total += k;
std::cout << " [HubAnchor] type " << t
<< " n=" << n << " frozen top-" << k
<< " (deg " << deg_nodes[k-1].first << "+"
<< " max=" << deg_nodes[0].first << ")\n";
}
std::cout << " [HubAnchor] total frozen: " << frozen_total
<< " / " << num_nodes << " nodes\n";
}
inline int get_node_type(int u) const {
for (int t = 0; t < num_types; ++t)
if (u < type_boundaries[t]) return t;
return -1;
}
inline const float *get_node_feature(int u) const {
int t = get_node_type(u);
if (t < 0) return nullptr;
int local = u - (t > 0 ? type_boundaries[t - 1] : 0);
return node_features_1d + feature_offsets[t] + local * feature_dims[t];
}
// ── Initialization ────────────────────────────────────────────────────────
void init_features(const float *features_1d, const int *boundaries,
const int *dims, int types) {
node_features_1d = features_1d;
type_boundaries = boundaries;
feature_dims = dims;
num_types = types;
feature_offsets.resize(num_types, 0);
int off = 0;
for (int t = 0; t < num_types; ++t) {
feature_offsets[t] = off;
int cnt = type_boundaries[t] - (t > 0 ? type_boundaries[t - 1] : 0);
off += cnt * feature_dims[t];
}
}
void init_features_from_owned(std::vector<float> feats,
std::vector<int> boundaries,
std::vector<int> dims) {
owned_features = std::move(feats);
owned_type_boundaries = std::move(boundaries);
owned_feature_dims_vec = std::move(dims);
num_types = static_cast<int>(owned_type_boundaries.size());
node_features_1d = owned_features.data();
type_boundaries = owned_type_boundaries.data();
feature_dims = owned_feature_dims_vec.data();
feature_offsets.resize(num_types, 0);
int cur = 0;
for (int t = 0; t < num_types; ++t) {
feature_offsets[t] = cur;
int cnt = owned_type_boundaries[t] - (t > 0 ? owned_type_boundaries[t - 1] : 0);
cur += cnt * owned_feature_dims_vec[t];
}
}
void build_from_edgelist(int n, const int *src, const int *dst,
const float *weights, int num_edges) {
num_nodes = n;
node_ptr.assign(num_nodes + 1, 0);
edge_dst.resize(num_edges * 2);
edge_weight.resize(num_edges * 2);
for (int i = 0; i < num_edges; ++i) {
node_ptr[src[i]]++;
node_ptr[dst[i]]++;
}
int cur = 0;
for (int i = 0; i < num_nodes; ++i) {
int d = node_ptr[i];
node_ptr[i] = cur;
cur += d;
}
node_ptr[num_nodes] = cur;
std::vector<int> cursor = node_ptr;
for (int i = 0; i < num_edges; ++i) {
int u = src[i], v = dst[i];
float w = weights[i];
edge_dst[cursor[u]] = v;
edge_weight[cursor[u]++] = w;
edge_dst[cursor[v]] = u;
edge_weight[cursor[v]++] = w;
}
}
// ── HCGC: per-type feature variance (adaptive merge threshold) ───────────
// Computes the average squared L2 distance of each node from its type mean.
// Serves as the natural Dirichlet-energy threshold: merge two coalitions
// whose squared centroid distance is below this "typical spread".
void compute_feat_var() {
feat_var.resize(num_types, 1.0f);
type_mean.resize(num_types);
for (int t = 0; t < num_types; ++t) {
int start = (t > 0) ? type_boundaries[t - 1] : 0;
int end = type_boundaries[t];
int n = end - start, dim = feature_dims[t];
type_mean[t].assign(dim, 0.0f);
if (n < 2 || dim == 0) { feat_var[t] = 1.0f; continue; }
std::vector<double> mean(dim, 0.0);
for (int u = start; u < end; ++u) {
const float *f = get_node_feature(u);
for (int d = 0; d < dim; ++d) mean[d] += f[d];
}
for (int d = 0; d < dim; ++d) mean[d] /= n;
for (int d = 0; d < dim; ++d)
type_mean[t][d] = static_cast<float>(mean[d]);
double var = 0.0;
for (int u = start; u < end; ++u) {
const float *f = get_node_feature(u);
for (int d = 0; d < dim; ++d) {
double diff = f[d] - mean[d];
var += diff * diff;
}
}
float fv = static_cast<float>(var / n);
if (!std::isfinite(fv) || fv <= 0.0f) {
feat_var[t] = 1.0f;
std::cout << "[C++] HCGC type " << t
<< " feat_var non-finite (raw=" << fv << "), fallback=1.0\n";
} else {
feat_var[t] = fv;
std::cout << "[C++] HCGC type " << t << " feat_var=" << feat_var[t] << "\n";
}
}
}
// ── Coalition union-find ──────────────────────────────────────────────────
inline int find_root(int x) const {
while (coalition_map[x] != x) x = coalition_map[x];
return x;
}
// Flatten all nodes to point directly at their root (path compression).
void normalize_coalition_map() {
for (int i = 0; i < num_nodes; ++i)
coalition_map[i] = find_root(i);
}
// Merge coalition `from` into coalition `into`.
inline void merge_coalitions(int from, int into) {
if (from == into) return;
int dim = (int)coalition_feat_sum[into].size();
for (int d = 0; d < dim; ++d)
coalition_feat_sum[into][d] += coalition_feat_sum[from][d];
coalition_size[into] += coalition_size[from];
coalition_size[from] = 0;
coalition_map[from] = into;
}
inline float coalition_centroid_dist_sq(int a, int b, int dim) const {
float cnt_a = static_cast<float>(std::max(1, coalition_size[a]));
float cnt_b = static_cast<float>(std::max(1, coalition_size[b]));
float dist_sq = 0.0f;
for (int d = 0; d < dim; ++d) {
float fa = coalition_feat_sum[a][d] / cnt_a;
float fb = coalition_feat_sum[b][d] / cnt_b;
float diff = fa - fb;
dist_sq += diff * diff;
}
return dist_sq;
}
inline float coalition_merge_cost(int into, int from, int dim,
float w_eff,
bool marginal_join_cost) const {
float dist_sq = coalition_centroid_dist_sq(into, from, dim);
if (!marginal_join_cost)
return w_eff * dist_sq;
float cnt_into = static_cast<float>(std::max(1, coalition_size[into]));
float cnt_from = static_cast<float>(std::max(1, coalition_size[from]));
float ward = (cnt_into * cnt_from) / std::max(cnt_into + cnt_from, 1.0f);
return w_eff * ward * dist_sq;
}
inline float coalition_centroid_coord(int root, int d) const {
float cnt = static_cast<float>(std::max(1, coalition_size[root]));
return coalition_feat_sum[root][d] / cnt;
}
float local_projected_dirichlet_delta(
int cu, int cv, int dim,
float w_c, float w_d,
const std::vector<std::pair<int,float>> &candidates) const {
float n_c = static_cast<float>(std::max(1, coalition_size[cu]));
float n_d = static_cast<float>(std::max(1, coalition_size[cv]));
float inv_m = 1.0f / std::max(n_c + n_d, 1.0f);
float delta = 0.0f;
// The candidate list is the local same-type graph induced by one mediator.
// Merging cu and cv removes their projected edge and rewires both incident
// projected edges to the merged centroid.
for (const auto &cand : candidates) {
int r = find_root(cand.first);
if (r == cu || r == cv) continue;
float w_r = cand.second;
float dist_cr = 0.0f, dist_dr = 0.0f, dist_mr = 0.0f;
for (int d = 0; d < dim; ++d) {
float mu_c = coalition_centroid_coord(cu, d);
float mu_d = coalition_centroid_coord(cv, d);
float mu_r = coalition_centroid_coord(r, d);
float mu_m = (n_c * mu_c + n_d * mu_d) * inv_m;
float diff_cr = mu_c - mu_r;
float diff_dr = mu_d - mu_r;
float diff_mr = mu_m - mu_r;
dist_cr += diff_cr * diff_cr;
dist_dr += diff_dr * diff_dr;
dist_mr += diff_mr * diff_mr;
}
delta += (w_c * w_r + w_d * w_r) * dist_mr
- (w_c * w_r) * dist_cr
- (w_d * w_r) * dist_dr;
}
delta -= (w_c * w_d) * coalition_centroid_dist_sq(cu, cv, dim);
if (!std::isfinite(delta))
return std::numeric_limits<float>::infinity();
return delta;
}
void init_coalition_map() {
coalition_map.assign(num_nodes, 0);
coalition_size.assign(num_nodes, 1);
coalition_feat_sum.resize(num_nodes);
for (int i = 0; i < num_nodes; ++i) {
coalition_map[i] = i;
int t = get_node_type(i), dim = feature_dims[t];
const float *f = get_node_feature(i);
coalition_feat_sum[i].assign(f, f + dim);
}
}
// ── Coarsened graph construction ──────────────────────────────────────────
CoarsenedData build_coarsened_data() {
CoarsenedData cd;
cd.old_to_new.assign(num_nodes, -1);
cd.feature_dims_new.assign(feature_dims, feature_dims + num_types);
cd.type_boundaries_new.resize(num_types);
int new_id = 0;
for (int t = 0; t < num_types; ++t) {
int start = (t > 0) ? type_boundaries[t - 1] : 0;
int end = type_boundaries[t];
for (int u = start; u < end; ++u)
if (coalition_map[u] == u) { // u is a root
cd.old_to_new[u] = new_id++;
cd.new_to_old.push_back(u);
}
cd.type_boundaries_new[t] = new_id;
}
cd.num_nodes_new = new_id;
int total_feat = 0;
for (int t = 0; t < num_types; ++t) {
int cnt = cd.type_boundaries_new[t] -
(t > 0 ? cd.type_boundaries_new[t - 1] : 0);
total_feat += cnt * feature_dims[t];
}
cd.features.resize(total_feat);
int feat_off = 0;
for (int t = 0; t < num_types; ++t) {
int start = (t > 0) ? type_boundaries[t - 1] : 0;
int end = type_boundaries[t], dim = feature_dims[t];
for (int u = start; u < end; ++u) {
if (coalition_map[u] == u) { // root: emit centroid
float inv = 1.0f / static_cast<float>(coalition_size[u]);
for (int d = 0; d < dim; ++d)
cd.features[feat_off + d] = coalition_feat_sum[u][d] * inv;
feat_off += dim;
}
}
}
// Use unordered_map<int64_t> for O(1) amortised lookup and better cache
// behaviour vs map<pair<int,int>> — ~20-50x faster on large graphs.
std::unordered_map<int64_t, float> edge_map;
edge_map.reserve(static_cast<size_t>(num_nodes) * 4);
const int progress_step = std::max(1, num_nodes / 20);
for (int u = 0; u < num_nodes; ++u) {
if (u % progress_step == 0) {
std::cout << "\r[HCGC] build_coarsened_data:\t" << u << "/" << num_nodes
<< " (" << (100 * u / num_nodes) << "%)\tedges so far: "
<< edge_map.size() << " ";
std::cout.flush();
}
int nu = cd.old_to_new[find_root(u)];
for (int e = node_ptr[u]; e < node_ptr[u + 1]; ++e) {
int v = edge_dst[e];
if (u >= v) continue;
int nv = cd.old_to_new[find_root(v)];
if (nu == nv) continue;
int64_t key = (nu < nv)
? (((int64_t)nu << 32) | (int64_t)(uint32_t)nv)
: (((int64_t)nv << 32) | (int64_t)(uint32_t)nu);
edge_map[key] += edge_weight[e];
}
}
std::cout << "\r[HCGC] build_coarsened_data:\t" << num_nodes << "/" << num_nodes
<< " (100%)\ttotal edges: " << edge_map.size() << " \n";
std::cout.flush();
cd.src.reserve(edge_map.size());
cd.dst.reserve(edge_map.size());
cd.edge_weights.reserve(edge_map.size());
for (auto &kv : edge_map) {
cd.src.push_back(static_cast<int>(kv.first >> 32));
cd.dst.push_back(static_cast<int>(kv.first & 0xFFFFFFFFLL));
cd.edge_weights.push_back(kv.second);
}
return cd;
}
// ── Node Reassignment (Jacobi-style best-response) ───────────────────────
//
// After Ball Multi-Merge, each node checks whether it would lower its
// Dirichlet energy by moving to a different coalition it can see via
// cross-type mediator paths. All moves are recorded first (read phase),
// then applied atomically (Jacobi write phase) to avoid order-dependency.
//
// Only singleton/non-root nodes can switch: multi-member roots would drag
// their children along (that is a merge, not a switch).
//
// Threshold: same as Ball Multi-Merge (feat_var_scale * feat_var[t]).
// Using the same energy criterion keeps reassignment consistent with the
// merge step and prevents cascade collapse from over-aggressive re-routing.
//
int reassignment_pass(float feat_var_scale) {
normalize_coalition_map(); // flatten: coalition_map[v] == root directly
struct PendingSwitch { int node_v, t_src, old_root, new_root; };
std::vector<PendingSwitch> pending;
// Stamped-array deduplication: seen_mark[r] == v means root r was already
// found while processing v. O(1) lookup/insert, no per-node heap alloc.
std::vector<int> seen_mark(num_nodes, -1);
std::vector<int> avail_roots;
avail_roots.reserve(64);
constexpr int max_avail_roots = 32;
constexpr int max_fruitless = 30;
for (int t_src = 0; t_src < num_types; ++t_src) {
int src_start = (t_src > 0) ? type_boundaries[t_src - 1] : 0;
int src_end = type_boundaries[t_src];
int dim = feature_dims[t_src];
float eff_scale = (!feat_var_scale_per_type.empty() &&
t_src < (int)feat_var_scale_per_type.size())
? feat_var_scale_per_type[t_src] : feat_var_scale;
float threshold = 1.0f * eff_scale *
((t_src < (int)feat_var.size()) ? feat_var[t_src] : 1.0f);
int type_n = src_end - src_start;
int prog_step = std::max(1, type_n / 10);
for (int v = src_start; v < src_end; ++v) {
if ((v - src_start) % prog_step == 0) {
int pct = 100 * (v - src_start) / type_n;
std::cout << "\r[HCGC] reassignment type=" << t_src
<< "\t" << pct << "%\tnodes=" << type_n
<< "\tpending=" << pending.size() << " ";
std::cout.flush();
}
int old_root = coalition_map[v];
// Skip any node whose root was ever merged by Ball Multi-Merge
// (across ALL outers and levels, not just this one).
// finalized[] grows monotonically and survives rebuild via make_compact_graph.
// This prevents the cascade where rebuild resets coalition_size=1 and
// allows previously-merged nodes to thrash in subsequent outers.
if (!finalized.empty() && finalized[old_root]) continue;
// Skip hub nodes
int cap_v = hub_cap_for_type(t_src);
if (cap_v > 0 && (node_ptr[v+1] - node_ptr[v]) > cap_v) continue;
// Skip frozen nodes (hub-anchor)
if (!frozen.empty() && frozen[v]) continue;
// Collect distinct coalition roots reachable in 2 hops
seen_mark[old_root] = v;
avail_roots.clear();
bool capped = false;
for (int e = node_ptr[v]; e < node_ptr[v+1] && !capped; ++e) {
int nb = edge_dst[e];
if (get_node_type(nb) == t_src) continue; // only cross-type mediators
int cap_nb = hub_cap_for_type(get_node_type(nb));
if (cap_nb > 0 && (node_ptr[nb+1] - node_ptr[nb]) > cap_nb) continue;
int fruitless = 0;
for (int e2 = node_ptr[nb]; e2 < node_ptr[nb+1]; ++e2) {
int u = edge_dst[e2];
if (get_node_type(u) != t_src) continue;
int r = coalition_map[u];
if (seen_mark[r] != v) {
seen_mark[r] = v;
avail_roots.push_back(r);
fruitless = 0;
if ((int)avail_roots.size() >= max_avail_roots) { capped = true; break; }
} else {
if (++fruitless >= max_fruitless) break;
}
}
}
if (avail_roots.empty()) continue;
const float *fv = get_node_feature(v);
// Cost of staying: dist² between v and (old_root \ v) centroid
float current_cost;
if (coalition_size[old_root] <= 1) {
current_cost = std::numeric_limits<float>::max();
} else {
int rem = coalition_size[old_root] - 1;
float inv = 1.0f / static_cast<float>(rem);
current_cost = 0.0f;
for (int d = 0; d < dim; ++d) {
float c = (coalition_feat_sum[old_root][d] - fv[d]) * inv;
float diff = fv[d] - c;
current_cost += diff * diff;
}
}
int best_root = -1;
float best_cost = current_cost;
for (int r : avail_roots) {
if (coalition_size[r] <= 0) continue;
float inv = 1.0f / static_cast<float>(coalition_size[r]);
float cand = 0.0f;
for (int d = 0; d < dim; ++d) {
float c = coalition_feat_sum[r][d] * inv;
float diff = fv[d] - c;
cand += diff * diff;
}
if (cand < best_cost) { best_cost = cand; best_root = r; }
}
// Degree penalty (parameter-free): high-degree nodes are harder to reassign.
// deg_factor ≥ 1 always, so this only tightens the acceptance criterion.
if (best_root != -1) {
float deg_v = static_cast<float>(node_ptr[v + 1] - node_ptr[v]);
float mean_d = (t_src < (int)type_mean_deg.size())
? type_mean_deg[t_src] : 1.0f;
float deg_factor = 1.0f + std::log1p(deg_v / std::max(mean_d, 1.0f));
if (best_cost * deg_factor <= threshold)
pending.push_back({v, t_src, old_root, best_root});
}
}
}
// ── Jacobi write phase ─────────────────────────────────────────────────
// Guards:
// 1. old_root check: skip if v was already moved by an earlier switch.
// 2. new_root check: skip if new_root is no longer a self-pointing root
// (prevents coalition_map cycles that make normalize loop infinitely).
int total_switches = 0;
for (auto &sw : pending) {
int v = sw.node_v;
int dim = feature_dims[sw.t_src];
if (coalition_map[v] != sw.old_root) continue; // guard 1
if (coalition_map[sw.new_root] != sw.new_root) continue; // guard 2
const float *fv = get_node_feature(v);
coalition_size[sw.old_root]--;
for (int d = 0; d < dim; ++d) coalition_feat_sum[sw.old_root][d] -= fv[d];
coalition_size[sw.new_root]++;
for (int d = 0; d < dim; ++d) coalition_feat_sum[sw.new_root][d] += fv[d];
coalition_map[v] = sw.new_root;
++total_switches;
}
normalize_coalition_map();
std::cout << "\n[HCGC] Reassignment:\t" << total_switches << " switches\n";
return total_switches;
}
// ── HCGC: Heterogeneous CGC (marginal Dirichlet energy) ──────────────────
//
// Merge criterion: merge coalitions u and v if their weighted centroid
// distance would not increase the graph's Dirichlet energy beyond the
// per-type variance threshold:
// ΔDE = (w_um * w_vm) * ||μ_u - μ_v||² <= feat_var_scale * feat_var[t_src]
//
// feat_var[t] is recomputed each outer pass from current centroids, so the
// threshold tightens naturally as coalitions grow — no fixed hyperparameter.
//
// Outer loop repeats until neither Ball Multi-Merge nor Node Reassignment
// produces any change (stable-state convergence).
//
// Returns {merges_this_outer, switches_this_outer}.
std::pair<int,int> coarse_graph_hcgc_once(int inner_passes,
float feat_var_scale,
bool skip_reassignment,
int outer_idx,
int window_size = 20,
int merge_cap_per_leader = 0,
int merge_objective = 0) {
std::mt19937 rng(42 + outer_idx);
std::normal_distribution<float> rand_dist(0.0f, 1.0f);
int merges_this_outer = 0;
std::vector<bool> matched(num_nodes, false);
for (int pass = 0; pass < inner_passes; ++pass) {
for (int t_src = 0; t_src < num_types; ++t_src) {
int dim = feature_dims[t_src];
std::vector<float> proj_vec(dim);
for (int d = 0; d < dim; ++d) proj_vec[d] = rand_dist(rng);
for (int t_med = 0; t_med < num_types; ++t_med) {
int med_start = (t_med > 0) ? type_boundaries[t_med - 1] : 0;
int med_end = type_boundaries[t_med];
int cap_med = hub_cap_for_type(t_med);
float eff_scale_ball = threshold_scale_for_pair(
t_src, t_med, feat_var_scale);
float threshold = eff_scale_ball *
((t_src < (int)feat_var.size()) ? feat_var[t_src] : 1.0f);
// Rank mediators by number of t_src neighbours (highest first)
std::vector<std::pair<int,int>> leaders;
leaders.reserve(med_end - med_start);
for (int m = med_start; m < med_end; ++m) {
if (cap_med > 0 && (node_ptr[m+1] - node_ptr[m]) > cap_med) continue;
int score = 0;
for (int e = node_ptr[m]; e < node_ptr[m+1]; ++e)
if (get_node_type(edge_dst[e]) == t_src) ++score;
if (score >= 2) leaders.push_back({score, m});
}
std::sort(leaders.begin(), leaders.end(),
[](const auto &a, const auto &b){ return a.first > b.first; });
int total_merges = 0;
for (size_t li = 0; li < leaders.size(); ++li) {
int m = leaders[li].second;
// Collect unmatched t_src neighbours (excluding frozen nodes)
std::vector<std::pair<int,float>> candidates;
for (int e = node_ptr[m]; e < node_ptr[m+1]; ++e) {
int v = edge_dst[e];
if (get_node_type(v) == t_src && !matched[v]
&& (frozen.empty() || !frozen[v]))
candidates.push_back({v, edge_weight[e]});
}
if (candidates.size() < 2) continue;
// 1D LSH sort: project centroids onto a random unit vector and sort.
// This places similar coalitions adjacently so the O(window_size)
// sliding-window comparison below approximates O(N²) pairwise search.
std::vector<float> proj_vals(candidates.size());
for (size_t ci2 = 0; ci2 < candidates.size(); ++ci2) {
int r = find_root(candidates[ci2].first);
float c = static_cast<float>(std::max(1, coalition_size[r]));
float p = 0.0f;
if (!coalition_feat_sum[r].empty())
for (int d = 0; d < dim; ++d)
p += (coalition_feat_sum[r][d] / c) * proj_vec[d];
proj_vals[ci2] = p;
}
std::vector<size_t> order(candidates.size());
std::iota(order.begin(), order.end(), 0);
std::sort(order.begin(), order.end(),
[&proj_vals](size_t a, size_t b){
return proj_vals[a] < proj_vals[b]; });
{
std::vector<std::pair<int,float>> tmp(candidates.size());
std::vector<float> tmp_proj(candidates.size());
for (size_t k = 0; k < candidates.size(); ++k) {
tmp[k] = candidates[order[k]];
tmp_proj[k] = proj_vals[order[k]];
}
candidates = std::move(tmp);
proj_vals = std::move(tmp_proj);
}
// Stride-sample if over cap
if (max_candidates_per_mediator > 0 &&
(int)candidates.size() > max_candidates_per_mediator) {
std::vector<std::pair<int,float>> sampled;
std::vector<float> sampled_proj;
sampled.reserve(max_candidates_per_mediator);
sampled_proj.reserve(max_candidates_per_mediator);
float stride = static_cast<float>(candidates.size()) /
static_cast<float>(max_candidates_per_mediator);
for (int si = 0; si < max_candidates_per_mediator; ++si) {
int idx = static_cast<int>(si * stride);
sampled.push_back(candidates[idx]);
sampled_proj.push_back(proj_vals[idx]);
}
candidates = std::move(sampled);
proj_vals = std::move(sampled_proj);
}
// 3-phase Ball Multi-Merge: avoids the greedy first-come-first-merged
// bias by electing leaders based on local density before merging.
bool marginal_join_cost = (merge_cap_per_leader > 0);
bool use_projected_de = (merge_objective == 1);
// Phase 1: Density Estimation
std::vector<int> density(candidates.size(), 0);
for (size_t i = 0; i < candidates.size(); ++i) {
int cu = find_root(candidates[i].first);
int start_j = std::max(0, static_cast<int>(i) - window_size);
int end_j = std::min(static_cast<int>(candidates.size()),
static_cast<int>(i) + window_size + 1);
for (int j = start_j; j < end_j; ++j) {
if (i == (size_t)j) continue;
int cv = find_root(candidates[j].first);
if (cu == cv) { density[i]++; continue; }
float w_eff = candidates[i].second * candidates[j].second;
float gate_cost = coalition_merge_cost(cu, cv, dim, w_eff,
marginal_join_cost);
bool admissible = false;
if (use_projected_de) {
float de_delta = local_projected_dirichlet_delta(
cu, cv, dim, candidates[i].second, candidates[j].second,
candidates);
admissible = (de_delta < 0.0f && gate_cost <= threshold);
} else {
admissible = (gate_cost <= threshold);
}
if (admissible) density[i]++;
}
}
// Phase 2: Leader Selection (sort by density descending)
std::vector<size_t> density_order(candidates.size());
std::iota(density_order.begin(), density_order.end(), 0);
std::sort(density_order.begin(), density_order.end(),
[&density](size_t a, size_t b){ return density[a] > density[b]; });
// Phase 3: Ball Multi-Merge (union-find)
// merge_coalitions() updates coalition_feat_sum[cu] in-place,
// so each subsequent comparison sees the grown centroid.
//
// If merge_cap_per_leader > 0, the density leader only accepts the
// cheapest eligible candidate(s) under marginal join cost:
// Δ = w_eff * |C||S|/(|C|+|S|) * ||mu_C - mu_S||^2.
// cap=1 gives the CGC-like one-by-one coalition formation ablation.
std::vector<bool> local_matched(candidates.size(), false);
for (size_t idx = 0; idx < candidates.size(); ++idx) {
size_t ci = density_order[idx];
if (local_matched[ci]) continue;
int u = candidates[ci].first;
if (matched[u]) continue;
int cu = find_root(u);
int start_j = std::max(0, static_cast<int>(ci) - window_size);
int end_j = std::min(static_cast<int>(candidates.size()),
static_cast<int>(ci) + window_size + 1);
bool any_merge = false;
int merges_for_leader = 0;
while (merge_cap_per_leader <= 0 ||
merges_for_leader < merge_cap_per_leader) {
int best_j = -1;
float best_cost = std::numeric_limits<float>::infinity();
for (int j = start_j; j < end_j; ++j) {
if (local_matched[j]) continue;
int v = candidates[j].first;
if (matched[v]) continue;
int cv = find_root(v);
if (cu == cv) continue;
float w_eff = candidates[ci].second * candidates[j].second;
float gate_cost = coalition_merge_cost(cu, cv, dim, w_eff,
marginal_join_cost);
float cost = gate_cost;
bool admissible = (gate_cost <= threshold);
if (use_projected_de) {
cost = local_projected_dirichlet_delta(
cu, cv, dim, candidates[ci].second,
candidates[j].second, candidates);
admissible = (cost < 0.0f && gate_cost <= threshold);
}
if (!admissible) continue;
if (merge_cap_per_leader <= 0) {
merge_coalitions(cv, cu);
local_matched[j] = true;
matched[v] = true;
any_merge = true;
++merges_for_leader;
++total_merges;
++merges_this_outer;
} else if (cost < best_cost) {
best_cost = cost;
best_j = j;
}
}
if (merge_cap_per_leader <= 0) break;
if (best_j < 0) break;
int best_v = candidates[best_j].first;
int best_cv = find_root(best_v);
if (best_cv == cu) break;
merge_coalitions(best_cv, cu);
local_matched[best_j] = true;
matched[best_v] = true;
any_merge = true;
++merges_for_leader;
++total_merges;
++merges_this_outer;
}
if (any_merge) {
matched[u] = true;
local_matched[ci] = true;
}
}
}
std::cout << "\r [outer " << (outer_idx+1)
<< " | pass " << (pass+1) << "/" << inner_passes
<< "]\tsrc=" << t_src << " med=" << t_med
<< ":\t" << total_merges << " merges\t("
<< merges_this_outer << " total) ";
std::cout.flush();
} // t_med
} // t_src
} // inner pass
std::cout << "\n[HCGC] Inner passes done:\t" << merges_this_outer << " merges\n";
std::cout.flush();
// ── Finalize merged roots ─────────────────────────────────────────────
// After Ball Multi-Merge: any root with coalition_size > 1 is permanently
// locked. This set grows monotonically across outers AND across rebuild
// boundaries (make_compact_graph transfers it).
// Without this, rebuild resets coalition_size=1 for every node, making
// the singleton-only check vacuous and causing runaway merges at high scales.
{
normalize_coalition_map(); // ensure flat so every root is self-pointed
if (finalized.empty()) finalized.assign(num_nodes, false);
int newly_finalized = 0;
for (int v = 0; v < num_nodes; ++v)
if (coalition_map[v] == v && coalition_size[v] > 1 && !finalized[v]) {
finalized[v] = true;
++newly_finalized;
}
std::cout << "[HCGC] Finalized " << newly_finalized
<< " new roots (total locked: "
<< std::count(finalized.begin(), finalized.end(), true)
<< " / " << num_nodes << ")\n";
std::cout.flush();
}
int switches_this_outer = 0;
if (!skip_reassignment) {
switches_this_outer = reassignment_pass(feat_var_scale);
} else {
std::cout << "[HCGC] Reassignment skipped.\n";
std::cout.flush();
}
return {merges_this_outer, switches_this_outer};
}
};
// ── HCGC entry point ─────────────────────────────────────────────────────────
//
// Threshold is automatically derived from per-type feature variance and
// tightens each outer pass as coalitions grow — self-regulating compression.
// Iterates until stable-state convergence (no merges and no reassignments).
//
py::array_t<int>
create_graph_hcgc(py::array_t<int> src_nodes,
py::array_t<int> dst_nodes,
py::array_t<float> weights,
py::array_t<float> all_features,
py::array_t<int> type_boundaries,
py::array_t<int> feature_dims,
int num_levels = 1,
int inner_passes = 2,
int max_outer = 10,
float feat_var_scale = 1.0f,
int max_candidates = 0,
int max_hub_degree = 0,
py::array_t<int> hub_degree_caps = py::array_t<int>(),
bool auto_hub_caps = true,
bool skip_reassignment = false,
int window_size = 20,
int merge_cap_per_leader = 0,
float hub_anchor_percentile = 0.0f,
py::array_t<float> feat_var_scale_per_type_arr = py::array_t<float>(),
py::array_t<float> feat_var_scale_by_src_med_arr = py::array_t<float>(),
float target_comp_ratio = 0.0f,
int merge_objective = 0) {
py::buffer_info src_buf = src_nodes.request();
py::buffer_info dst_buf = dst_nodes.request();
py::buffer_info w_buf = weights.request();
py::buffer_info feat_buf = all_features.request();
py::buffer_info bound_buf = type_boundaries.request();
py::buffer_info dims_buf = feature_dims.request();
int num_edges = src_buf.shape[0];
int num_types = bound_buf.shape[0];
const int *boundaries_ptr = static_cast<const int *>(bound_buf.ptr);
int num_nodes = boundaries_ptr[num_types - 1];
CSRGraph graph;
graph.init_features(static_cast<const float *>(feat_buf.ptr), boundaries_ptr,
static_cast<const int *>(dims_buf.ptr), num_types);
graph.build_from_edgelist(num_nodes,
static_cast<const int *>(src_buf.ptr),
static_cast<const int *>(dst_buf.ptr),
static_cast<const float *>(w_buf.ptr), num_edges);
std::cout << "[HCGC] Graph: " << num_nodes << " nodes, "
<< num_edges << " edges, " << num_types << " types.\n";