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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
//! Trivially perfect graph predicate (ALGO-PR-094).
//!
//! A graph is trivially perfect (also called quasi-threshold) iff
//! every connected induced subgraph has a universal vertex (a vertex
//! adjacent to all others in the component). Equivalently, it has no
//! induced `P_4` or `C_4`.
//!
//! For directed graphs, the function returns `false`.
use crate::core::{Graph, IgraphResult};
/// Check whether a graph is trivially perfect.
///
/// A trivially perfect graph (quasi-threshold graph) has no induced
/// `P_4` or `C_4`. Equivalently, every connected induced subgraph
/// has a universal vertex.
///
/// Returns `false` for directed graphs.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, is_trivially_perfect};
///
/// // Star `K_{1,3}` is trivially perfect
/// 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_trivially_perfect(&g).unwrap());
///
/// // `P_4` (path of 4 vertices) is NOT trivially perfect
/// 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_trivially_perfect(&g).unwrap());
/// ```
pub fn is_trivially_perfect(graph: &Graph) -> IgraphResult<bool> {
if graph.is_directed() {
return Ok(false);
}
let n = graph.vcount();
if n <= 2 {
return Ok(true);
}
// Check for induced P_4: for each edge (u, v), check if there exists
// w adjacent to v but not u, and x adjacent to w but not v and not u.
// This gives an induced path u - v - w - x.
//
// Also check for induced C_4: for each pair (u, v) at distance 2
// (sharing a common neighbor), check if they have two distinct
// common non-neighbors that form the other two vertices of a C_4.
//
// We use the universal-vertex characterization instead for clarity:
// For each connected component, verify there's a vertex adjacent to
// all others in the component, then recurse on the remaining vertices.
// Build adjacency sets for fast lookup
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;
}
}
// Find connected components via BFS on our adjacency matrix
let mut visited = vec![false; n_usize];
let mut component = Vec::new();
for start in 0..n {
if visited[start as usize] {
continue;
}
// BFS to find this component
component.clear();
let mut queue = std::collections::VecDeque::new();
queue.push_back(start);
visited[start as usize] = true;
while let Some(v) = queue.pop_front() {
component.push(v);
for w in 0..n {
if !visited[w as usize] && adj[v as usize][w as usize] {
visited[w as usize] = true;
queue.push_back(w);
}
}
}
// Check this component recursively
if !check_component_trivially_perfect(&adj, &component) {
return Ok(false);
}
}
Ok(true)
}
fn check_component_trivially_perfect(adj: &[Vec<bool>], component: &[u32]) -> bool {
let size = component.len();
if size <= 2 {
return true;
}
// Find a universal vertex in this component
let mut universal = None;
for &v in component {
let deg_in_comp = component
.iter()
.filter(|&&w| w != v && adj[v as usize][w as usize])
.count();
if deg_in_comp == size - 1 {
universal = Some(v);
break;
}
}
let Some(univ) = universal else {
return false;
};
// Remove the universal vertex and recurse on each resulting component
let remaining: Vec<u32> = component.iter().copied().filter(|&v| v != univ).collect();
// Find connected components among remaining vertices
let mut rem_visited = vec![false; remaining.len()];
let mut sub_component = Vec::new();
for i in 0..remaining.len() {
if rem_visited[i] {
continue;
}
sub_component.clear();
let mut queue = std::collections::VecDeque::new();
queue.push_back(i);
rem_visited[i] = true;
while let Some(idx) = queue.pop_front() {
sub_component.push(remaining[idx]);
for j in 0..remaining.len() {
if !rem_visited[j] && adj[remaining[idx] as usize][remaining[j] as usize] {
rem_visited[j] = true;
queue.push_back(j);
}
}
}
if !check_component_trivially_perfect(adj, &sub_component) {
return false;
}
}
true
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_graph() {
let g = Graph::with_vertices(0);
assert!(is_trivially_perfect(&g).unwrap());
}
#[test]
fn single_vertex() {
let g = Graph::with_vertices(1);
assert!(is_trivially_perfect(&g).unwrap());
}
#[test]
fn single_edge() {
let mut g = Graph::with_vertices(2);
g.add_edge(0, 1).unwrap();
assert!(is_trivially_perfect(&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_trivially_perfect(&g).unwrap());
}
#[test]
fn star_k14() {
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(0, 4).unwrap();
assert!(is_trivially_perfect(&g).unwrap());
}
#[test]
fn complete_k4() {
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();
g.add_edge(2, 3).unwrap();
assert!(is_trivially_perfect(&g).unwrap());
}
#[test]
fn p4_not_trivially_perfect() {
// P_4: 0-1-2-3 — has an induced P_4
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_trivially_perfect(&g).unwrap());
}
#[test]
fn c4_not_trivially_perfect() {
// C_4: 0-1-2-3-0 — has an induced C_4
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_trivially_perfect(&g).unwrap());
}
#[test]
fn c5_not_trivially_perfect() {
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_trivially_perfect(&g).unwrap());
}
#[test]
fn disconnected_trivially_perfect() {
// Two isolated edges: trivially perfect
let mut g = Graph::with_vertices(4);
g.add_edge(0, 1).unwrap();
g.add_edge(2, 3).unwrap();
assert!(is_trivially_perfect(&g).unwrap());
}
#[test]
fn disconnected_with_p4() {
// Triangle + P_4
let mut g = Graph::with_vertices(7);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 0).unwrap();
g.add_edge(3, 4).unwrap();
g.add_edge(4, 5).unwrap();
g.add_edge(5, 6).unwrap();
assert!(!is_trivially_perfect(&g).unwrap());
}
#[test]
fn fan_with_p4_not_trivially_perfect() {
// Fan: universal vertex 0 connected to path 1-2-3-4
// Removing 0 leaves P_4 → not trivially perfect
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(0, 4).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
g.add_edge(3, 4).unwrap();
assert!(!is_trivially_perfect(&g).unwrap());
}
#[test]
fn fan_with_p3_trivially_perfect() {
// Universal vertex 0 connected to path 1-2-3
// Removing 0 leaves P_3; vertex 2 is universal in P_3.
// Removing 2 leaves {1, 3} disconnected, each trivially perfect.
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(2, 3).unwrap();
assert!(is_trivially_perfect(&g).unwrap());
}
#[test]
fn nested_stars() {
// 0 connected to all; 1 connected to {2, 3}
// This is a "threshold graph" = trivially perfect
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_trivially_perfect(&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_trivially_perfect(&g).unwrap());
}
#[test]
fn bull_graph_not_trivially_perfect() {
// Bull: triangle 0-1-2 with pendant edges 1-3 and 2-4
// Induced path: 3-1-2-4 → P_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(2, 4).unwrap();
assert!(!is_trivially_perfect(&g).unwrap());
}
#[test]
fn isolated_vertices() {
let g = Graph::with_vertices(5);
assert!(is_trivially_perfect(&g).unwrap());
}
}