hypergraph-rs 0.3.0

Hypergraphs for Rust: a port of XGI built on rustworkx-core, with a value on every membership
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
//! The core `Hypergraph` data structure.

#[cfg(feature = "generators")]
use rand::RngExt;
#[cfg(feature = "generators")]
use rand_core::SeedableRng;
#[cfg(feature = "generators")]
use rand_pcg::Pcg64Mcg;
use rustworkx_core::petgraph::graph::{NodeIndex as GraphNodeIndex, UnGraph};
use rustworkx_core::petgraph::stable_graph::{NodeIndex, StableDiGraph};
use rustworkx_core::petgraph::visit::NodeIndexable;
use serde_json::Value;

use super::error::{EdgeError, MembershipError, NodeError, SwapError};
use super::idmap::IdMap;
use super::kinds::{MembershipEdge, NodeKind};

/// A hypergraph, represented as a bipartite graph.
///
/// The bipartite graph has two node kinds: `Agent` (the hypergraph's nodes)
/// and `Hyperedge` (the hypergraph's edges). Membership edges connect agents
/// to their hyperedges. This representation makes the hypergraph a genuine
/// rustworkx-core plugin — all petgraph/rustworkx-core algorithms work on
/// the bipartite graph directly.
///
/// # Type Parameters
///
/// - `N` — agent node attribute type (defaults to `serde_json::Value`)
/// - `E` — hyperedge attribute type (defaults to `serde_json::Value`)
/// - `M` — per-membership attribute type (defaults to `serde_json::Value`)
pub struct Hypergraph<N = serde_json::Value, E = serde_json::Value, M = serde_json::Value> {
    /// The bipartite graph: Agent nodes + Hyperedge nodes + membership edges.
    /// StableDiGraph preserves insertion order under removal (leaves holes,
    /// doesn't compact) — required for III.7 determinism parity.
    inner: StableDiGraph<NodeKind<N, E>, MembershipEdge<M>>,

    /// Insertion-ordered bimap: agent_id -> NodeIndex in `inner`.
    agent_ids: IdMap,

    /// Insertion-ordered bimap: edge_id -> NodeIndex in `inner`.
    hyperedge_ids: IdMap,

    /// Auto-incrementing counter for edges added without an explicit `idx`.
    edge_uid_counter: u64,

    /// Graph-level attributes (XGI's `H.graph` dict).
    graph_attrs: serde_json::Map<String, serde_json::Value>,

    /// Whether this hypergraph is frozen (mutation methods panic when true).
    frozen: bool,
}

/// How to name a merged edge (subset of XGI's `rename`; `"tuple"` deferred).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MergeRename {
    /// Reuse the smallest (sorted) duplicate id.
    First,
    /// Assign a fresh auto-generated id.
    New,
}

/// How to combine attrs of merged edges (XGI's `merge_rule`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MergeRule {
    /// Take the smallest-id duplicate's attrs.
    First,
    /// For each key, a JSON array of the distinct values (missing -> null).
    Union,
    /// For each key, the shared value or null if the duplicates disagree.
    Intersection,
}

/// What `cleanup` keeps. `true` keeps that kind; the default (all false) removes all of them, as XGI's defaults do.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct CleanupOptions {
    /// Keep isolated nodes.
    pub isolates: bool,
    /// Keep edges of size one.
    pub singletons: bool,
    /// Keep edges of size zero.
    pub empties: bool,
    /// Keep duplicate edges.
    pub multiedges: bool,
}

impl<N, E, M> Default for Hypergraph<N, E, M> {
    fn default() -> Self {
        Self::new()
    }
}

impl<N, E, M> Hypergraph<N, E, M> {
    /// Create an empty hypergraph.
    pub fn new() -> Self {
        Self {
            inner: StableDiGraph::new(),
            agent_ids: IdMap::new(),
            hyperedge_ids: IdMap::new(),
            edge_uid_counter: 0,
            graph_attrs: serde_json::Map::new(),
            frozen: false,
        }
    }

    /// Panic if the hypergraph is frozen. Called at the top of every
    /// mutation method. XGI parity: frozen-method guard (hypergraph.py:1550).
    fn assert_not_frozen(&self) {
        if self.frozen {
            panic!("Frozen hypergraph can't be modified");
        }
    }

    /// The number of agent nodes in the hypergraph.
    pub fn num_nodes(&self) -> usize {
        self.agent_ids.len()
    }

    /// The number of hyperedges in the hypergraph.
    pub fn num_edges(&self) -> usize {
        self.hyperedge_ids.len()
    }

    /// Add a node with attributes. Returns `true` if a new node was created,
    /// `false` if it already existed.
    ///
    /// XGI parity: `H.add_node(node, **attr)`. If `node_id` already exists,
    /// its stored attrs are REPLACED (XGI's binding merges the incoming
    /// dict into the existing one before calling; a generic `N` can't
    /// merge, so the core replaces and the PyO3 layer composes the merge
    /// on top — divergence D6).
    pub fn add_node(&mut self, node_id: &str, attrs: N) -> bool {
        self.assert_not_frozen();
        if let Some(idx) = self.agent_ids.get(node_id) {
            if let Some(NodeKind::Agent(existing)) = self.inner.node_weight_mut(*idx) {
                *existing = attrs;
            }
            return false;
        }
        let idx = self.inner.add_node(NodeKind::Agent(attrs));
        self.agent_ids.insert(node_id.to_string(), idx);
        true
    }

    /// Check if a node exists in the hypergraph.
    ///
    /// XGI parity: `n in H` / `H.has_node(n)`.
    pub fn has_node(&self, node_id: &str) -> bool {
        self.agent_ids.contains_key(node_id)
    }

