Skip to main content

hypergraph_rs/generators/
simple.rs

1//! Simple structural hypergraph generators (XGI `xgi/generators/simple.py` port).
2
3use serde_json::Value;
4
5use super::classic::GeneratorError;
6use crate::Hypergraph;
7
8/// Enumerate strictly-increasing `k`-combinations of the slice `pool`.
9fn combinations_of(pool: &[usize], k: usize) -> Vec<Vec<usize>> {
10    let n = pool.len();
11    let mut out = Vec::new();
12    if k == 0 || k > n {
13        return out;
14    }
15    let mut idx: Vec<usize> = (0..k).collect();
16    loop {
17        out.push(idx.iter().map(|&i| pool[i]).collect());
18        let mut i = k;
19        while i > 0 {
20            i -= 1;
21            if idx[i] != i + n - k {
22                idx[i] += 1;
23                for j in (i + 1)..k {
24                    idx[j] = idx[j - 1] + 1;
25                }
26                break;
27            }
28            if i == 0 {
29                return out;
30            }
31        }
32    }
33}
34
35/// A star network joined to a clique promoted to hyperedges up to `d_max`.
36/// XGI `star_clique(n_star, n_clique, d_max)`.
37pub fn star_clique(
38    n_star: usize,
39    n_clique: usize,
40    d_max: usize,
41) -> Result<Hypergraph, GeneratorError> {
42    if n_star == 0 {
43        return Err(GeneratorError::InvalidParameter(
44            "n_star must be > 0".into(),
45        ));
46    }
47    if n_clique == 0 {
48        return Err(GeneratorError::InvalidParameter(
49            "n_clique must be > 0".into(),
50        ));
51    }
52    if d_max > n_clique - 1 {
53        return Err(GeneratorError::InvalidParameter(
54            "d_max must be <= n_clique - 1".into(),
55        ));
56    }
57
58    let nodes_star: Vec<usize> = (0..n_star).collect();
59    let nodes_clique: Vec<usize> = (n_star..(n_star + n_clique)).collect();
60
61    let mut h = Hypergraph::new();
62    h.add_nodes_from((0..(n_star + n_clique)).map(|i| (i.to_string(), Value::Null)));
63
64    // star legs (center = nodes_star[0])
65    for i in 1..n_star {
66        let _ = h.add_edge(
67            vec![nodes_star[0].to_string(), nodes_star[i].to_string()],
68            None,
69            Value::Null,
70        );
71    }
72    // connect star to clique
73    let _ = h.add_edge(
74        vec![nodes_star[0].to_string(), nodes_clique[0].to_string()],
75        None,
76        Value::Null,
77    );
78    // clique hyperedges up to order d_max (sizes 2..=d_max+1)
79    for d in 1..=d_max {
80        for combo in combinations_of(&nodes_clique, d + 1) {
81            let members: Vec<String> = combo.iter().map(|i| i.to_string()).collect();
82            let _ = h.add_edge(members, None, Value::Null);
83        }
84    }
85    Ok(h)
86}
87
88/// An m-uniform sunflower hypergraph: `l` petals sharing a `c`-node core.
89/// XGI `sunflower(l, c, m)`.
90pub fn sunflower(l: usize, c: usize, m: usize) -> Result<Hypergraph, GeneratorError> {
91    if m < c {
92        return Err(GeneratorError::InvalidParameter(
93            "m cannot be smaller than c".into(),
94        ));
95    }
96    let core: Vec<String> = (0..c).map(|i| i.to_string()).collect();
97    let mut h = Hypergraph::new();
98    let petal = m - c;
99    // FINDING 5 (deliberate deviation, documentation only): when m == c,
100    // `petal == 0`. XGI's `while start_label + (m - c) <= c + (m - c) * l`
101    // (xgi/generators/simple.py:114) becomes `while c <= c`, which is always
102    // true and `start_label` never advances — an unbounded loop by
103    // construction (read from source, not run — it would not terminate).
104    // Our `for p in 0..l` is bounded by construction (Power-of-10 rule 2)
105    // and instead terminates after exactly `l` petals, each identical to the
106    // `c`-node core (added as `l` distinct parallel edges via auto-id) — a
107    // deliberate improvement, not a bug-for-bug port.
108    // Bounded: exactly l petals.
109    for p in 0..l {
110        let mut members = core.clone();
111        let start = c + p * petal;
112        for i in 0..petal {
113            members.push((start + i).to_string());
114        }
115        let _ = h.add_edge(members, None, Value::Null);
116    }
117    Ok(h)
118}