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
//! Chain graph predicate (ALGO-PR-114).
//!
//! A bipartite graph is a chain graph if the neighborhoods of the
//! vertices in each part are totally ordered by inclusion.
//! Equivalently, a bipartite graph with no induced `2K_2`
//! (two disjoint edges as induced subgraph).
//!
//! Non-bipartite and directed graphs return `false`.
use crate::algorithms::properties::is_bipartite::is_bipartite;
use crate::core::{Graph, IgraphResult};
/// Check whether a graph is a chain graph.
///
/// A bipartite graph is a chain graph if the neighborhoods of the
/// vertices in each part are totally ordered by inclusion.
/// Returns `false` for non-bipartite or directed graphs.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, is_chain_graph};
///
/// // Complete bipartite `K_{2,3}` is a chain graph
/// 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_chain_graph(&g).unwrap());
///
/// // `C_6` is bipartite but NOT a chain graph (has induced `2K_2`)
/// 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_chain_graph(&g).unwrap());
/// ```
pub fn is_chain_graph(graph: &Graph) -> IgraphResult<bool> {
if graph.is_directed() {
return Ok(false);
}
let n = graph.vcount();
if n <= 2 {
return is_bipartite(graph).map(|r| r.is_bipartite);
}
let bip = is_bipartite(graph)?;
if !bip.is_bipartite {
return Ok(false);
}
let n_usize = n as usize;
let types = &bip.types;
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;
}
}
// Partition vertices by type
let part_a: Vec<usize> = (0..n_usize).filter(|&v| !types[v]).collect();
let part_b: Vec<usize> = (0..n_usize).filter(|&v| types[v]).collect();
// Check neighborhoods in part A are totally ordered by inclusion
// w.r.t. their neighbors in part B
if !neighborhoods_nested(&adj, &part_a, &part_b) {
return Ok(false);
}
// Check neighborhoods in part B are totally ordered by inclusion
// w.r.t. their neighbors in part A
if !neighborhoods_nested(&adj, &part_b, &part_a) {
return Ok(false);
}
Ok(true)
}
/// Check that the neighborhoods of vertices in `part` (restricted to
/// `other_part`) are totally ordered by inclusion.
fn neighborhoods_nested(adj: &[Vec<bool>], part: &[usize], other_part: &[usize]) -> bool {
if part.len() <= 1 {
return true;
}
// Collect neighborhood sets (as sorted vecs of other-side indices)
let mut nbr_sets: Vec<Vec<usize>> = part
.iter()
.map(|&v| other_part.iter().copied().filter(|&u| adj[v][u]).collect())
.collect();
// Sort by neighborhood size
nbr_sets.sort_by_key(Vec::len);
// Check each consecutive pair: smaller must be subset of larger
for window in nbr_sets.windows(2) {
let smaller = &window[0];
let larger = &window[1];
if !is_subset(smaller, larger) {
return false;
}
}
true
}
/// Check if `a` ⊆ `b` (both sorted).
fn is_subset(a: &[usize], b: &[usize]) -> bool {
if a.len() > b.len() {
return false;
}
let mut bi = 0;
for &x in a {
while bi < b.len() && b[bi] < x {
bi += 1;
}
if bi >= b.len() || b[bi] != x {
return false;
}
bi += 1;
}
true
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_graph() {
let g = Graph::with_vertices(0);
assert!(is_chain_graph(&g).unwrap());
}
#[test]
fn single_vertex() {
let g = Graph::with_vertices(1);
assert!(is_chain_graph(&g).unwrap());
}
#[test]
fn single_edge() {
let mut g = Graph::with_vertices(2);
g.add_edge(0, 1).unwrap();
assert!(is_chain_graph(&g).unwrap());
}
#[test]
fn path_p3() {
// 0-1-2: parts {0,2} and {1}. N(0)={1}, N(2)={1} → equal → nested.
let mut g = Graph::with_vertices(3);
g.add_edge(0, 1).unwrap();
g.add_edge(1, 2).unwrap();
assert!(is_chain_graph(&g).unwrap());
}
#[test]
fn path_p4() {
// 0-1-2-3: parts {0,2} and {1,3}. N(0)={1}, N(2)={1,3} → {1}⊂{1,3} ✓
// N(1)={0,2}, N(3)={2} → {2}⊂{0,2} ✓ → chain graph
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_chain_graph(&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_chain_graph(&g).unwrap());
}
#[test]
fn c4_not_chain() {
// C_4: parts {0,2}, {1,3}. N(0)={1,3}, N(2)={1,3} → equal → nested ✓
// N(1)={0,2}, N(3)={0,2} → equal → nested ✓
// Actually C_4 IS a chain graph (all nbrs same size and equal).
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_chain_graph(&g).unwrap());
}
#[test]
fn c6_not_chain() {
// C_6 has induced 2K_2 → NOT a chain graph
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_chain_graph(&g).unwrap());
}
#[test]
fn star() {
// Star: parts {0} and {1,2,3}. N(0)={1,2,3} → single vertex → nested.
// N(1)=N(2)=N(3)={0} → all equal → nested. Chain graph.
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_chain_graph(&g).unwrap());
}
#[test]
fn two_disjoint_edges() {
// 2K_2: 0-1, 2-3. Parts {0,2}, {1,3}. N(0)={1}, N(2)={3}.
// Neither {1}⊂{3} nor {3}⊂{1} → NOT nested → NOT chain.
let mut g = Graph::with_vertices(4);
g.add_edge(0, 1).unwrap();
g.add_edge(2, 3).unwrap();
assert!(!is_chain_graph(&g).unwrap());
}
#[test]
fn triangle_not_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_chain_graph(&g).unwrap());
}
#[test]
fn directed_returns_false() {
let mut g = Graph::new(2, true).unwrap();
g.add_edge(0, 1).unwrap();
assert!(!is_chain_graph(&g).unwrap());
}
#[test]
fn isolated_vertices() {
// 3 isolated vertices: bipartite, each has empty neighborhood → nested.
let g = Graph::with_vertices(3);
assert!(is_chain_graph(&g).unwrap());
}
#[test]
fn k33_not_chain() {
// K_{3,3}: all vertices in each part have the same neighborhood → nested ✓
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_chain_graph(&g).unwrap());
}
#[test]
fn bipartite_with_induced_2k2() {
// 0-2, 0-3, 1-3 (missing 1-2). Parts {0,1}, {2,3}.
// N(0)={2,3}, N(1)={3}. {3}⊂{2,3} ✓
// N(2)={0}, N(3)={0,1}. {0}⊂{0,1} ✓
// Actually this IS a chain graph! No induced 2K_2.
// Let me try: 0-2, 1-3 only.
// Parts {0,1}, {2,3}. N(0)={2}, N(1)={3}. Neither subset → NOT chain.
let mut g = Graph::with_vertices(4);
g.add_edge(0, 2).unwrap();
g.add_edge(1, 3).unwrap();
assert!(!is_chain_graph(&g).unwrap());
}
#[test]
fn nested_neighborhoods() {
// 0-3, 0-4, 1-3, 1-4, 1-5, 2-3, 2-4, 2-5
// Parts {0,1,2}, {3,4,5}
// N(0)={3,4}, N(1)={3,4,5}, N(2)={3,4,5}
// Sorted: {3,4} ⊂ {3,4,5} = {3,4,5} ✓
// N(3)={0,1,2}, N(4)={0,1,2}, N(5)={1,2}
// Sorted: {1,2} ⊂ {0,1,2} = {0,1,2} ✓
let mut g = Graph::with_vertices(6);
g.add_edge(0, 3).unwrap();
g.add_edge(0, 4).unwrap();
g.add_edge(1, 3).unwrap();
g.add_edge(1, 4).unwrap();
g.add_edge(1, 5).unwrap();
g.add_edge(2, 3).unwrap();
g.add_edge(2, 4).unwrap();
g.add_edge(2, 5).unwrap();
assert!(is_chain_graph(&g).unwrap());
}
}