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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//! Chordal bipartite graph predicate (ALGO-PR-111).
//!
//! A bipartite graph is chordal bipartite if every cycle of length ≥ 6
//! has a chord. Equivalently, a bipartite graph with no induced `C_6`
//! or longer. This is NOT the same as being both chordal and bipartite
//! (which would force a forest).
//!
//! Non-bipartite and directed graphs return `false`.
use crate::algorithms::connectivity::{ConnectednessMode, is_connected};
use crate::algorithms::properties::is_bipartite::is_bipartite;
use crate::core::{Graph, IgraphResult};
/// Check whether a graph is chordal bipartite.
///
/// A bipartite graph is chordal bipartite if it has no induced cycle
/// of length ≥ 6. Returns `false` for non-bipartite or directed graphs.
///
/// Uses a perfect elimination ordering (PEO) approach on the bipartite
/// adjacency: repeatedly finds a vertex whose neighborhood forms a
/// "bisimplicial" structure and removes it.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, is_chordal_bipartite};
///
/// // Complete bipartite `K_{2,3}` is chordal bipartite
/// let mut g = Graph::with_vertices(5);
/// 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(1, 3).unwrap();
/// g.add_edge(1, 4).unwrap();
/// assert!(is_chordal_bipartite(&g).unwrap());
///
/// // `C_6` is bipartite but NOT chordal bipartite
/// let mut g = Graph::with_vertices(6);
/// 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, 5).unwrap();
/// g.add_edge(5, 0).unwrap();
/// assert!(!is_chordal_bipartite(&g).unwrap());
/// ```
pub fn is_chordal_bipartite(graph: &Graph) -> IgraphResult<bool> {
if graph.is_directed() {
return Ok(false);
}
let n = graph.vcount();
if n <= 4 {
// Any bipartite graph on ≤ 4 vertices is chordal bipartite
// (need ≥ 6 vertices for C_6).
return is_bipartite(graph).map(|r| r.is_bipartite);
}
let bip = is_bipartite(graph)?;
if !bip.is_bipartite {
return Ok(false);
}
// For disconnected graphs, each component must be chordal bipartite.
if !is_connected(graph, ConnectednessMode::Weak)? {
return check_components_chordal_bipartite(graph);
}
check_single_component(graph, n)
}
/// Check each connected component independently.
fn check_components_chordal_bipartite(graph: &Graph) -> IgraphResult<bool> {
let n = graph.vcount();
let n_usize = n as usize;
let mut visited = vec![false; n_usize];
let mut nbrs_cache: Vec<Vec<u32>> = Vec::with_capacity(n_usize);
for v in 0..n {
nbrs_cache.push(graph.neighbors(v)?);
}
for start in 0..n_usize {
if visited[start] {
continue;
}
// BFS to find component
let mut component = Vec::new();
let mut queue = std::collections::VecDeque::new();
visited[start] = true;
queue.push_back(start);
while let Some(u) = queue.pop_front() {
component.push(u);
for &w in &nbrs_cache[u] {
let wi = w as usize;
if !visited[wi] {
visited[wi] = true;
queue.push_back(wi);
}
}
}
if component.len() >= 6 && !check_component_chordal_bipartite(&nbrs_cache, &component) {
return Ok(false);
}
}
Ok(true)
}
/// Check a single connected component for chordal bipartiteness.
fn check_component_chordal_bipartite(nbrs_cache: &[Vec<u32>], component: &[usize]) -> bool {
// Build local adjacency for the component using a vertex map.
let max_id = component.iter().copied().max().unwrap_or(0);
let mut global_to_local = vec![usize::MAX; max_id + 1];
for (i, &v) in component.iter().enumerate() {
global_to_local[v] = i;
}
let cn = component.len();
let mut adj = vec![vec![false; cn]; cn];
for &v in component {
let li = global_to_local[v];
for &w in &nbrs_cache[v] {
let wi = w as usize;
if wi <= max_id && global_to_local[wi] != usize::MAX {
adj[li][global_to_local[wi]] = true;
}
}
}
is_chordal_bipartite_from_adj(&adj, cn)
}
/// Check a single connected graph using adjacency matrix.
fn check_single_component(graph: &Graph, n: u32) -> IgraphResult<bool> {
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;
}
}
Ok(is_chordal_bipartite_from_adj(&adj, n_usize))
}
/// Core algorithm: check chordal bipartiteness via bisimplicial
/// elimination. A bipartite graph is chordal bipartite iff it has a
/// perfect elimination ordering where each eliminated vertex is
/// bisimplicial (its neighbors form a complete bipartite subgraph
/// with their common neighborhood).
///
/// Simpler approach: check that no induced `C_6` exists. Since
/// checking all `C_{2k}` for k≥3 is expensive, we use the
/// bisimplicial elimination characterization.
fn is_chordal_bipartite_from_adj(adj: &[Vec<bool>], n: usize) -> bool {
// A vertex v is bisimplicial if: for every pair of neighbors
// u, w of v, every common neighbor of u and w (other than v)
// that is on the same side as v is also a neighbor of v.
// Equivalently: N(u) ∩ N(w) ⊆ N(v) ∪ {v} for all u,w ∈ N(v)
// on one side, and the neighborhoods of all N(v) restricted to
// v's side are nested (form a chain under inclusion).
// Practical approach: iteratively remove bisimplicial vertices.
// If we can remove all vertices, it's chordal bipartite.
let mut removed = vec![false; n];
let mut remaining = n;
while remaining > 0 {
let mut found = false;
for v in 0..n {
if removed[v] {
continue;
}
if is_bisimplicial(adj, v, &removed, n) {
removed[v] = true;
remaining -= 1;
found = true;
break;
}
}
if !found {
return false;
}
}
true
}
/// Check if vertex v is bisimplicial in the remaining graph.
/// In a bipartite graph, v is bisimplicial if the subgraph induced by
/// N(v) and (N(N(v)) \ {v}) is complete bipartite: every second-neighbor
/// of v must be adjacent to ALL vertices in N(v).
fn is_bisimplicial(adj: &[Vec<bool>], v: usize, removed: &[bool], n: usize) -> bool {
let nbrs_v: Vec<usize> = (0..n).filter(|&u| !removed[u] && adj[v][u]).collect();
if nbrs_v.is_empty() {
return true;
}
// For each neighbor u of v, every active neighbor x of u (x ≠ v)
// must be adjacent to ALL of N(v).
for &u in &nbrs_v {
for x in 0..n {
if removed[x] || x == v || !adj[u][x] {
continue;
}
// x is a second-neighbor of v via u — must connect to all of N(v)
for &w in &nbrs_v {
if !adj[x][w] {
return false;
}
}
}
}
true
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_graph() {
let g = Graph::with_vertices(0);
assert!(is_chordal_bipartite(&g).unwrap());
}
#[test]
fn single_vertex() {
let g = Graph::with_vertices(1);
assert!(is_chordal_bipartite(&g).unwrap());
}
#[test]
fn single_edge() {
let mut g = Graph::with_vertices(2);
g.add_edge(0, 1).unwrap();
assert!(is_chordal_bipartite(&g).unwrap());
}
#[test]
fn c4_chordal_bipartite() {
// `C_4` is bipartite and chordal bipartite (no `C_6`)
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_chordal_bipartite(&g).unwrap());
}
#[test]
fn c6_not_chordal_bipartite() {
let mut g = Graph::with_vertices(6);
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, 5).unwrap();
g.add_edge(5, 0).unwrap();
assert!(!is_chordal_bipartite(&g).unwrap());
}
#[test]
fn complete_bipartite_k23() {
let mut g = Graph::with_vertices(5);
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(1, 3).unwrap();
g.add_edge(1, 4).unwrap();
assert!(is_chordal_bipartite(&g).unwrap());
}
#[test]
fn complete_bipartite_k33() {
let mut g = Graph::with_vertices(6);
for i in 0..3u32 {
for j in 3..6u32 {
g.add_edge(i, j).unwrap();
}
}
assert!(is_chordal_bipartite(&g).unwrap());
}
#[test]
fn tree_chordal_bipartite() {
// Trees are bipartite and chordal bipartite (no cycles at all)
let mut g = Graph::with_vertices(5);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(1, 3).unwrap();
g.add_edge(3, 4).unwrap();
assert!(is_chordal_bipartite(&g).unwrap());
}
#[test]
fn triangle_not_bipartite() {
// Triangle is not bipartite → not chordal bipartite
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_chordal_bipartite(&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();
assert!(!is_chordal_bipartite(&g).unwrap());
}
#[test]
fn path_chordal_bipartite() {
let mut g = Graph::with_vertices(6);
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, 5).unwrap();
assert!(is_chordal_bipartite(&g).unwrap());
}
#[test]
fn star_chordal_bipartite() {
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_chordal_bipartite(&g).unwrap());
}
#[test]
fn c8_not_chordal_bipartite() {
let mut g = Graph::with_vertices(8);
for i in 0..8u32 {
g.add_edge(i, (i + 1) % 8).unwrap();
}
assert!(!is_chordal_bipartite(&g).unwrap());
}
#[test]
fn disconnected_bipartite() {
// Two `C_4` components → chordal bipartite
let mut g = Graph::with_vertices(8);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
g.add_edge(2, 3).unwrap();
g.add_edge(3, 0).unwrap();
g.add_edge(4, 5).unwrap();
g.add_edge(5, 6).unwrap();
g.add_edge(6, 7).unwrap();
g.add_edge(7, 4).unwrap();
assert!(is_chordal_bipartite(&g).unwrap());
}
}