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
//! Cricket-free graph predicate (ALGO-PR-104).
//!
//! A graph is cricket-free if it contains no induced cricket. The
//! cricket has 5 vertices: a triangle {a, b, c} plus two pendant edges
//! from the *same* triangle vertex, say b-d and b-e (d and e each have
//! degree 1 in the cricket). It has 5 edges total.
//!
//! The cricket differs from the bull: in a bull the two pendants hang
//! from *different* triangle vertices; in a cricket they hang from the
//! *same* vertex.
//!
//! For directed graphs, the function returns `false`.
use crate::core::{Graph, IgraphResult};
/// Check whether a graph is cricket-free.
///
/// The cricket is a triangle with two pendant edges from the same
/// triangle vertex. A cricket-free graph has no induced cricket.
///
/// Returns `false` for directed graphs.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, is_cricket_free};
///
/// // Triangle is cricket-free
/// let mut g = Graph::with_vertices(3);
/// g.add_edge(0, 1).unwrap();
/// g.add_edge(1, 2).unwrap();
/// g.add_edge(2, 0).unwrap();
/// assert!(is_cricket_free(&g).unwrap());
///
/// // Cricket: triangle {0,1,2}, pendants 1-3 and 1-4
/// let mut g = Graph::with_vertices(5);
/// g.add_edge(0, 1).unwrap();
/// g.add_edge(0, 2).unwrap();
/// g.add_edge(1, 2).unwrap();
/// g.add_edge(1, 3).unwrap();
/// g.add_edge(1, 4).unwrap();
/// assert!(!is_cricket_free(&g).unwrap());
/// ```
pub fn is_cricket_free(graph: &Graph) -> IgraphResult<bool> {
if graph.is_directed() {
return Ok(false);
}
let n = graph.vcount();
if n < 5 {
return Ok(true);
}
let n_usize = n as usize;
let mut adj = vec![vec![false; n_usize]; n_usize];
for v in 0..n {
let nbrs = graph.neighbors(v)?;
for &w in &nbrs {
adj[v as usize][w as usize] = true;
}
}
// Induced cricket: triangle {a, b, c} with two pendant vertices d, e
// adjacent to b (the "hub") but not to a, c, or each other.
// For each triangle, check each of the 3 vertices as potential hub.
for a in 0..n {
let ai = a as usize;
for b in (a + 1)..n {
let bi = b as usize;
if !adj[ai][bi] {
continue;
}
for c in (b + 1)..n {
let ci = c as usize;
if !adj[ai][ci] || !adj[bi][ci] {
continue;
}
// Triangle {a, b, c}. Try each vertex as the pendant hub.
if has_cricket_pendants(&adj, n_usize, ai, bi, ci) {
return Ok(false);
}
if has_cricket_pendants(&adj, n_usize, bi, ai, ci) {
return Ok(false);
}
if has_cricket_pendants(&adj, n_usize, ci, ai, bi) {
return Ok(false);
}
}
}
}
Ok(true)
}
/// Check if vertex `hub` (part of triangle {hub, t1, t2}) has two
/// pendant neighbors d, e that form an induced cricket.
/// d and e must be adjacent to hub only, not to t1, t2, or each other.
fn has_cricket_pendants(adj: &[Vec<bool>], n: usize, hub: usize, t1: usize, t2: usize) -> bool {
let row_hub = &adj[hub];
let row_t1 = &adj[t1];
let row_t2 = &adj[t2];
let pendants: Vec<usize> = row_hub
.iter()
.enumerate()
.take(n)
.filter(|&(d, &is_adj)| {
is_adj && d != hub && d != t1 && d != t2 && !row_t1[d] && !row_t2[d]
})
.map(|(d, _)| d)
.collect();
if pendants.len() < 2 {
return false;
}
for (i, &d) in pendants.iter().enumerate() {
for &e in &pendants[(i + 1)..] {
if !adj[d][e] {
return true;
}
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_graph() {
let g = Graph::with_vertices(0);
assert!(is_cricket_free(&g).unwrap());
}
#[test]
fn small_graphs() {
let g = Graph::with_vertices(4);
assert!(is_cricket_free(&g).unwrap());
}
#[test]
fn triangle() {
let mut g = Graph::with_vertices(3);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 0).unwrap();
assert!(is_cricket_free(&g).unwrap());
}
#[test]
fn cricket() {
// Triangle {0,1,2}, pendants 1-3 and 1-4
let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(1, 3).unwrap();
g.add_edge(1, 4).unwrap();
assert!(!is_cricket_free(&g).unwrap());
}
#[test]
fn bull_is_cricket_free() {
// Bull: triangle {0,1,2}, pendants from DIFFERENT vertices: 1-3, 2-4
// Not a cricket (pendants from same vertex)
let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(1, 3).unwrap();
g.add_edge(2, 4).unwrap();
assert!(is_cricket_free(&g).unwrap());
}
#[test]
fn k5_cricket_free() {
// `K_5`: all edges present → no induced cricket (pendants would need non-edges)
let mut g = Graph::with_vertices(5);
for i in 0..5u32 {
for j in (i + 1)..5 {
g.add_edge(i, j).unwrap();
}
}
assert!(is_cricket_free(&g).unwrap());
}
#[test]
fn paw_cricket_free() {
// Paw: triangle {0,1,2} + pendant 2-3. Only one pendant → not a cricket
let mut g = Graph::with_vertices(4);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 0).unwrap();
g.add_edge(2, 3).unwrap();
assert!(is_cricket_free(&g).unwrap());
}
#[test]
fn pendants_adjacent_not_cricket() {
// Triangle {0,1,2}, "pendants" 1-3, 1-4 but 3-4 adjacent
// Induced subgraph has edge 3-4 → not a cricket (cricket has no d-e edge)
let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(1, 3).unwrap();
g.add_edge(1, 4).unwrap();
g.add_edge(3, 4).unwrap();
assert!(is_cricket_free(&g).unwrap());
}
#[test]
fn pendant_connected_to_triangle_vertex() {
// Triangle {0,1,2}, 1-3, 1-4, but 3-0 also present
// d=3 is adjacent to a=0, so not a pendant → not a cricket on this subgraph
// But check: triangle {0,1,2} with hub=1: pendant candidates must NOT
// be adjacent to 0 or 2. d=3 is adjacent to 0 → d=3 not a pendant.
// So only e=4 is a pendant of hub=1, and we need ≥ 2.
let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(1, 3).unwrap();
g.add_edge(1, 4).unwrap();
g.add_edge(0, 3).unwrap();
assert!(is_cricket_free(&g).unwrap());
}
#[test]
fn directed_returns_false() {
let mut g = Graph::new(3, true).unwrap();
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 0).unwrap();
assert!(!is_cricket_free(&g).unwrap());
}
#[test]
fn star_cricket_free() {
// Star: no triangles → no cricket
let mut g = Graph::with_vertices(6);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(0, 3).unwrap();
g.add_edge(0, 4).unwrap();
g.add_edge(0, 5).unwrap();
assert!(is_cricket_free(&g).unwrap());
}
#[test]
fn path_cricket_free() {
let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
g.add_edge(3, 4).unwrap();
assert!(is_cricket_free(&g).unwrap());
}
#[test]
fn three_pendants_from_hub() {
// Triangle {0,1,2}, three pendants from 1: 1-3, 1-4, 1-5
// Any pair of non-adjacent pendants forms a cricket
let mut g = Graph::with_vertices(6);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(1, 3).unwrap();
g.add_edge(1, 4).unwrap();
g.add_edge(1, 5).unwrap();
assert!(!is_cricket_free(&g).unwrap());
}
}