    /// Add a hyperedge connecting the given members.
    ///
    /// XGI parity: `H.add_edge(members, idx=id, **attr)`.
    pub fn add_edge(
        &mut self,
        members: Vec<String>,
        idx: Option<String>,
        attrs: E,
    ) -> Result<String, EdgeError>
    where
        N: Default,
        M: Default + Clone,
    {
        self.assert_not_frozen();

        let edge_id = match &idx {
            Some(id) => {
                if self.hyperedge_ids.contains_key(id) {
                    return Err(EdgeError::AlreadyExists {
                        edge_id: id.clone(),
                    });
                }
                id.clone()
            }
            None => self.edge_uid_counter.to_string(),
        };

        // XGI 1.x `update_uid_counter` advances the counter for every explicit id that parses as an
        // integer, `0` included (XGI 0.10.2 skipped `0` as falsy: issue 720, fixed in 1.0).
        if let Some(id) = &idx {
            if let Ok(n) = id.parse::<u64>() {
                if n >= self.edge_uid_counter {
                    self.edge_uid_counter = n.saturating_add(1);
                }
            }
        } else {
            self.edge_uid_counter = self.edge_uid_counter.saturating_add(1);
        }

        let mut seen = std::collections::HashSet::new();
        let unique_members: Vec<String> = members
            .into_iter()
            .filter(|m| seen.insert(m.clone()))
            .collect();

        for member in &unique_members {
            if !self.agent_ids.contains_key(member) {
                let nidx = self.inner.add_node(NodeKind::Agent(N::default()));
                self.agent_ids.insert(member.clone(), nidx);
            }
        }

        let he_idx = self.inner.add_node(NodeKind::Hyperedge(attrs));
        self.hyperedge_ids.insert(edge_id.clone(), he_idx);

        for member in &unique_members {
            let agent_idx = self.agent_ids[member];
            let membership = MembershipEdge {
                member_data: M::default(),
            };
            self.inner.add_edge(agent_idx, he_idx, membership.clone());
            self.inner.add_edge(he_idx, agent_idx, membership);
        }

        Ok(edge_id)
    }

    /// Add a hyperedge with NO members (an empty edge). Returns the assigned edge id.
    ///
    /// **Documentation-defect fix (this comment previously claimed the opposite of what the code
    /// does — see the register entry below).** XGI 0.10.2's own docstring claims `add_edge([])`
    /// raises `XGIError`; its RUNTIME does not — it silently creates an empty edge, and XGI's own
    /// test suite asserts that (D1 register, `conform_add_edge_empty_creates_edge`). `add_edge`
    /// above matches the runtime, not the docstring: an empty member list is ACCEPTED, not
    /// rejected, and no `EdgeError::EmptyMembers` variant exists anywhere in this crate. This
    /// method is not a workaround for a rejection that does not happen — it is the explicit,
    /// self-documenting spelling for the empty-edge case (Phase 7 OQ-1), duplicating only
    /// `add_edge`'s id-assignment/counter policy, adding a lone `Hyperedge` node with no
    /// membership edges, and returning the id (so callers need no separate auto-id peek).
    pub fn add_empty_edge(&mut self, idx: Option<String>, attrs: E) -> String {
        self.assert_not_frozen();
        let edge_id = match &idx {
            Some(id) => id.clone(),
            None => self.edge_uid_counter.to_string(),
        };
        if let Some(id) = &idx {
            if let Ok(n) = id.parse::<u64>() {
                if n >= self.edge_uid_counter {
                    self.edge_uid_counter = n.saturating_add(1);
                }
            }
        } else {
            self.edge_uid_counter = self.edge_uid_counter.saturating_add(1);
        }
        let he_idx = self.inner.add_node(NodeKind::Hyperedge(attrs));
        self.hyperedge_ids.insert(edge_id.clone(), he_idx);
        edge_id
    }

    /// Check if a hyperedge exists.
    /// XGI parity: `id in H.edges`.
    pub fn has_edge(&self, edge_id: &str) -> bool {
        self.hyperedge_ids.contains_key(edge_id)
    }

    /// Get the edge IDs of which a node is a member, in insertion (membership-add)
    /// order. petgraph's `neighbors` walks a node's adjacency list in REVERSE
    /// insertion order, so we collect + reverse before mapping back to ids
    /// (same correction as DiHypergraph's D5 owner upgrade).
    /// XGI parity: `H.nodes.memberships(n)`.
    pub fn memberships(&self, node_id: &str) -> Option<Vec<String>> {
        let agent_idx = *self.agent_ids.get(node_id)?;
        let mut neighbor_idxs: Vec<_> = self.inner.neighbors(agent_idx).collect();
        neighbor_idxs.reverse();
        let mut result: Vec<String> = Vec::new();
        for neighbor_idx in neighbor_idxs {
            if let Some(NodeKind::Hyperedge(_)) = self.inner.node_weight(neighbor_idx) {
                if let Some(eid) = self.hyperedge_ids.id_of(neighbor_idx) {
                    result.push(eid.to_string());
                }
            }
        }
        Some(result)
    }

    /// Get the node IDs that are members of an edge, in insertion order (see
    /// [`Self::memberships`] for the reverse-adjacency correction).
    /// XGI parity: `H.edges.members(e)`.
    pub fn members(&self, edge_id: &str) -> Option<Vec<String>> {
        let he_idx = *self.hyperedge_ids.get(edge_id)?;
        let mut neighbor_idxs: Vec<_> = self.inner.neighbors(he_idx).collect();
        neighbor_idxs.reverse();
        let mut result: Vec<String> = Vec::new();
        for neighbor_idx in neighbor_idxs {
            if let Some(NodeKind::Agent(_)) = self.inner.node_weight(neighbor_idx) {
                if let Some(nid) = self.agent_ids.id_of(neighbor_idx) {
                    result.push(nid.to_string());
                }
            }
        }
        Some(result)
    }

    /// All node IDs in insertion order (III.7 determinism parity).
    /// XGI parity: `list(H.nodes)`.
    pub fn node_ids(&self) -> Vec<String> {
        self.agent_ids.keys().cloned().collect()
    }

    /// All edge IDs in insertion order (III.7 determinism parity).
    /// XGI parity: `list(H.edges)`.
    pub fn edge_ids(&self) -> Vec<String> {
        self.hyperedge_ids.keys().cloned().collect()
    }

    /// Remove a node from the hypergraph. Weak removal (`strong=false`)
    /// detaches the node from its edges and, when `remove_empty` is true,
    /// drops edges that become empty as a result; strong removal drops all
    /// containing edges regardless of `remove_empty` (D9).
    /// XGI parity: `H.remove_node(n, strong=False, remove_empty=True)`.
    pub fn remove_node(
        &mut self,
        node_id: &str,
        strong: bool,
        remove_empty: bool,
    ) -> Result<(), NodeError> {
        self.assert_not_frozen();
        let agent_idx = *self.agent_ids.get(node_id).ok_or(NodeError::NotFound {
            node_id: node_id.to_string(),
        })?;

        let edge_ids: Vec<String> = self.memberships(node_id).unwrap_or_default();

        if strong {
            for eid in &edge_ids {
                let he_idx = self.hyperedge_ids[eid];
                self.inner.remove_node(he_idx);
                self.hyperedge_ids.shift_remove(eid);
            }
        } else {
            for eid in &edge_ids {
                let he_idx = self.hyperedge_ids[eid];
                if let Some(e) = self.inner.find_edge(agent_idx, he_idx) {
                    self.inner.remove_edge(e);
                }
                if let Some(e) = self.inner.find_edge(he_idx, agent_idx) {
                    self.inner.remove_edge(e);
                }
                if remove_empty {
                    let has_members = self
                        .inner
                        .neighbors(he_idx)
                        .any(|n| matches!(self.inner.node_weight(n), Some(NodeKind::Agent(_))));
                    if !has_members {
                        self.inner.remove_node(he_idx);
                        self.hyperedge_ids.shift_remove(eid);
                    }
                }
            }
        }

        self.inner.remove_node(agent_idx);
        self.agent_ids.shift_remove(node_id);
        Ok(())
    }

