hypergraph_rs/generators/
simple.rs1use serde_json::Value;
4
5use super::classic::GeneratorError;
6use crate::Hypergraph;
7
8fn 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
35pub 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 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 let _ = h.add_edge(
74 vec![nodes_star[0].to_string(), nodes_clique[0].to_string()],
75 None,
76 Value::Null,
77 );
78 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
88pub 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 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}