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
//! The simplicial complex data structure.
//!
//! A `SimplicialComplex` composes a [`Hypergraph`] and enforces closure under
//! faces: adding a simplex also adds every missing subface of size >= 2. A
//! lazily-computed [`FaceLattice`] caches the immediate-face relation; it is
//! invalidated on every mutation. XGI parity: `xgi.SimplicialComplex`
//! (simplicialcomplex.py).

use std::cell::OnceCell;
use std::collections::{BTreeMap, BTreeSet};

use super::error::SimplexError;
use super::hypergraph::Hypergraph;

/// The cached immediate-face relation: each simplex id maps to the ids of the
/// codimension-1 simplices present in the complex that are its faces.
#[derive(Debug, Clone, Default)]
pub struct FaceLattice {
    /// simplex id -> ids of its codim-1 faces present in the complex.
    pub immediate_faces: BTreeMap<String, Vec<String>>,
}

/// A simplicial complex (undirected, closed under faces).
pub struct SimplicialComplex<N = serde_json::Value, E = serde_json::Value, M = serde_json::Value> {
    inner: Hypergraph<N, E, M>,
    face_lattice: OnceCell<FaceLattice>,
}

/// Deduplicate a Vec of ids into a sorted set-vector (deterministic order).
fn as_sorted_set(items: &[String]) -> Vec<String> {
    let set: BTreeSet<String> = items.iter().cloned().collect();
    set.into_iter().collect()
}

/// All subsets of `set` of sizes from `set.len()-1` down to 2 (never
/// singletons). XGI parity: `_subfaces` (simplicialcomplex.py:334-361).
fn subfaces(set: &[String]) -> Vec<Vec<String>> {
    let k = set.len();
    let mut out: Vec<Vec<String>> = Vec::new();
    // sizes k-1, k-2, ..., 2
    let mut size = k.saturating_sub(1);
    while size >= 2 {
        combinations(set, size, &mut out);
        size -= 1;
    }
    out
}

/// Push every `choose`-sized combination of `set` (sorted input -> sorted
/// combos) into `out`. Bounded by `total = n_choose_k(set.len(), choose)`,
/// computed BEFORE the loop (Power-of-10 rule 2 — 2026-07-19 preflight
/// correction, defect P2-D2: the prior `loop { ... }` form terminated via an
/// internal `return` after C(n,k) iterations, which is provably-but-not
/// *statically* bounded; this form gives a checkable `for` bound instead).
fn combinations(set: &[String], choose: usize, out: &mut Vec<Vec<String>>) {
    let n = set.len();
    if choose == 0 || choose > n {
        return;
    }
    let total = n_choose_k(n, choose);
    // index-based lexicographic combinations; bounded by n choose k.
    let mut idx: Vec<usize> = (0..choose).collect();
    for _ in 0..total {
        out.push(idx.iter().map(|&i| set[i].clone()).collect());
        // Advance to the next combination: find the rightmost index that can
        // still be incremented (bounded by `choose`, known before this
        // sub-loop starts).
        let mut pivot: Option<usize> = None;
        for i in (0..choose).rev() {
            if idx[i] != i + n - choose {
                pivot = Some(i);
                break;
            }
        }
        let i = match pivot {
            Some(i) => i,
            None => return, // exhausted (the final combination was just pushed)
        };
        idx[i] += 1;
        for j in (i + 1)..choose {
            idx[j] = idx[j - 1] + 1;
        }
    }
}

/// `n` choose `k`, via the multiplicative formula (no factorials — this
/// crate's `n` is always small, but the formula avoids any needless overflow
/// risk regardless).
fn n_choose_k(n: usize, k: usize) -> usize {
    if k > n {
        return 0;
    }
    let k = k.min(n - k);
    let mut result: usize = 1;
    for i in 0..k {
        result = result * (n - i) / (i + 1);
    }
    result
}

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

impl<N, E, M> SimplicialComplex<N, E, M> {
    /// Create an empty simplicial complex.
    pub fn new() -> Self {
        Self {
            inner: Hypergraph::new(),
            face_lattice: OnceCell::new(),
        }
    }

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

    /// Raise the counter past `idx` (see [`Hypergraph::update_uid_counter`]).
    pub fn update_uid_counter(&mut self, idx: u64) {
        self.inner.update_uid_counter(idx);
    }