    /// Remove a hyperedge from the hypergraph.
    /// XGI parity: `H.remove_edge(e)`.
    pub fn remove_edge(&mut self, edge_id: &str) -> Result<(), EdgeError> {
        self.assert_not_frozen();
        let he_idx = *self.hyperedge_ids.get(edge_id).ok_or(EdgeError::NotFound {
            edge_id: edge_id.to_string(),
        })?;
        self.inner.remove_node(he_idx);
        self.hyperedge_ids.shift_remove(edge_id);
        Ok(())
    }

    /// Read a node's attributes. XGI parity: `H.nodes[n]`.
    pub fn node_attrs(&self, node_id: &str) -> Option<&N> {
        let idx = self.agent_ids.get(node_id)?;
        match self.inner.node_weight(*idx)? {
            NodeKind::Agent(attrs) => Some(attrs),
            _ => None,
        }
    }

    /// Mutably access a node's attributes.
    pub fn node_attrs_mut(&mut self, node_id: &str) -> Option<&mut N> {
        let idx = *self.agent_ids.get(node_id)?;
        match self.inner.node_weight_mut(idx)? {
            NodeKind::Agent(attrs) => Some(attrs),
            _ => None,
        }
    }

    /// Read a hyperedge's attributes. XGI parity: `H.edges[e]`.
    pub fn edge_attrs(&self, edge_id: &str) -> Option<&E> {
        let idx = self.hyperedge_ids.get(edge_id)?;
        match self.inner.node_weight(*idx)? {
            NodeKind::Hyperedge(attrs) => Some(attrs),
            _ => None,
        }
    }

    /// Mutably access a hyperedge's attributes.
    pub fn edge_attrs_mut(&mut self, edge_id: &str) -> Option<&mut E> {
        let idx = *self.hyperedge_ids.get(edge_id)?;
        match self.inner.node_weight_mut(idx)? {
            NodeKind::Hyperedge(attrs) => Some(attrs),
            _ => None,
        }
    }

    /// Read a graph-level attribute. XGI parity: `H["name"]`.
    pub fn graph_attr(&self, key: &str) -> Option<&serde_json::Value> {
        self.graph_attrs.get(key)
    }

    /// Set a graph-level attribute. XGI parity: `H["name"] = value`.
    pub fn set_graph_attr(&mut self, key: &str, value: serde_json::Value) {
        self.graph_attrs.insert(key.to_string(), value);
    }

    /// Remove all nodes and edges. XGI parity: `H.clear()`.
    ///
    /// The edge-uid counter is deliberately NOT reset — XGI's `clear()`
    /// never touches `_edge_uid` (verified 0.10.2: auto-ids continue the
    /// pre-clear sequence).
    pub fn clear(&mut self) {
        self.assert_not_frozen();
        self.inner = StableDiGraph::new();
        self.agent_ids.clear();
        self.hyperedge_ids.clear();
        self.graph_attrs.clear();
    }

    /// Remove all edges, keeping nodes.
    ///
    /// XGI parity: `H.clear_edges()` — the edge-uid counter is NOT reset
    /// (XGI's `clear_edges` never touches `_edge_uid`, verified 0.10.2).
    pub fn clear_edges(&mut self) {
        self.assert_not_frozen();
        let hyperedge_indices: Vec<NodeIndex> = self.hyperedge_ids.values().copied().collect();
        for he_idx in hyperedge_indices {
            self.inner.remove_node(he_idx);
        }
        self.hyperedge_ids.clear();
    }

    /// The id the next edge added without an explicit id will get.
    /// XGI parity: `H._edge_uid`.
    pub fn next_edge_id(&self) -> u64 {
        self.edge_uid_counter
    }

    /// Raise the counter past `idx`: if `next_edge_id() <= idx`, it becomes
    /// `idx + 1`. XGI parity: `update_uid_counter`. Not a structural change,
    /// so it is allowed on a frozen hypergraph. At `idx == u64::MAX` the
    /// counter saturates there instead of overflowing.
    pub fn update_uid_counter(&mut self, idx: u64) {
        if self.edge_uid_counter <= idx {
            self.edge_uid_counter = idx.saturating_add(1);
        }
    }

    /// Freeze the hypergraph, preventing modification.
    /// XGI parity: `H.freeze()`.
    pub fn freeze(&mut self) {
        self.frozen = true;
    }

    /// Check if the hypergraph is frozen.
    /// XGI parity: `H.is_frozen`.
    pub fn is_frozen(&self) -> bool {
        self.frozen
    }

    /// Return an independent deep copy. XGI parity: `H.copy()`.
    /// D13: the copy carries `frozen`, unlike XGI's `copy()` — XGI's
    /// freeze is per-instance method-swizzling that a fresh instance
    /// never inherits, but the core's `frozen` is data, so the safety
    /// property must propagate.
    pub fn copy(&self) -> Self
    where
        N: Clone,
        E: Clone,
        M: Clone,
    {
        Self {
            inner: self.inner.clone(),
            agent_ids: self.agent_ids.clone(),
            hyperedge_ids: self.hyperedge_ids.clone(),
            edge_uid_counter: self.edge_uid_counter,
            graph_attrs: self.graph_attrs.clone(),
            frozen: self.frozen,
        }
    }

    /// Add multiple nodes. XGI parity: `H.add_nodes_from(nodes_for_adding)`.
    pub fn add_nodes_from(&mut self, nodes: impl IntoIterator<Item = (String, N)>) {
        for (node_id, attrs) in nodes {
            self.add_node(&node_id, attrs);
        }
    }

    /// Add multiple edges. Returns a result per edge.
    pub fn add_edges_from(
        &mut self,
        edges: impl IntoIterator<Item = (Vec<String>, Option<String>, E)>,
    ) -> Vec<Result<String, EdgeError>>
    where
        N: Default,
        M: Default + Clone,
    {
        edges
            .into_iter()
            .map(|(m, i, a)| self.add_edge(m, i, a))
            .collect()
    }

