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
//! Fork-free graph predicate (ALGO-PR-108).
//!
//! A graph is fork-free if it contains no induced fork (also called
//! cross or star-1,1,2). The fork has 5 vertices and 4 edges: a center
//! vertex with 3 neighbors, one of which has an additional pendant.
//! Equivalently, the fork is `K_{1,3}` (claw) with one edge subdivided.
//!
//! Vertices: {c, a, b, d, e} where c-a, c-b, c-d, d-e are edges
//! (4 edges total). No other edges among these 5 vertices.
//!
//! For directed graphs, the function returns `false`.
use crate::core::{Graph, IgraphResult};
/// Check whether a graph is fork-free (no induced fork / cross).
///
/// The fork is a claw (`K_{1,3}`) with one edge subdivided: center c
/// adjacent to a, b, d, and d also adjacent to e. No other edges
/// among {c, a, b, d, e}.
///
/// Returns `false` for directed graphs.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, is_fork_free};
///
/// // Path `P_4` is fork-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();
/// assert!(is_fork_free(&g).unwrap());
///
/// // Fork: center 0, arms 1, 2, 3, pendant 3-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(3, 4).unwrap();
/// assert!(!is_fork_free(&g).unwrap());
/// ```
pub fn is_fork_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);
}
// Fork: center c with neighbors {a, b, d} where a, b, d pairwise
// non-adjacent (induced claw), and d has a neighbor e outside
// {c, a, b, d} not adjacent to c, a, or b.
//
// Strategy: for each vertex c with degree ≥ 3, find triples of
// pairwise non-adjacent neighbors (induced claw). For each such
// triple, check if any of the three arms has a pendant forming
// the fork.
for c in 0..n {
let ci = c as usize;
let cn = &nbrs_list[ci];
if cn.len() < 3 {
continue;
}
for (i, &a) in cn.iter().enumerate() {
let ai = a as usize;
for (j, &b) in cn.iter().enumerate().skip(i + 1) {
let bi = b as usize;
if adj[ai][bi] {
continue;
}
for &d in &cn[(j + 1)..] {
let di = d as usize;
if adj[ai][di] || adj[bi][di] {
continue;
}
// Induced claw: c-{a,b,d}. Check each arm for pendant.
if has_fork_pendant(&adj, &nbrs_list, ai, ci, bi, di) {
return Ok(false);
}
if has_fork_pendant(&adj, &nbrs_list, bi, ci, ai, di) {
return Ok(false);
}
if has_fork_pendant(&adj, &nbrs_list, di, ci, ai, bi) {
return Ok(false);
}
}
}
}
}
Ok(true)
}
/// Check if arm vertex has a pendant neighbor outside the claw.
fn has_fork_pendant(
adj: &[Vec<bool>],
nbrs_list: &[Vec<u32>],
arm: usize,
center: usize,
other1: usize,
other2: usize,
) -> bool {
for &e_u32 in &nbrs_list[arm] {
let e = e_u32 as usize;
if e == center || e == other1 || e == other2 {
continue;
}
if !adj[center][e] && !adj[other1][e] && !adj[other2][e] {
return true;
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_graph() {
let g = Graph::with_vertices(0);
assert!(is_fork_free(&g).unwrap());
}
#[test]
fn small_graphs() {
let g = Graph::with_vertices(4);
assert!(is_fork_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_fork_free(&g).unwrap());
}
#[test]
fn fork() {
// Center 0, arms 1,2,3, pendant 3-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(3, 4).unwrap();
assert!(!is_fork_free(&g).unwrap());
}
#[test]
fn claw_is_fork_free() {
// Claw `K_{1,3}`: only 4 vertices, no room for pendant
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();
assert!(is_fork_free(&g).unwrap());
}
#[test]
fn claw_with_pendant_is_fork() {
// Claw + pendant = fork
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, 4).unwrap();
assert!(!is_fork_free(&g).unwrap());
}
#[test]
fn k5_fork_free() {
// `K_5`: no induced claw → no fork
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_fork_free(&g).unwrap());
}
#[test]
fn path_p5_not_fork_free() {
// `P_5`: 0-1-2-3-4. Vertex 2 has neighbors {1,3}. Only degree 2,
// not enough for claw. No vertex has degree ≥ 3. So no claw → fork-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_fork_free(&g).unwrap());
}
#[test]
fn star_with_subdivided_edge() {
// Star `S_4` (center 0, leaves 1,2,3,4) with edge 0-1 subdivided:
// 0-5-1, 0-2, 0-3, 0-4. Center 0 has neighbors {5,2,3,4} → claw
// {0,5,2,3}. Does 5 have a pendant? 5-1 edge, 1 not adj to 0... wait,
// we removed 0-1 and added 0-5, 5-1. So 0's neighbors are {5,2,3,4}.
// 5's neighbors are {0,1}. Claw on {0; 5,2,3}: 5 has pendant 1.
// 1 not adj to 0? Yes (we removed that edge). Not adj to 2,3? Yes.
// So this IS a fork.
let mut g = Graph::with_vertices(6);
g.add_edge(0, 5).unwrap();
g.add_edge(5, 1).unwrap();
g.add_edge(0, 2).unwrap();
g.add_edge(0, 3).unwrap();
g.add_edge(0, 4).unwrap();
assert!(!is_fork_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_fork_free(&g).unwrap());
}
#[test]
fn pendant_adjacent_to_another_arm() {
// Claw center 0 arms {1,2,3}, pendant from 3: 3-4.
// But 4 adjacent to 1 → not an induced fork (4-1 edge present)
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(3, 4).unwrap();
g.add_edge(1, 4).unwrap();
assert!(is_fork_free(&g).unwrap());
}
#[test]
fn c5_fork_free() {
// `C_5`: max degree 2, no claw → fork-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_fork_free(&g).unwrap());
}
#[test]
fn star_s5_is_fork_free() {
// `S_5`: center 0, leaves 1-5. Degree of center is 5.
// Claw triples: e.g. {1,2,3}. Pendant of 1? 1 has degree 1 (only
// neighbor is 0), so no pendant → fork-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_fork_free(&g).unwrap());
}
}