    /// Rebuild a complex from its nodes and its simplices in id order, as
    /// `node_ids()` and `edge_ids()` list them, adding no face: every subface
    /// of size >= 2 of each simplex must be in the list. The first simplex
    /// with a face missing gives `SimplexError::NotClosed { idx }`.
    ///
    /// An entry is skipped, and the first one kept, when its id is already
    /// taken or when its member set is already in the complex: the two cases
    /// in which `add_simplex` is a no-op. No complex built by `add_simplex`
    /// can list such an entry, so a state pickled from a real complex never
    /// loses one here; only a hand-made list can.
    pub fn from_closed_simplices(
        nodes: Vec<(String, N)>,
        simplices: Vec<(String, Vec<String>, E)>,
    ) -> Result<Self, SimplexError>
    where
        N: Default,
        E: Default,
        M: Default + Clone,
    {
        let present: BTreeSet<Vec<String>> = simplices
            .iter()
            .map(|(_, members, _)| as_sorted_set(members))
            .collect();
        for (id, members, _) in &simplices {
            let set = as_sorted_set(members);
            if subfaces(&set)
                .iter()
                .any(|face| !face.is_empty() && !present.contains(face))
            {
                return Err(SimplexError::NotClosed { idx: id.clone() });
            }
        }
        let mut complex = Self::new();
        complex.add_nodes_from(nodes);
        let mut added: std::collections::HashSet<Vec<String>> = std::collections::HashSet::new();
        for (id, members, attrs) in simplices {
            let set = as_sorted_set(&members);
            if added.contains(&set) || complex.inner.has_edge(&id) {
                continue;
            }
            added.insert(set.clone());
            complex
                .inner
                .add_edge(set, Some(id), attrs)
                .expect("the id was checked free just above");
        }
        Ok(complex)
    }

    /// Number of nodes. XGI parity: `S.num_nodes`.
    pub fn num_nodes(&self) -> usize {
        self.inner.num_nodes()
    }

    /// Number of simplices. XGI parity: `S.num_edges`.
    pub fn num_edges(&self) -> usize {
        self.inner.num_edges()
    }

    /// Whether a node exists.
    pub fn has_node(&self, node_id: &str) -> bool {
        self.inner.has_node(node_id)
    }

    /// Whether a simplex id exists.
    pub fn has_edge(&self, edge_id: &str) -> bool {
        self.inner.has_edge(edge_id)
    }

    /// The members of a simplex (by id).
    pub fn members(&self, edge_id: &str) -> Option<Vec<String>> {
        self.inner.members(edge_id)
    }

    /// A node's attributes.
    pub fn node_attrs(&self, node_id: &str) -> Option<&N> {
        self.inner.node_attrs(node_id)
    }

    /// A simplex's attributes.
    pub fn edge_attrs(&self, edge_id: &str) -> Option<&E> {
        self.inner.edge_attrs(edge_id)
    }

    /// All node ids (insertion order).
    pub fn node_ids(&self) -> Vec<String> {
        self.inner.node_ids()
    }

    /// Add a node (no simplices). XGI parity: `S.add_node(id, **attr)`.
    /// (Phase 4 Task 9 addition: the generator `random_simplicial_complex`
    /// must add all `N` nodes up front regardless of which simplices are
    /// sampled — XGI's own generator does `S.add_nodes_from(nodes)` before
    /// `S.add_simplices_from(simplices)`. This method was not part of the
    /// Phase-2 surface the Task-9 plan text assumed; it mirrors
    /// `Hypergraph::add_node` exactly and required no other change.)
    pub fn add_node(&mut self, node_id: &str, attrs: N) -> bool {
        self.invalidate();
        self.inner.add_node(node_id, attrs)
    }