    /// Add a node to an existing edge. Auto-creates both if missing.
    /// XGI parity: `H.add_node_to_edge(edge, node)`.
    pub fn add_node_to_edge(&mut self, edge_id: &str, node_id: &str) -> Result<(), EdgeError>
    where
        N: Default,
        E: Default,
        M: Default + Clone,
    {
        self.assert_not_frozen();
        self.ensure_edge_and_node(edge_id, node_id);
        // Add the membership edges (both directions for undirected)
        let agent_idx = self.agent_ids[node_id];
        let he_idx = self.hyperedge_ids[edge_id];
        // Check if membership already exists
        if self.inner.find_edge(agent_idx, he_idx).is_none() {
            let membership = MembershipEdge {
                member_data: M::default(),
            };
            self.inner.add_edge(agent_idx, he_idx, membership.clone());
            self.inner.add_edge(he_idx, agent_idx, membership);
        }
        Ok(())
    }

    /// Look up both ends of a membership, naming what is missing.
    fn resolve_membership(
        &self,
        edge_id: &str,
        node_id: &str,
    ) -> Result<(NodeIndex, NodeIndex), MembershipError> {
        let he_idx =
            *self
                .hyperedge_ids
                .get(edge_id)
                .ok_or_else(|| MembershipError::EdgeNotFound {
                    edge_id: edge_id.to_string(),
                })?;
        let agent_idx =
            *self
                .agent_ids
                .get(node_id)
                .ok_or_else(|| MembershipError::NodeNotFound {
                    node_id: node_id.to_string(),
                })?;
        if self.inner.find_edge(agent_idx, he_idx).is_none() {
            return Err(MembershipError::NotAMember {
                node_id: node_id.to_string(),
                edge_id: edge_id.to_string(),
            });
        }
        Ok((agent_idx, he_idx))
    }

    /// The value the membership of `node_id` in `edge_id` carries. `None` when
    /// the edge or the node does not exist, or the node is not a member of the
    /// edge.
    ///
    /// A membership made by any constructor carries `M::default()` until a
    /// value is written with [`Self::set_member_data`] or
    /// [`Self::add_node_to_edge_with_data`]. XGI has no value on a membership,
    /// so the XGI-compatible formats do not carry it.
    ///
    /// ```
    /// use hypergraph_rs::Hypergraph;
    ///
    /// let mut h: Hypergraph<(), (), u64> = Hypergraph::new();
    /// h.add_edge(vec!["a".into(), "b".into()], Some("e".into()), ()).unwrap();
    /// assert_eq!(h.member_data("e", "a"), Some(&0));
    /// assert_eq!(h.member_data("e", "nobody"), None);
    /// ```
    pub fn member_data(&self, edge_id: &str, node_id: &str) -> Option<&M> {
        let (agent_idx, he_idx) = self.resolve_membership(edge_id, node_id).ok()?;
        let edge = self.inner.find_edge(agent_idx, he_idx)?;
        self.inner.edge_weight(edge).map(|m| &m.member_data)
    }

    /// Replace that value. Refuses an unknown edge, an unknown node, and a
    /// node that is not a member. Both stored directions of the membership
    /// receive the value. Panics on a frozen hypergraph, like every mutator.
    ///
    /// ```
    /// use hypergraph_rs::{Hypergraph, MembershipError};
    ///
    /// let mut h: Hypergraph<(), (), u64> = Hypergraph::new();
    /// h.add_edge(vec!["a".into(), "b".into()], Some("e".into()), ()).unwrap();
    /// h.set_member_data("e", "a", 7).unwrap();
    /// assert_eq!(h.member_data("e", "a"), Some(&7));
    /// assert_eq!(h.member_data("e", "b"), Some(&0));
    /// assert!(matches!(
    ///     h.set_member_data("e", "zed", 1),
    ///     Err(MembershipError::NodeNotFound { .. })
    /// ));
    /// ```
    pub fn set_member_data(
        &mut self,
        edge_id: &str,
        node_id: &str,
        data: M,
    ) -> Result<(), MembershipError>
    where
        M: Clone,
    {
        self.assert_not_frozen();
        let (agent_idx, he_idx) = self.resolve_membership(edge_id, node_id)?;
        self.write_member_data(agent_idx, he_idx, data);
        Ok(())
    }

    /// Write `data` to both stored directions of an existing membership.
    fn write_member_data(&mut self, agent_idx: NodeIndex, he_idx: NodeIndex, data: M)
    where
        M: Clone,
    {
        if let Some(e) = self.inner.find_edge(he_idx, agent_idx) {
            if let Some(w) = self.inner.edge_weight_mut(e) {
                w.member_data = data.clone();
            }
        }
        if let Some(e) = self.inner.find_edge(agent_idx, he_idx) {
            if let Some(w) = self.inner.edge_weight_mut(e) {
                w.member_data = data;
            }
        }
    }

    /// The members of an edge with their values, in the order of
    /// [`Self::members`]. `None` when the edge does not exist.
    ///
    /// ```
    /// use hypergraph_rs::Hypergraph;
    ///
    /// let mut h: Hypergraph<(), (), u64> = Hypergraph::new();
    /// h.add_edge(vec!["a".into(), "b".into()], Some("e".into()), ()).unwrap();
    /// h.set_member_data("e", "b", 2).unwrap();
    /// assert_eq!(
    ///     h.members_with_data("e"),
    ///     Some(vec![("a".to_string(), &0), ("b".to_string(), &2)])
    /// );
    /// ```
    pub fn members_with_data(&self, edge_id: &str) -> Option<Vec<(String, &M)>> {
        let he_idx = *self.hyperedge_ids.get(edge_id)?;
        let mut neighbor_idxs: Vec<_> = self.inner.neighbors(he_idx).collect();
        neighbor_idxs.reverse();
        let mut result = Vec::new();
        for neighbor_idx in neighbor_idxs {
            if let Some(NodeKind::Agent(_)) = self.inner.node_weight(neighbor_idx) {
                // A neighbour that cannot be resolved is skipped, exactly as
                // `members()` skips it, so the two listings never disagree.
                let Some(id) = self.agent_ids.id_of(neighbor_idx) else {
                    continue;
                };
                let Some(edge) = self.inner.find_edge(neighbor_idx, he_idx) else {
                    continue;
                };
                let Some(weight) = self.inner.edge_weight(edge) else {
                    continue;
                };
                result.push((id.to_string(), &weight.member_data));
            }
        }
        Some(result)
    }

