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
//! Dirac's condition for Hamiltonicity (ALGO-PR-118).
//!
//! A simple undirected graph on n ≥ 3 vertices satisfies Dirac's
//! condition if every vertex has degree ≥ n/2. By Dirac's theorem
//! (1952), such a graph is Hamiltonian (contains a Hamiltonian cycle).
//!
//! Returns `false` for directed graphs, graphs with n < 3, or
//! graphs failing the degree condition.
use crate::core::{Graph, IgraphResult};
/// Check whether a graph satisfies Dirac's condition.
///
/// Dirac's condition: every vertex has degree ≥ ⌈n/2⌉ (equivalently
/// ≥ n/2 for integer arithmetic). A graph satisfying this on n ≥ 3
/// vertices is guaranteed to be Hamiltonian.
///
/// Returns `false` for directed graphs or n < 3.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, satisfies_dirac};
///
/// // `K_4`: every vertex has degree 3 ≥ 4/2 = 2
/// 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!(satisfies_dirac(&g).unwrap());
///
/// // `C_5`: each vertex has degree 2 < 5/2 = 2.5 → fails
/// 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!(!satisfies_dirac(&g).unwrap());
/// ```
pub fn satisfies_dirac(graph: &Graph) -> IgraphResult<bool> {
if graph.is_directed() {
return Ok(false);
}
let n = graph.vcount();
if n < 3 {
return Ok(false);
}
let threshold = (n as usize).div_ceil(2);
for v in 0..n {
let deg = graph.degree(v)?;
if deg < threshold {
return Ok(false);
}
}
Ok(true)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_graph() {
let g = Graph::with_vertices(0);
assert!(!satisfies_dirac(&g).unwrap());
}
#[test]
fn single_vertex() {
let g = Graph::with_vertices(1);
assert!(!satisfies_dirac(&g).unwrap());
}
#[test]
fn two_vertices() {
let mut g = Graph::with_vertices(2);
g.add_edge(0, 1).unwrap();
assert!(!satisfies_dirac(&g).unwrap());
}
#[test]
fn triangle() {
// K_3: deg=2 ≥ 3/2=1 ✓
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!(satisfies_dirac(&g).unwrap());
}
#[test]
fn complete_k4() {
// K_4: deg=3 ≥ 4/2=2 ✓
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!(satisfies_dirac(&g).unwrap());
}
#[test]
fn complete_k5() {
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!(satisfies_dirac(&g).unwrap());
}
#[test]
fn c4() {
// C_4: deg=2 ≥ 4/2=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();
g.add_edge(3, 0).unwrap();
assert!(satisfies_dirac(&g).unwrap());
}
#[test]
fn c5_fails() {
// C_5: deg=2 < 5/2=2 (integer: 2 < 2 is false, but 2 < 2.5)
// threshold = 5/2 = 2 (integer division). deg=2 ≥ 2 ✓
// Actually with integer div, 5/2 = 2, and deg=2 ≥ 2 is true.
// Dirac's condition is deg ≥ n/2 where n/2 is real 2.5.
// So C_5 does NOT satisfy Dirac (deg 2 < 2.5).
// With integer: threshold should be ceil(n/2) = 3.
// Wait, Dirac's theorem states δ(G) ≥ n/2 (real division).
// For n=5, need deg ≥ 2.5, so min deg must be ≥ 3.
// C_5 has deg 2 < 3. So does NOT satisfy.
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!(!satisfies_dirac(&g).unwrap());
}
#[test]
fn path_p4_fails() {
// P_4: endpoint deg=1 < 4/2=2 → fails
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!(!satisfies_dirac(&g).unwrap());
}
#[test]
fn star_fails() {
// Star: leaves have deg 1 → fails
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!(!satisfies_dirac(&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!(!satisfies_dirac(&g).unwrap());
}
#[test]
fn complete_bipartite_k33() {
// K_{3,3}: deg=3 ≥ 6/2=3 ✓
let mut g = Graph::with_vertices(6);
for i in 0..3u32 {
for j in 3..6u32 {
g.add_edge(i, j).unwrap();
}
}
assert!(satisfies_dirac(&g).unwrap());
}
#[test]
fn k22_satisfies() {
// K_{2,2} = C_4: deg=2 ≥ 4/2=2 ✓
let mut g = Graph::with_vertices(4);
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!(satisfies_dirac(&g).unwrap());
}
}