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
//! Acyclic predicate (ALGO-PR-022).
//!
//! Counterpart of `igraph_is_acyclic()` from
//! `references/igraph/src/properties/trees.c:753`. Returns `true`
//! iff `graph` contains no cycle (of any length). For directed
//! graphs this delegates to [`crate::is_dag`]; for undirected
//! graphs we run union-find over the edges — a cycle is the first
//! edge whose endpoints are already in the same connected
//! component. Self-loops and parallel edges count as cycles.
//!
//! Time complexity: `O(V + E · α(E))` where `α` is the inverse
//! Ackermann function (effectively `O(V + E)`).
use crate::algorithms::properties::is_dag::is_dag;
use crate::core::Graph;
/// Returns `true` iff `graph` contains no cycle.
///
/// For directed graphs this is equivalent to [`crate::is_dag`].
/// For undirected graphs, a cycle is any non-trivial closed walk —
/// in particular self-loops and parallel undirected edges count
/// as cycles.
///
/// Algorithm:
/// - Directed: delegate to `is_dag` (matches upstream).
/// - Undirected: walk every edge; for each `(u, v)` use union-find
/// to check whether `u` and `v` are already connected. If yes,
/// we just closed a cycle ⇒ return false. Otherwise union and
/// continue.
///
/// Counterpart of `igraph_is_acyclic` from
/// `references/igraph/src/properties/trees.c:753`.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, is_acyclic};
///
/// // Undirected tree 0-1-2-3: acyclic.
/// 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_acyclic(&g));
///
/// // Triangle 0-1-2-0: cyclic.
/// 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_acyclic(&g));
/// ```
#[must_use]
pub fn is_acyclic(graph: &Graph) -> bool {
if graph.is_directed() {
return is_dag(graph);
}
let n = graph.vcount();
let m = graph.ecount();
let n_us = n as usize;
// Trivial early outs: no vertices or no edges ⇒ no cycle.
if n == 0 || m == 0 {
return true;
}
// Union-find with path compression + union-by-rank-ish sizes.
let mut parent: Vec<u32> = (0..n).collect();
let mut size: Vec<u32> = vec![1; n_us];
let Ok(m_u32) = u32::try_from(m) else {
// Edge count beyond u32::MAX is impossible (EdgeId is u32),
// but if it did happen we'd lose precision: treat as cyclic
// to be safe.
return false;
};
for eid in 0..m_u32 {
// Should not happen on a well-formed Graph; fall back to
// "cyclic" (conservative).
let Ok((u, v)) = graph.edge(eid) else {
return false;
};
if u == v {
// Self-loop: instant cycle.
return false;
}
let mut ru = u as usize;
while parent[ru] as usize != ru {
parent[ru] = parent[parent[ru] as usize];
ru = parent[ru] as usize;
}
let mut rv = v as usize;
while parent[rv] as usize != rv {
parent[rv] = parent[parent[rv] as usize];
rv = parent[rv] as usize;
}
if ru == rv {
// Already in the same component ⇒ this edge closes a
// cycle. Parallel undirected edges land here on the
// second occurrence.
return false;
}
// Union by size: attach smaller under larger.
let (parent_idx, child_idx) = if size[ru] < size[rv] {
(rv, ru)
} else {
(ru, rv)
};
let Ok(parent_u32) = u32::try_from(parent_idx) else {
return false;
};
parent[child_idx] = parent_u32;
size[parent_idx] = size[parent_idx].saturating_add(size[child_idx]);
}
true
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::Graph;
// -------- Undirected cases --------
#[test]
fn empty_undirected_is_acyclic() {
let g = Graph::with_vertices(0);
assert!(is_acyclic(&g));
}
#[test]
fn isolated_vertices_only_is_acyclic() {
let g = Graph::with_vertices(5);
assert!(is_acyclic(&g));
}
#[test]
fn undirected_tree_is_acyclic() {
let mut g = Graph::with_vertices(4);
g.add_edges(vec![(0u32, 1u32), (1, 2), (2, 3)]).unwrap();
assert!(is_acyclic(&g));
}
#[test]
fn undirected_forest_is_acyclic() {
// Two disjoint trees: 0-1-2 and 3-4.
let mut g = Graph::with_vertices(5);
g.add_edges(vec![(0u32, 1u32), (1, 2), (3, 4)]).unwrap();
assert!(is_acyclic(&g));
}
#[test]
fn undirected_triangle_is_not_acyclic() {
let mut g = Graph::with_vertices(3);
g.add_edges(vec![(0u32, 1u32), (1, 2), (2, 0)]).unwrap();
assert!(!is_acyclic(&g));
}
#[test]
fn undirected_self_loop_is_not_acyclic() {
let mut g = Graph::with_vertices(2);
g.add_edge(0, 0).unwrap();
g.add_edge(0, 1).unwrap();
assert!(!is_acyclic(&g));
}
#[test]
fn undirected_parallel_edge_is_not_acyclic() {
// Two parallel edges between 0 and 1 form a 2-cycle.
let mut g = Graph::with_vertices(2);
g.add_edge(0, 1).unwrap();
g.add_edge(0, 1).unwrap();
assert!(!is_acyclic(&g));
}
#[test]
fn undirected_star_is_acyclic() {
// Star around vertex 0.
let mut g = Graph::with_vertices(5);
g.add_edges(vec![(0u32, 1u32), (0, 2), (0, 3), (0, 4)])
.unwrap();
assert!(is_acyclic(&g));
}
// -------- Directed cases (delegated to is_dag) --------
#[test]
fn directed_dag_chain_is_acyclic() {
let mut g = Graph::new(3, true).unwrap();
g.add_edges(vec![(0u32, 1u32), (1, 2)]).unwrap();
assert!(is_acyclic(&g));
}
#[test]
fn directed_cycle_is_not_acyclic() {
let mut g = Graph::new(3, true).unwrap();
g.add_edges(vec![(0u32, 1u32), (1, 2), (2, 0)]).unwrap();
assert!(!is_acyclic(&g));
}
#[test]
fn directed_self_loop_is_not_acyclic() {
let mut g = Graph::new(2, true).unwrap();
g.add_edge(0, 0).unwrap();
g.add_edge(0, 1).unwrap();
assert!(!is_acyclic(&g));
}
#[test]
fn directed_diamond_dag_is_acyclic() {
// 0→1, 0→2, 1→3, 2→3.
let mut g = Graph::new(4, true).unwrap();
g.add_edges(vec![(0u32, 1u32), (0, 2), (1, 3), (2, 3)])
.unwrap();
assert!(is_acyclic(&g));
}
#[test]
fn undirected_no_edges_with_many_vertices_is_acyclic() {
let g = Graph::with_vertices(100);
assert!(is_acyclic(&g));
}
}