    /// [`Self::add_node_to_edge`], with the value the new membership carries.
    /// Auto-creates the edge and the node if missing. If the node is already a
    /// member, its value is replaced.
    ///
    /// ```
    /// use hypergraph_rs::Hypergraph;
    ///
    /// let mut h: Hypergraph<(), (), u64> = Hypergraph::new();
    /// h.add_node_to_edge_with_data("e", "a", 5).unwrap();
    /// assert_eq!(h.member_data("e", "a"), Some(&5));
    /// h.add_node_to_edge_with_data("e", "a", 9).unwrap();
    /// assert_eq!(h.member_data("e", "a"), Some(&9));
    /// ```
    pub fn add_node_to_edge_with_data(
        &mut self,
        edge_id: &str,
        node_id: &str,
        data: M,
    ) -> Result<(), EdgeError>
    where
        N: Default,
        E: Default,
        M: Clone,
    {
        self.assert_not_frozen();
        self.ensure_edge_and_node(edge_id, node_id);
        let agent_idx = self.agent_ids[node_id];
        let he_idx = self.hyperedge_ids[edge_id];
        if self.inner.find_edge(agent_idx, he_idx).is_none() {
            let membership = MembershipEdge {
                member_data: data.clone(),
            };
            self.inner.add_edge(agent_idx, he_idx, membership.clone());
            self.inner.add_edge(he_idx, agent_idx, membership);
        } else {
            self.write_member_data(agent_idx, he_idx, data);
        }
        Ok(())
    }

    /// Create the edge and the node when missing (default attributes), and
    /// keep the auto-id counter ahead of a numeric edge id.
    fn ensure_edge_and_node(&mut self, edge_id: &str, node_id: &str)
    where
        N: Default,
        E: Default,
    {
        if !self.hyperedge_ids.contains_key(edge_id) {
            let he_idx = self.inner.add_node(NodeKind::Hyperedge(E::default()));
            self.hyperedge_ids.insert(edge_id.to_string(), he_idx);
            if let Ok(n) = edge_id.parse::<u64>() {
                if n >= self.edge_uid_counter {
                    self.edge_uid_counter = n.saturating_add(1);
                }
            }
        }
        if !self.agent_ids.contains_key(node_id) {
            let nidx = self.inner.add_node(NodeKind::Agent(N::default()));
            self.agent_ids.insert(node_id.to_string(), nidx);
        }
    }

    /// Remove a node from an existing edge.
    ///
    /// XGI parity: `H.remove_node_from_edge(edge, node, remove_empty=True)`.
    /// XGI raises `XGIError` for a missing edge, a missing node, or a node
    /// that exists but is not a member of the named edge — the core returns
    /// the matching [`MembershipError`] variant so callers discriminate
    /// without string-matching; the PyO3 binding translates `Err` -> raise,
    /// reproducing XGI's exact per-branch messages (D2 error channel).
    pub fn remove_node_from_edge(
        &mut self,
        edge_id: &str,
        node_id: &str,
        remove_empty: bool,
    ) -> Result<(), MembershipError> {
        self.assert_not_frozen();
        let he_idx = *self
            .hyperedge_ids
            .get(edge_id)
            .ok_or(MembershipError::EdgeNotFound {
                edge_id: edge_id.to_string(),
            })?;
        let agent_idx = *self
            .agent_ids
            .get(node_id)
            .ok_or(MembershipError::NodeNotFound {
                node_id: node_id.to_string(),
            })?;

        // Check if the node is actually in the edge
        let edge_to_he = self.inner.find_edge(agent_idx, he_idx);
        let edge_from_he = self.inner.find_edge(he_idx, agent_idx);
        if edge_to_he.is_none() {
            return Err(MembershipError::NotAMember {
                node_id: node_id.to_string(),
                edge_id: edge_id.to_string(),
            });
        }

        // Remove the bipartite membership edges
        if let Some(e) = edge_to_he {
            self.inner.remove_edge(e);
        }
        if let Some(e) = edge_from_he {
            self.inner.remove_edge(e);
        }

        // Check if edge is now empty
        if remove_empty {
            let has_members = self
                .inner
                .neighbors(he_idx)
                .any(|n| matches!(self.inner.node_weight(n), Some(NodeKind::Agent(_))));
            if !has_members {
                self.inner.remove_node(he_idx);
                self.hyperedge_ids.shift_remove(edge_id);
            }
        }
        Ok(())
    }

    /// Remove multiple edges, returning a result per ATTEMPTED edge.
    ///
    /// XGI parity: `H.remove_edges_from(ebunch)` — XGI iterates in order
    /// and RAISES on the first missing id, so ids after it are never
    /// attempted (partial effects: ids before the failure are already
    /// removed). The core mirrors this: stop on the first `Err`.
    pub fn remove_edges_from(&mut self, edge_ids: &[String]) -> Vec<Result<(), EdgeError>> {
        let mut results = Vec::new();
        for eid in edge_ids {
            let result = self.remove_edge(eid);
            let stop = result.is_err();
            results.push(result);
            if stop {
                break;
            }
        }
        results
    }

    /// Remove multiple nodes, returning a result per item.
    ///
    /// XGI parity: `H.remove_nodes_from(nodes)` — XGI warns and skips a
    /// missing id, then CONTINUES; the core records a per-item `Err` and
    /// continues (D2-class channel translation — the binding warns per
    /// `Err` item).
    pub fn remove_nodes_from(
        &mut self,
        node_ids: &[String],
        strong: bool,
        remove_empty: bool,
    ) -> Vec<Result<(), NodeError>> {
        node_ids
            .iter()
            .map(|nid| self.remove_node(nid, strong, remove_empty))
            .collect()
    }

    /// Add nodes then edges. XGI parity: `H.update(edges=, nodes=)`
    /// (hypergraph.py:1210-1229).
    pub fn update(&mut self, nodes: Vec<(String, N)>, edges: Vec<(Vec<String>, Option<String>, E)>)
    where
        N: Default,
        M: Default + Clone,
    {
        self.add_nodes_from(nodes);
        let _ = self.add_edges_from(edges);
    }

    /// Node ids with zero memberships, in insertion order.
    /// XGI parity: `H.nodes.isolates()` (views.py:632-667, default).
    pub fn isolate_node_ids(&self) -> Vec<String> {
        self.node_ids()
            .into_iter()
            .filter(|n| self.memberships(n).is_some_and(|m| m.is_empty()))
            .collect()
    }

    /// Edge ids with exactly one member, in insertion order.
    /// XGI parity: `H.edges.singletons()` (views.py:749-761).
    pub fn singleton_edge_ids(&self) -> Vec<String> {
        self.edge_ids()
            .into_iter()
            .filter(|e| self.members(e).is_some_and(|m| m.len() == 1))
            .collect()
    }