    /// Add many nodes (no simplices). XGI parity: `S.add_nodes_from(nbunch)`.
    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);
        }
    }

    /// All simplex ids (insertion order).
    pub fn edge_ids(&self) -> Vec<String> {
        self.inner.edge_ids()
    }

    /// The ids of the simplices a node is a member of, in insertion order. XGI parity:
    /// `S.nodes.memberships(n)`.
    pub fn memberships(&self, node_id: &str) -> Option<Vec<String>> {
        self.inner.memberships(node_id)
    }

    /// Node ids with the same simplex-membership set, grouped over the whole complex: groups of two
    /// or more, in order of first occurrence. XGI parity: `S.nodes.duplicate_groups()`.
    pub fn node_duplicate_groups(&self) -> Vec<Vec<String>> {
        self.inner.nodes().duplicate_groups()
    }

    /// Simplex ids with the same member set, grouped over the whole complex (empty for a valid
    /// complex). XGI parity: `S.edges.duplicate_groups()`.
    pub fn edge_duplicate_groups(&self) -> Vec<Vec<String>> {
        self.inner.edges().duplicate_groups()
    }

    /// Whether a simplex with exactly these members is present (by member-set,
    /// not id). XGI parity: `S.has_simplex(simplex)` (simplicialcomplex.py:783-806).
    pub fn has_simplex(&self, members: &[String]) -> bool {
        let want: BTreeSet<String> = members.iter().cloned().collect();
        self.inner.edge_ids().iter().any(|e| {
            let got: BTreeSet<String> = self
                .inner
                .members(e)
                .unwrap_or_default()
                .into_iter()
                .collect();
            got == want
        })
    }

    /// Add a simplex and all its missing subfaces (size >= 2). XGI parity:
    /// `S.add_simplex(members, idx, **attr)` (simplicialcomplex.py:252-332).
    /// Duplicate members or a duplicate `idx` are silent no-ops (deviation #5).
    pub fn add_simplex(
        &mut self,
        members: Vec<String>,
        idx: Option<String>,
        attrs: E,
    ) -> Result<(), SimplexError>
    where
        N: Default,
        E: Default,
        M: Default + Clone,
    {
        let set = as_sorted_set(&members);
        // A no-op add (duplicate members or duplicate idx) adds no faces either.
        if !self.add_top_simplex(members, idx, attrs) {
            return Ok(());
        }
        // Missing subfaces (size >= 2), attribute-free.
        for face in subfaces(&set) {
            if face.is_empty() || self.has_simplex(&face) {
                continue;
            }
            let _ = self.inner.add_edge(face, None, E::default());
        }
        self.invalidate();
        Ok(())
    }

    /// Add only the top simplex, leaving its subfaces out: the complex is not closed until
    /// [`close`](Self::close) runs. XGI does this by accident: `add_simplices_from` collects the
    /// subfaces and adds them after its loop, so an error inside the loop leaves the simplices read
    /// so far without their faces; the Python bindings mirror that state. Same no-op rules as
    /// [`add_simplex`](Self::add_simplex).
    pub fn add_simplex_without_faces(
        &mut self,
        members: Vec<String>,
        idx: Option<String>,
        attrs: E,
    ) -> Result<(), SimplexError>
    where
        N: Default,
        E: Default,
        M: Default + Clone,
    {
        self.add_top_simplex(members, idx, attrs);
        Ok(())
    }

    /// Adds the top simplex; returns whether it was added (false for a no-op).
    fn add_top_simplex(&mut self, members: Vec<String>, idx: Option<String>, attrs: E) -> bool
    where
        N: Default,
        E: Default,
        M: Default + Clone,
    {
        let set = as_sorted_set(&members);
        // D1: XGI's docstring claims empty simplices cannot be added; the
        // runtime CREATES one. A repeat empty add is a no-op via the
        // has_simplex check below (an empty set is a member of itself).
        if self.has_simplex(&set) {
            return false;
        }
        if let Some(id) = &idx {
            if self.inner.has_edge(id) {
                return false; // duplicate idx -> no-op (XGI warns+returns)
            }
        }
        // Top simplex. `add_edge` has no member-count floor to pass — it
        // accepts an empty member list exactly as it accepts any other (see
        // the documentation-defect fix on `Hypergraph::add_empty_edge`,
        // same D1 register entry this function's own comment above cites);
        // an empty `set` here (the empty-simplex case) succeeds the same way.
        let _ = self.inner.add_edge(set, idx, attrs);
        self.invalidate();
        true
    }

    /// `add_edge` is not available on a simplicial complex.
    /// XGI parity: raises XGIError (simplicialcomplex.py:135-139).
    pub fn add_edge(
        &mut self,
        _members: Vec<String>,
        _idx: Option<String>,
        _attrs: E,
    ) -> Result<(), SimplexError> {
        Err(SimplexError::NotAvailable {
            method: "add_edge".to_string(),
            alternative: "add_simplex".to_string(),
        })
    }

    /// `remove_edge` is not available on a simplicial complex.
    /// XGI parity: raises XGIError (simplicialcomplex.py:153-157).
    pub fn remove_edge(&mut self, _edge_id: &str) -> Result<(), SimplexError> {
        Err(SimplexError::NotAvailable {
            method: "remove_edge".to_string(),
            alternative: "remove_simplex_id".to_string(),
        })
    }

    /// The lazily-computed face lattice (immediate-face relation), rebuilt on
    /// first access after a mutation.
    pub fn face_lattice(&self) -> &FaceLattice {
        self.face_lattice
            .get_or_init(|| self.compute_face_lattice())
    }

    fn compute_face_lattice(&self) -> FaceLattice {
        // Precompute each simplex's member-set.
        let sets: Vec<(String, BTreeSet<String>)> = self
            .inner
            .edge_ids()
            .into_iter()
            .map(|e| {
                let s: BTreeSet<String> = self
                    .inner
                    .members(&e)
                    .unwrap_or_default()
                    .into_iter()
                    .collect();
                (e, s)
            })
            .collect();
        let mut immediate_faces: BTreeMap<String, Vec<String>> = BTreeMap::new();
        for (eid, me) in &sets {
            let faces: Vec<String> = sets
                .iter()
                .filter(|(fid, mf)| fid != eid && mf.len() + 1 == me.len() && mf.is_subset(me))
                .map(|(fid, _)| fid.clone())
                .collect();
            immediate_faces.insert(eid.clone(), faces);
        }
        FaceLattice { immediate_faces }
    }

    /// Invalidate the cached face lattice (called after any mutation).
    fn invalidate(&mut self) {
        self.face_lattice = OnceCell::new();
    }

    /// Add many simplices (each via `add_simplex`, so each closes under faces).
    /// XGI parity: `S.add_simplices_from(ebunch)` — 3-tuple form
    /// (simplicialcomplex.py:373-636).
    pub fn add_simplices_from(&mut self, simplices: Vec<(Vec<String>, Option<String>, E)>)
    where
        N: Default,
        E: Default,
        M: Default + Clone,
    {
        for (members, idx, attrs) in simplices {
            let _ = self.add_simplex(members, idx, attrs);
        }
    }

    /// Add all missing subfaces of every existing simplex.
    /// XGI parity: `S.close()` (simplicialcomplex.py:638-662).
    pub fn close(&mut self)
    where
        N: Default,
        E: Default,
        M: Default + Clone,
    {
        let existing: Vec<Vec<String>> = self
            .inner
            .edge_ids()
            .iter()
            .filter_map(|e| self.inner.members(e))
            .collect();
        for members in existing {
            for face in subfaces(&as_sorted_set(&members)) {
                if !face.is_empty() && !self.has_simplex(&face) {
                    let _ = self.inner.add_edge(face, None, E::default());
                }
            }
        }
        self.invalidate();
    }

    /// Remove a simplex AND every simplex that strictly contains it (its
    /// supfaces), preserving closure. XGI parity: `S.remove_simplex_id(idx)`
    /// (simplicialcomplex.py:726-756).
    pub fn remove_simplex_id(&mut self, idx: &str) -> Result<(), SimplexError> {
        if !self.inner.has_edge(idx) {
            return Err(SimplexError::NotFound {
                idx: idx.to_string(),
            });
        }
        let target: BTreeSet<String> = self
            .inner
            .members(idx)
            .unwrap_or_default()
            .into_iter()
            .collect();
        // Supfaces: simplices whose member-set is a strict superset of target.
        let supfaces: Vec<String> = self
            .inner
            .edge_ids()
            .into_iter()
            .filter(|e| {
                let m: BTreeSet<String> = self
                    .inner
                    .members(e)
                    .unwrap_or_default()
                    .into_iter()
                    .collect();
                m.len() > target.len() && target.is_subset(&m)
            })
            .collect();
        for s in supfaces {
            let _ = self.inner.remove_edge(&s);
        }
        let _ = self.inner.remove_edge(idx);
        self.invalidate();
        Ok(())
    }

    /// Remove several simplices by id (each with its supfaces).
    pub fn remove_simplex_ids_from(&mut self, ids: &[String]) {
        for idx in ids {
            // A supface removal earlier may already have removed this id; skip.
            if self.inner.has_edge(idx) {
                let _ = self.remove_simplex_id(idx);
            }
        }
    }

    /// An independent deep copy (face lattice recomputed lazily).
    /// XGI parity: `S.copy()`.
    pub fn copy(&self) -> Self
    where
        N: Clone,
        E: Clone,
        M: Clone,
    {
        Self {
            inner: self.inner.copy(),
            face_lattice: OnceCell::new(),
        }
    }

    /// Freeze against modification (via the inner hypergraph's frozen guard).
    /// XGI parity: `S.freeze()`.
    pub fn freeze(&mut self) {
        self.inner.freeze();
    }

    /// Whether the complex is frozen. XGI parity: `S.is_frozen`.
    pub fn is_frozen(&self) -> bool {
        self.inner.is_frozen()
    }
}