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
//! Dart-free graph predicate (ALGO-PR-107).
//!
//! A graph is dart-free if it contains no induced dart. The dart is a
//! diamond (`K_4` minus one edge) plus one pendant edge from a
//! degree-2 vertex of the diamond: 5 vertices, 6 edges.
//!
//! Concretely: vertices {a, b, c, d, e} where {a, b, c, d} form a
//! diamond with edges a-b, a-c, a-d, b-c, b-d (missing c-d), and
//! pendant edge c-e (or d-e). Vertex e is adjacent only to c.
//!
//! For directed graphs, the function returns `false`.
use crate::core::{Graph, IgraphResult};
/// Check whether a graph is dart-free (no induced dart).
///
/// The dart is a diamond plus a pendant from a degree-2 vertex of the
/// diamond. A dart-free graph has no induced dart.
///
/// Returns `false` for directed graphs.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, is_dart_free};
///
/// // Diamond is dart-free (only 4 vertices)
/// let mut g = Graph::with_vertices(4);
/// g.add_edge(0, 1).unwrap();
/// g.add_edge(0, 2).unwrap();
/// g.add_edge(0, 3).unwrap();
/// g.add_edge(1, 2).unwrap();
/// g.add_edge(1, 3).unwrap();
/// assert!(is_dart_free(&g).unwrap());
///
/// // Dart: diamond {0,1,2,3} (missing 2-3) + pendant 2-4
/// let mut g = Graph::with_vertices(5);
/// g.add_edge(0, 1).unwrap();
/// g.add_edge(0, 2).unwrap();
/// g.add_edge(0, 3).unwrap();
/// g.add_edge(1, 2).unwrap();
/// g.add_edge(1, 3).unwrap();
/// g.add_edge(2, 4).unwrap();
/// assert!(!is_dart_free(&g).unwrap());
/// ```
pub fn is_dart_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];
let mut nbrs_list: Vec<Vec<u32>> = Vec::with_capacity(n_usize);
for v in 0..n {
let nbrs = graph.neighbors(v)?;
for &w in &nbrs {
adj[v as usize][w as usize] = true;
}
nbrs_list.push(nbrs);
}
// Diamond {a, b, c, d}: a-b, a-c, a-d, b-c, b-d edges, c-d missing.
// a and b are the degree-3 vertices (the "spine"), c and d are
// degree-2 (the "wings").
//
// Dart: pendant e adjacent to c (or d) but not to a, b, or d (or c).
//
// Strategy: for each edge (a,b) that share ≥ 2 common neighbors,
// find pairs (c,d) of common neighbors where c-d is NOT an edge
// (forming a diamond). Then check if c or d has a pendant neighbor
// outside {a,b,c,d} not adjacent to the other 3.
for a in 0..n {
let ai = a as usize;
for &b in &nbrs_list[ai] {
if b <= a {
continue;
}
let bi = b as usize;
// Common neighbors of a and b
let common: Vec<usize> = nbrs_list[ai]
.iter()
.filter(|&&w| w != b && adj[bi][w as usize])
.map(|&w| w as usize)
.collect();
if common.len() < 2 {
continue;
}
for (i, &ci) in common.iter().enumerate() {
for &di in &common[(i + 1)..] {
if adj[ci][di] {
continue;
}
// Diamond: {a, b} spine, {c, d} wings (c-d missing)
// Check c for pendant
if has_dart_pendant(&adj, &nbrs_list, ci, ai, bi, di) {
return Ok(false);
}
// Check d for pendant
if has_dart_pendant(&adj, &nbrs_list, di, ai, bi, ci) {
return Ok(false);
}
}
}
}
}
Ok(true)
}
/// Check if wing vertex has a neighbor outside the diamond
/// that is not adjacent to the other three diamond vertices.
fn has_dart_pendant(
adj: &[Vec<bool>],
nbrs_list: &[Vec<u32>],
wing: usize,
s1: usize,
s2: usize,
other_wing: usize,
) -> bool {
for &e_u32 in &nbrs_list[wing] {
let e = e_u32 as usize;
if e == s1 || e == s2 || e == other_wing {
continue;
}
if !adj[s1][e] && !adj[s2][e] && !adj[other_wing][e] {
return true;
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_graph() {
let g = Graph::with_vertices(0);
assert!(is_dart_free(&g).unwrap());
}
#[test]
fn small_graphs() {
let g = Graph::with_vertices(4);
assert!(is_dart_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_dart_free(&g).unwrap());
}
#[test]
fn diamond_dart_free() {
let mut g = Graph::with_vertices(4);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(0, 3).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(1, 3).unwrap();
assert!(is_dart_free(&g).unwrap());
}
#[test]
fn dart() {
// Diamond {0,1,2,3}: edges 0-1, 0-2, 0-3, 1-2, 1-3 (missing 2-3)
// Pendant: 2-4
let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(0, 3).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(1, 3).unwrap();
g.add_edge(2, 4).unwrap();
assert!(!is_dart_free(&g).unwrap());
}
#[test]
fn dart_pendant_from_other_wing() {
// Diamond {0,1,2,3}, pendant from d=3 instead of c=2
let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(0, 3).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(1, 3).unwrap();
g.add_edge(3, 4).unwrap();
assert!(!is_dart_free(&g).unwrap());
}
#[test]
fn k5_dart_free() {
// `K_5`: no missing edges in any 4-vertex subgraph → no diamond → no dart
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_dart_free(&g).unwrap());
}
#[test]
fn pendant_from_spine_not_dart() {
// Diamond {0,1,2,3} with pendant from spine vertex 0 (degree-3 vertex)
// Induced subgraph: {0,1,2,3,4} has edges 0-1,0-2,0-3,1-2,1-3,0-4
// Vertex 4 connects to 0 (spine) → this is a diamond + pendant from
// spine, which is NOT a dart (dart needs pendant from wing).
// Actually, the induced subgraph on {1,0,2,3,4}: 1-0, 0-2, 0-3, 1-2,
// 1-3, 0-4 → diamond {1,0,2,3} missing 2-3, pendant from 0 to 4.
// 0 is a spine vertex (deg 3 in diamond). Not a wing vertex.
// But wait: {2,3} are common neighbors of {0,1}. Missing edge 2-3.
// Wings are 2 and 3. Pendant 4 is from vertex 0 (spine), not wing.
// So it IS NOT a dart... but we need to check: does 2 or 3 have a
// pendant? Only if 2 or 3 has a neighbor outside {0,1,2,3} that
// is not adjacent to 0, 1, or the other wing. Vertex 4 is not a
// neighbor of 2 or 3, so no dart.
let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(0, 3).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(1, 3).unwrap();
g.add_edge(0, 4).unwrap();
assert!(is_dart_free(&g).unwrap());
}
#[test]
fn pendant_connected_to_other_wing_not_dart() {
// Diamond {0,1,2,3} + vertex 4 adjacent to both wings 2 and 3
// → not a pendant (adjacent to two diamond vertices)
let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(0, 3).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(1, 3).unwrap();
g.add_edge(2, 4).unwrap();
g.add_edge(3, 4).unwrap();
assert!(is_dart_free(&g).unwrap());
}
#[test]
fn path_dart_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_dart_free(&g).unwrap());
}
#[test]
fn star_dart_free() {
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_dart_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_dart_free(&g).unwrap());
}
#[test]
fn c4_dart_free() {
// No diamond in `C_4` → dart-free
let mut g = Graph::with_vertices(4);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
g.add_edge(3, 0).unwrap();
assert!(is_dart_free(&g).unwrap());
}
#[test]
fn c5_dart_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();
g.add_edge(4, 0).unwrap();
assert!(is_dart_free(&g).unwrap());
}
}