    /// Read-only view of the graph-level attribute dict (XGI's `H.graph`).
    pub fn graph_attrs(&self) -> &serde_json::Map<String, serde_json::Value> {
        &self.graph_attrs
    }

    /// Attach `node_id` to `edge_id` (both bipartite directions). No-op if the
    /// membership already exists. Both ids must already exist.
    fn attach_member(&mut self, edge_id: &str, node_id: &str)
    where
        M: Default + Clone,
    {
        let a = self.agent_ids[node_id];
        let he = self.hyperedge_ids[edge_id];
        if self.inner.find_edge(a, he).is_none() {
            let m = MembershipEdge {
                member_data: M::default(),
            };
            self.inner.add_edge(a, he, m.clone());
            self.inner.add_edge(he, a, m);
        }
    }

    /// Detach `node_id` from `edge_id` (both bipartite directions). No-op if
    /// the membership is absent.
    fn detach_member(&mut self, edge_id: &str, node_id: &str) {
        let a = self.agent_ids[node_id];
        let he = self.hyperedge_ids[edge_id];
        if let Some(e) = self.inner.find_edge(a, he) {
            self.inner.remove_edge(e);
        }
        if let Some(e) = self.inner.find_edge(he, a) {
            self.inner.remove_edge(e);
        }
    }

    /// Swap the edge memberships of two nodes between two edges.
    /// XGI parity: `H.double_edge_swap(n1, n2, e1, e2)` (hypergraph.py:918-992).
    pub fn double_edge_swap(
        &mut self,
        n_id1: &str,
        n_id2: &str,
        e_id1: &str,
        e_id2: &str,
    ) -> Result<(), SwapError>
    where
        M: Default + Clone,
    {
        self.assert_not_frozen();

        let members1 = self.members(e_id1).ok_or(SwapError::IdNotFound {
            id: e_id1.to_string(),
        })?;
        let members2 = self.members(e_id2).ok_or(SwapError::IdNotFound {
            id: e_id2.to_string(),
        })?;
        if !members1.iter().any(|m| m == n_id1) {
            return Err(SwapError::IdNotFound {
                id: n_id1.to_string(),
            });
        }
        if !members2.iter().any(|m| m == n_id2) {
            return Err(SwapError::IdNotFound {
                id: n_id2.to_string(),
            });
        }

        // Compute the post-swap member sets and verify size preservation BEFORE
        // mutating (XGI hypergraph.py:980-986).
        use std::collections::BTreeSet;
        let new1: BTreeSet<String> = members1
            .iter()
            .filter(|m| *m != n_id1)
            .cloned()
            .chain(std::iter::once(n_id2.to_string()))
            .collect();
        let new2: BTreeSet<String> = members2
            .iter()
            .filter(|m| *m != n_id2)
            .cloned()
            .chain(std::iter::once(n_id1.to_string()))
            .collect();
        if new1.len() != members1.len() || new2.len() != members2.len() {
            return Err(SwapError::SizeNotPreserved);
        }

        // Commit: e1 loses n1 gains n2; e2 loses n2 gains n1.
        self.detach_member(e_id1, n_id1);
        self.attach_member(e_id1, n_id2);
        self.detach_member(e_id2, n_id2);
        self.attach_member(e_id2, n_id1);
        Ok(())
    }

    /// Randomly redistribute nodes between two hyperedges, preserving both
    /// sizes. XGI parity: `H.random_edge_shuffle(e1, e2, seed)`
    /// (hypergraph.py:994-1078). Requires an explicit seed (deviation #3);
    /// the rand_pcg sequence does not match numpy, so only structural
    /// invariants are guaranteed across implementations.
    ///
    /// Gated on `generators` (the feature that owns the `rand_pcg`/`rand_core`
    /// optional deps this method needs) so the core module stays buildable
    /// under `--no-default-features` (Phase 4 Task 19 feature-matrix check).
    #[cfg(feature = "generators")]
    pub fn random_edge_shuffle(
        &mut self,
        e_id1: Option<&str>,
        e_id2: Option<&str>,
        seed: u64,
    ) -> Result<(), EdgeError>
    where
        M: Default + Clone,
    {
        self.assert_not_frozen();
        if self.hyperedge_ids.len() < 2 {
            return Err(EdgeError::TooFewEdges);
        }
        let mut rng = Pcg64Mcg::seed_from_u64(seed);

        // Resolve the two edge ids (given, or two distinct random).
        let (id1, id2) = match (e_id1, e_id2) {
            (Some(a), Some(b)) => (a.to_string(), b.to_string()),
            _ => {
                let ids = self.edge_ids();
                let i = rng.random_range(0..ids.len());
                let mut j = rng.random_range(0..ids.len() - 1);
                if j >= i {
                    j += 1;
                }
                (ids[i].clone(), ids[j].clone())
            }
        };

        use std::collections::BTreeSet;
        let s1: BTreeSet<String> = self
            .members(&id1)
            .ok_or(EdgeError::NotFound {
                edge_id: id1.clone(),
            })?
            .into_iter()
            .collect();
        let s2: BTreeSet<String> = self
            .members(&id2)
            .ok_or(EdgeError::NotFound {
                edge_id: id2.clone(),
            })?
            .into_iter()
            .collect();

        let both: BTreeSet<String> = s1.intersection(&s2).cloned().collect();
        let only1: BTreeSet<String> = s1.difference(&both).cloned().collect();
        let only2: BTreeSet<String> = s2.difference(&both).cloned().collect();

        // Bucket in a deterministic base order (BTreeSet -> sorted).
        let mut bucket: Vec<String> = only1.union(&only2).cloned().collect();
        let k = only1.len();
        let n = bucket.len();
        // Partial Fisher-Yates: bounded by k (computed above), never exceeds n.
        for i in 0..k {
            let j = i + rng.random_range(0..(n - i));
            bucket.swap(i, j);
        }
        let new_only1: BTreeSet<String> = bucket[..k].iter().cloned().collect();
        let new_e1: BTreeSet<String> = new_only1.union(&both).cloned().collect();
        let new_only2: BTreeSet<String> = bucket[k..].iter().cloned().collect();
        let new_e2: BTreeSet<String> = new_only2.union(&both).cloned().collect();

        // Rewire: detach all old members, attach the new ones (ids/attrs kept).
        for node in &s1 {
            self.detach_member(&id1, node);
        }
        for node in &s2 {
            self.detach_member(&id2, node);
        }
        for node in &new_e1 {
            self.attach_member(&id1, node);
        }
        for node in &new_e2 {
            self.attach_member(&id2, node);
        }
        Ok(())
    }

