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
//! Proper interval graph predicate (ALGO-PR-115).
//!
//! A graph is a proper interval graph (equivalently, unit interval
//! graph) if it can be represented as an intersection graph of
//! intervals on the real line where no interval properly contains
//! another. This is equivalent to being both an interval graph and
//! claw-free, or equivalently chordal + claw-free + AT-free.
//!
//! A simpler characterization: a graph is a proper interval graph iff
//! it is chordal and does not contain an induced claw (`K_{1,3}`) and
//! admits a vertex ordering where the closed neighborhoods are
//! consecutive (consecutive-1's property in the augmented adjacency
//! matrix). We use the characterization: proper interval ⟺ chordal +
//! claw-free (Roberts 1969 / Wegner 1967).
//!
//! Returns `false` for directed graphs.
use crate::algorithms::chordality::is_chordal;
use crate::algorithms::properties::is_claw_free::is_claw_free;
use crate::core::{Graph, IgraphResult};
/// Check whether a graph is a proper interval graph.
///
/// A proper interval graph is chordal and claw-free. Equivalently,
/// it can be represented by unit-length intervals on the real line.
///
/// Returns `false` for directed graphs.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, is_proper_interval};
///
/// // Path `P_4` is a proper interval 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_proper_interval(&g).unwrap());
///
/// // Star `S_3` is NOT proper interval (has a claw)
/// 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_proper_interval(&g).unwrap());
/// ```
pub fn is_proper_interval(graph: &Graph) -> IgraphResult<bool> {
if graph.is_directed() {
return Ok(false);
}
let ch = is_chordal(graph, None)?;
if !ch.chordal {
return Ok(false);
}
is_claw_free(graph)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_graph() {
let g = Graph::with_vertices(0);
assert!(is_proper_interval(&g).unwrap());
}
#[test]
fn single_vertex() {
let g = Graph::with_vertices(1);
assert!(is_proper_interval(&g).unwrap());
}
#[test]
fn single_edge() {
let mut g = Graph::with_vertices(2);
g.add_edge(0, 1).unwrap();
assert!(is_proper_interval(&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_proper_interval(&g).unwrap());
}
#[test]
fn path_p4() {
// P_4: 0-1-2-3. Chordal (no induced C_4+), claw-free (max degree 2).
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_proper_interval(&g).unwrap());
}
#[test]
fn path_p5() {
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_proper_interval(&g).unwrap());
}
#[test]
fn complete_k4() {
let mut g = Graph::with_vertices(4);
for i in 0..4u32 {
for j in (i + 1)..4 {
g.add_edge(i, j).unwrap();
}
}
assert!(is_proper_interval(&g).unwrap());
}
#[test]
fn star_not_proper_interval() {
// Star S_3: has claw K_{1,3} → NOT proper interval
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_proper_interval(&g).unwrap());
}
#[test]
fn c4_not_proper_interval() {
// C_4: not chordal → NOT proper interval
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_proper_interval(&g).unwrap());
}
#[test]
fn c5_not_proper_interval() {
// C_5: not chordal → NOT proper interval
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_proper_interval(&g).unwrap());
}
#[test]
fn diamond() {
// Diamond: K_4 minus one edge. Chordal, max degree 3 but no
// induced claw (the degree-3 vertices have two mutual neighbors).
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();
// missing 2-3
assert!(is_proper_interval(&g).unwrap());
}
#[test]
fn edgeless() {
let g = Graph::with_vertices(4);
assert!(is_proper_interval(&g).unwrap());
}
#[test]
fn sun_3_not_proper_interval() {
// Sun S_3: chordal but has a claw (outer vertex + 2 inner nbrs +
// another outer vertex). Actually let's check: outer vertex 3 has
// neighbors 0,1. Degree 2, can't form claw from 3.
// Inner vertex 0 has neighbors 1,2,3,5 (degree 4). Check if 0
// has 3 independent neighbors: 3 adj to 1 ✓, 5 adj to 2 ✓,
// 3 adj to 5? No edge → 3,2,5 independent? 2 adj 5 ✓ → not independent.
// Actually: N(0) = {1, 2, 5, 3}. Are {1,5,3} independent?
// 1-5? No. 1-3? Yes (edge). So {1,3} not independent.
// {2,5,3}: 2-5? Yes. Not independent.
// This might not have a claw. Sun_3 is chordal but not strongly
// chordal. It IS interval and proper interval depends on claw-free.
// Let me use a different example.
// Tree with branching: 0-1, 0-2, 0-3, 1-4 → claw at 0 (leaves 2,3
// and 1 independent). Chordal (tree). Has claw → NOT proper interval.
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_proper_interval(&g).unwrap());
}
#[test]
fn caterpillar_with_claw() {
// 0-1-2-3, 1-4, 1-5. Vertex 1 has neighbors {0,2,4,5}.
// {0,4,5} pairwise non-adjacent → claw at 1 → NOT proper interval.
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(1, 4).unwrap();
g.add_edge(1, 5).unwrap();
assert!(!is_proper_interval(&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_proper_interval(&g).unwrap());
}
}