    /// The undirected 1-skeleton (pairwise projection): a node for every agent
    /// (weight = its id, added in `node_ids()` order so `NodeIndex::new(i)`
    /// corresponds to `node_ids()[i]`), and an edge (weight `1.0`) between two
    /// agents that co-occur in at least one hyperedge.
    ///
    /// Undirected (deviation from spec §4.1's `Graph`): the connectivity,
    /// shortest-path, and clustering algorithms operate on this graph.
    pub fn skeleton(&self) -> UnGraph<String, f64> {
        let mut g = UnGraph::<String, f64>::new_undirected();
        let node_ids = self.node_ids();
        let mut idx_of: indexmap::IndexMap<String, GraphNodeIndex> = indexmap::IndexMap::new();
        for n in &node_ids {
            let ix = g.add_node(n.clone());
            idx_of.insert(n.clone(), ix);
        }
        let mut seen: std::collections::HashSet<(usize, usize)> = std::collections::HashSet::new();
        for e in self.edge_ids() {
            let members = self.members(&e).unwrap_or_default();
            // Bounded: |members| choose 2.
            for a in 0..members.len() {
                for b in (a + 1)..members.len() {
                    let ia = idx_of[&members[a]];
                    let ib = idx_of[&members[b]];
                    let key = if g.to_index(ia) < g.to_index(ib) {
                        (g.to_index(ia), g.to_index(ib))
                    } else {
                        (g.to_index(ib), g.to_index(ia))
                    };
                    if seen.insert(key) {
                        g.add_edge(ia, ib, 1.0);
                    }
                }
            }
        }
        g
    }

    /// The neighbours of a node: all nodes that share at least one hyperedge
    /// with it, excluding itself, in insertion order (deduped). `None` if the
    /// node is absent. XGI parity: `H.nodes.neighbors(n)`.
    pub fn node_neighbors(&self, node_id: &str) -> Option<Vec<String>> {
        let edges = self.memberships(node_id)?;
        let mut result: Vec<String> = Vec::new();
        let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();
        for e in edges {
            for m in self.members(&e).unwrap_or_default() {
                if m != node_id && seen.insert(m.clone()) {
                    result.push(m);
                }
            }
        }
        Some(result)
    }

    /// The degree of a node = number of edges it belongs to. With
    /// `order = Some(d)`, counts only edges of that order (size `d+1`).
    /// `None` if the node is absent. XGI parity: `H.degree(n, order)`.
    pub fn node_degree(&self, node_id: &str, order: Option<usize>) -> Option<usize> {
        let edges = self.memberships(node_id)?;
        Some(match order {
            None => edges.len(),
            Some(d) => edges
                .iter()
                .filter(|e| self.edge_size(e) == Some(d + 1))
                .count(),
        })
    }

    /// The size (number of members) of an edge. `None` if the edge is absent.
    pub fn edge_size(&self, edge_id: &str) -> Option<usize> {
        self.members(edge_id).map(|m| m.len())
    }
}

impl<M> Hypergraph<serde_json::Value, serde_json::Value, M> {
    /// Set node attributes from (id, attr_map) pairs.
    /// XGI parity: `H.set_node_attributes(values, name=None)`.
    /// Silently skips missing node IDs (XGI warns).
    pub fn set_node_attributes(
        &mut self,
        values: impl IntoIterator<Item = (String, serde_json::Map<String, serde_json::Value>)>,
    ) {
        for (node_id, attrs) in values {
            if let Some(node_attrs) = self.node_attrs_mut(&node_id) {
                if !node_attrs.is_object() {
                    *node_attrs = serde_json::Value::Object(serde_json::Map::new());
                }
                if let Some(obj) = node_attrs.as_object_mut() {
                    for (k, v) in attrs {
                        obj.insert(k, v);
                    }
                }
            }
            // Silently skip missing nodes (XGI warns)
        }
    }

    /// Set edge attributes from (id, attr_map) pairs.
    /// XGI parity: `H.set_edge_attributes(values, name=None)`.
    /// Silently skips missing edge IDs (XGI warns).
    pub fn set_edge_attributes(
        &mut self,
        values: impl IntoIterator<Item = (String, serde_json::Map<String, serde_json::Value>)>,
    ) {
        for (edge_id, attrs) in values {
            if let Some(edge_attrs) = self.edge_attrs_mut(&edge_id) {
                if !edge_attrs.is_object() {
                    *edge_attrs = serde_json::Value::Object(serde_json::Map::new());
                }
                if let Some(obj) = edge_attrs.as_object_mut() {
                    for (k, v) in attrs {
                        obj.insert(k, v);
                    }
                }
            }
        }
    }
}

impl Hypergraph<Value, Value, Value> {
    /// The dual: nodes become edges and edges become nodes.
    /// XGI parity: `H.dual()` (hypergraph.py:1446-1466). Isolate nodes are
    /// dropped (they would map to empty dual-edges, which our strict add_edge
    /// cannot represent — deviation #8).
    pub fn dual(&self) -> Self {
        let mut d = Self::new();

        // Original edges -> dual nodes (id = edge id, attrs = edge attrs).
        // Done FIRST so their attrs survive the auto-node-creation in add_edge.
        for eid in self.edge_ids() {
            let attrs = self.edge_attrs(&eid).cloned().unwrap_or(Value::Null);
            d.add_node(&eid, attrs);
        }

        // Original nodes -> dual edges (id = node id, members = memberships).
        for nid in self.node_ids() {
            let memberships = self.memberships(&nid).unwrap_or_default();
            if memberships.is_empty() {
                continue; // isolate node -> empty dual-edge -> skipped
            }
            let attrs = self.node_attrs(&nid).cloned().unwrap_or(Value::Null);
            let _ = d.add_edge(memberships, Some(nid.clone()), attrs);
        }

        // Graph attrs.
        for (k, val) in self.graph_attrs().clone() {
            d.set_graph_attr(&k, val);
        }
        d
    }
}

impl Hypergraph<Value, Value, Value> {
    /// Merge edges that have identical member-sets.
    /// XGI parity: `H.merge_duplicate_edges(rename, merge_rule, multiplicity)`
    /// (hypergraph.py:1257-1419). `rename="tuple"` is deferred (deviation #6);
    /// union produces a JSON array, not a Python set.
    pub fn merge_duplicate_edges(
        &mut self,
        rename: MergeRename,
        merge_rule: MergeRule,
        multiplicity: Option<&str>,
    ) {
        self.assert_not_frozen();
        use std::collections::BTreeMap;

        // Group edge ids by their sorted member-set (deterministic).
        let mut groups: BTreeMap<Vec<String>, Vec<String>> = BTreeMap::new();
        for eid in self.edge_ids() {
            let mut mem = self.members(&eid).unwrap_or_default();
            mem.sort();
            groups.entry(mem).or_default().push(eid);
        }

        let mut to_remove: Vec<String> = Vec::new();
        let mut to_add: Vec<(Vec<String>, String, Value)> = Vec::new();
        for (members, dup_ids) in &groups {
            if dup_ids.len() <= 1 {
                continue;
            }
            to_remove.extend(dup_ids.iter().cloned());
            let new_id = match rename {
                MergeRename::First => {
                    let mut sorted = dup_ids.clone();
                    sorted.sort();
                    sorted[0].clone()
                }
                MergeRename::New => {
                    let id = self.edge_uid_counter.to_string();
                    self.edge_uid_counter = self.edge_uid_counter.saturating_add(1);
                    id
                }
            };
            let mut new_attrs = self.merged_edge_attrs(dup_ids, merge_rule);
            if let Some(mkey) = multiplicity {
                // merged_edge_attrs(MergeRule::First) can yield a non-object
                // (e.g. Value::Null, our default empty attrs) — coerce before
                // inserting the multiplicity key (2026-07-19 preflight defect
                // P2-D1: Null.as_object_mut() silently dropped the insert).
                if !new_attrs.is_object() {
                    new_attrs = serde_json::json!({});
                }
                new_attrs
                    .as_object_mut()
                    .unwrap()
                    .insert(mkey.to_string(), serde_json::json!(dup_ids.len()));
            }
            to_add.push((members.clone(), new_id, new_attrs));
        }

        for eid in &to_remove {
            let _ = self.remove_edge(eid);
        }
        for (members, id, attrs) in to_add {
            let _ = self.add_edge(members, Some(id), attrs);
        }
    }

    /// Combine the attrs of a group of duplicate edges per `merge_rule`.
    fn merged_edge_attrs(&self, dup_ids: &[String], rule: MergeRule) -> Value {
        match rule {
            MergeRule::First => {
                let min_id = dup_ids.iter().min().cloned().unwrap_or_default();
                self.edge_attrs(&min_id)
                    .cloned()
                    .unwrap_or_else(|| serde_json::json!({}))
            }
            MergeRule::Union | MergeRule::Intersection => {
                use std::collections::BTreeSet;
                let mut keys: BTreeSet<String> = BTreeSet::new();
                for id in dup_ids {
                    if let Some(obj) = self.edge_attrs(id).and_then(|a| a.as_object()) {
                        keys.extend(obj.keys().cloned());
                    }
                }
                let mut out = serde_json::Map::new();
                for k in keys {
                    let mut distinct: Vec<Value> = Vec::new();
                    for id in dup_ids {
                        let val = self
                            .edge_attrs(id)
                            .and_then(|a| a.get(&k))
                            .cloned()
                            .unwrap_or(Value::Null);
                        if !distinct.contains(&val) {
                            distinct.push(val);
                        }
                    }
                    let merged = match rule {
                        MergeRule::Union => Value::Array(distinct),
                        _ => {
                            if distinct.len() == 1 {
                                distinct.into_iter().next().unwrap()
                            } else {
                                Value::Null
                            }
                        }
                    };
                    out.insert(k, merged);
                }
                Value::Object(out)
            }
        }
    }

    /// Remove undesirable artifacts in place. XGI parity: `H.cleanup(...)`
    /// (hypergraph.py). `connected` and `relabel` are not part of the core
    /// (the Python binding does them). In order: merge duplicate edges,
    /// remove singleton edges, remove empty edges, remove isolated nodes.
    /// Each flag in [`CleanupOptions`] keeps that kind; the default removes
    /// all of them.
    pub fn cleanup(&mut self, options: CleanupOptions) {
        self.assert_not_frozen();
        if !options.multiedges {
            self.merge_duplicate_edges(MergeRename::First, MergeRule::First, None);
        }
        if !options.singletons {
            let s = self.singleton_edge_ids();
            let _ = self.remove_edges_from(&s);
        }
        if !options.empties {
            let e: Vec<String> = self
                .edge_ids()
                .into_iter()
                .filter(|id| self.members(id).is_some_and(|m| m.is_empty()))
                .collect();
            let _ = self.remove_edges_from(&e);
        }
        if !options.isolates {
            let i = self.isolate_node_ids();
            let _ = self.remove_nodes_from(&i, false, true);
        }
    }

    /// Return a cleaned copy, leaving `self` untouched.
    pub fn cleaned(&self, options: CleanupOptions) -> Self {
        let mut c = self.copy();
        c.cleanup(options);
        c
    }
}

impl<N: PartialEq, E: PartialEq, M: PartialEq> PartialEq for Hypergraph<N, E, M> {
    /// Two hypergraphs are equal if they have the same nodes, edges,
    /// memberships, and attributes. XGI parity: `H1 == H2`.
    fn eq(&self, other: &Self) -> bool {
        if self.agent_ids.len() != other.agent_ids.len() {
            return false;
        }
        for (nid, _) in &self.agent_ids {
            match (self.node_attrs(nid), other.node_attrs(nid)) {
                (Some(a), Some(b)) if a == b => {}
                _ => return false,
            }
        }
        if self.hyperedge_ids.len() != other.hyperedge_ids.len() {
            return false;
        }
        for (eid, _) in &self.hyperedge_ids {
            match (self.edge_attrs(eid), other.edge_attrs(eid)) {
                (Some(a), Some(b)) if a == b => {}
                _ => return false,
            }
            let mut m1 = self.members(eid).unwrap_or_default();
            let mut m2 = other.members(eid).unwrap_or_default();
            m1.sort();
            m2.sort();
            if m1 != m2 {
                return false;
            }
        }
        self.graph_attrs == other.graph_attrs
    }
}

impl<N, E, M> std::fmt::Debug for Hypergraph<N, E, M>
where
    N: std::fmt::Debug,
    E: std::fmt::Debug,
    M: std::fmt::Debug,
{
    /// XGI parity: `__repr__` returns `Hypergraph([{members}, ...])`.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Hypergraph(")?;
        let edges: Vec<String> = self
            .hyperedge_ids
            .keys()
            .map(|eid| {
                let members = self.members(eid).unwrap_or_default();
                format!("{{{}}}", members.join(", "))
            })
            .collect();
        write!(f, "[{}])", edges.join(", "))
    }
}