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
//! `is_complete` predicate (ALGO-PR-016).
//!
//! Counterpart of `igraph_is_complete()` from
//! `references/igraph/src/properties/complete.c:43`. A graph is *complete*
//! if all pairs of distinct vertices are adjacent. The null graph and the
//! singleton graph are considered complete by convention (matches upstream).
//!
//! Algorithm: cardinality short-circuit then either a simple-graph
//! fast path or a per-vertex unique-neighbour scan.
//!
//! 1. `n <= 1` → `true`.
//! 2. Compute the target edge count for a complete graph on `n`
//! vertices: `n*(n-1)` (directed) or `n*(n-1)/2` (undirected).
//! Fall back to `false` immediately if the multiplication would
//! overflow `u64`.
//! 3. If `ecount < target` → `false`.
//! 4. If the graph is simple (no loops, no parallel edges) → return
//! `ecount == target` (must hold with equality once the lower
//! bound is met).
//! 5. Otherwise the graph has loops / multi-edges that pad `ecount`;
//! walk every vertex and require its set of unique non-self
//! neighbours to have size at least `n - 1`.
//!
//! Time complexity: `O(V + E)` worst case (the unique-neighbour scan
//! visits every incidence at most once).
use std::collections::HashSet;
use crate::core::Graph;
use crate::core::IgraphResult;
use crate::core::graph::VertexId;
use super::is_simple::{SimpleMode, is_simple_with_mode};
/// Returns `true` iff every pair of distinct vertices is adjacent.
///
/// The null graph (`n == 0`) and the singleton graph (`n == 1`) are
/// considered complete (matches `igraph_is_complete`).
///
/// On directed graphs both directions must be present for every pair
/// `(u, v)` with `u != v` — this matches upstream's
/// `igraph_neighbors(_, _, _, IGRAPH_OUT, NO_LOOPS, NO_MULTIPLE)`
/// counting and `igraph_is_simple(_, _, IGRAPH_DIRECTED)` short-circuit.
///
/// Counterpart of `igraph_is_complete` from
/// `references/igraph/src/properties/complete.c:43`.
///
/// # Examples
///
/// ```
/// use rust_igraph::{Graph, is_complete};
///
/// // Null graph — vacuously complete.
/// let g = Graph::with_vertices(0);
/// assert!(is_complete(&g).unwrap());
///
/// // K_3 — every pair adjacent.
/// let mut g = Graph::with_vertices(3);
/// g.add_edges(vec![(0u32, 1u32), (0, 2), (1, 2)]).unwrap();
/// assert!(is_complete(&g).unwrap());
///
/// // Path P_3 (0-1-2) — 0 and 2 not adjacent.
/// let mut g = Graph::with_vertices(3);
/// g.add_edges(vec![(0u32, 1u32), (1, 2)]).unwrap();
/// assert!(!is_complete(&g).unwrap());
/// ```
pub fn is_complete(graph: &Graph) -> IgraphResult<bool> {
let n = graph.vcount();
if n <= 1 {
return Ok(true);
}
let n64 = u64::from(n);
let m64 = graph.ecount() as u64;
let directed = graph.is_directed();
// n*(n-1) cannot overflow u64 since n is u32 (so ≤ 2^32).
let target_undir_x2 = n64.checked_mul(n64 - 1).expect("u64 mul fits");
let target = if directed {
target_undir_x2
} else {
target_undir_x2 / 2
};
if m64 < target {
return Ok(false);
}
// Fast path: a simple graph has no padding, so equality is exact.
if is_simple_with_mode(graph, SimpleMode::DirectedAsDirected)? {
return Ok(m64 == target);
}
// Slow path: graph has loops or multi-edges. For every vertex v,
// count distinct non-self out-neighbours (matches `IGRAPH_OUT`
// semantics with NO_LOOPS / NO_MULTIPLE in the C reference). On
// an undirected graph `Graph::neighbors` already returns all
// neighbours; the directed branch needs the out-direction only.
let n_us = n as usize;
let mut seen: HashSet<VertexId> = HashSet::with_capacity(n_us);
for v in 0..n {
seen.clear();
if directed {
let eids = graph.incident(v).expect("incident on valid vertex");
for eid in eids {
let nei = graph.edge_other(eid, v).expect("edge_other on valid edge");
if nei != v {
seen.insert(nei);
}
}
} else {
let neis = graph.neighbors(v).expect("neighbors on valid vertex");
for nei in neis {
if nei != v {
seen.insert(nei);
}
}
}
// n - 1 is required (every other vertex must appear once).
if seen.len() + 1 < n_us {
return Ok(false);
}
}
Ok(true)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::Graph;
#[test]
fn null_graph_is_complete() {
let g = Graph::with_vertices(0);
assert!(is_complete(&g).unwrap());
}
#[test]
fn null_directed_is_complete() {
let g = Graph::new(0, true).unwrap();
assert!(is_complete(&g).unwrap());
}
#[test]
fn singleton_undirected_is_complete() {
let g = Graph::with_vertices(1);
assert!(is_complete(&g).unwrap());
}
#[test]
fn singleton_directed_is_complete() {
let g = Graph::new(1, true).unwrap();
assert!(is_complete(&g).unwrap());
}
#[test]
fn singleton_with_self_loop_is_complete() {
// n == 1 short-circuits — the loop doesn't matter.
let mut g = Graph::with_vertices(1);
g.add_edge(0, 0).unwrap();
assert!(is_complete(&g).unwrap());
}
#[test]
fn k2_undirected_is_complete() {
let mut g = Graph::with_vertices(2);
g.add_edge(0, 1).unwrap();
assert!(is_complete(&g).unwrap());
}
#[test]
fn k3_undirected_is_complete() {
let mut g = Graph::with_vertices(3);
g.add_edges(vec![(0u32, 1u32), (0, 2), (1, 2)]).unwrap();
assert!(is_complete(&g).unwrap());
}
#[test]
fn k4_undirected_is_complete() {
let mut g = Graph::with_vertices(4);
g.add_edges(vec![(0u32, 1u32), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)])
.unwrap();
assert!(is_complete(&g).unwrap());
}
#[test]
fn path_is_not_complete() {
// P_3: 0-1-2 — 0 and 2 not adjacent.
let mut g = Graph::with_vertices(3);
g.add_edges(vec![(0u32, 1u32), (1, 2)]).unwrap();
assert!(!is_complete(&g).unwrap());
}
#[test]
fn missing_one_edge_is_not_complete() {
// K_4 minus the edge (2,3).
let mut g = Graph::with_vertices(4);
g.add_edges(vec![(0u32, 1u32), (0, 2), (0, 3), (1, 2), (1, 3)])
.unwrap();
assert!(!is_complete(&g).unwrap());
}
#[test]
fn k3_directed_needs_both_directions() {
// Three directed cycle 0→1→2→0 has the right edge count
// for a complete *undirected* K_3 (3 edges) but only one
// direction per pair, so it is NOT a complete directed graph.
let mut g = Graph::new(3, true).unwrap();
g.add_edges(vec![(0u32, 1u32), (1, 2), (2, 0)]).unwrap();
assert!(!is_complete(&g).unwrap());
}
#[test]
fn k3_directed_complete() {
// n*(n-1) = 6 directed edges for K_3.
let mut g = Graph::new(3, true).unwrap();
g.add_edges(vec![(0u32, 1u32), (1, 0), (0, 2), (2, 0), (1, 2), (2, 1)])
.unwrap();
assert!(is_complete(&g).unwrap());
}
#[test]
fn k2_directed_not_complete_with_only_one_arc() {
let mut g = Graph::new(2, true).unwrap();
g.add_edge(0, 1).unwrap();
assert!(!is_complete(&g).unwrap());
}
#[test]
fn k2_directed_complete_with_both_arcs() {
let mut g = Graph::new(2, true).unwrap();
g.add_edges(vec![(0u32, 1u32), (1, 0)]).unwrap();
assert!(is_complete(&g).unwrap());
}
#[test]
fn complete_with_extra_self_loops_still_complete() {
// K_3 plus a self-loop at vertex 0: ecount > target but every
// vertex still has the other two as unique non-self neighbours.
let mut g = Graph::with_vertices(3);
g.add_edges(vec![(0u32, 1u32), (0, 2), (1, 2), (0, 0)])
.unwrap();
assert!(is_complete(&g).unwrap());
}
#[test]
fn complete_with_parallel_edges_still_complete() {
// K_3 with a duplicate (0,1) edge: ecount > target but unique
// neighbour set per vertex still equals the other two.
let mut g = Graph::with_vertices(3);
g.add_edges(vec![(0u32, 1u32), (0, 2), (1, 2), (0, 1)])
.unwrap();
assert!(is_complete(&g).unwrap());
}
#[test]
fn parallel_edges_padding_does_not_help_missing_pair() {
// 4 vertices, edges (0,1) (0,2) (0,3) (1,2) (1,2) (1,2)
// ecount = 6 = K_4 target, but {2,3} and {3,1}? Let's check:
// vertex 3 only has 0 as neighbour — incomplete.
let mut g = Graph::with_vertices(4);
g.add_edges(vec![(0u32, 1u32), (0, 2), (0, 3), (1, 2), (1, 2), (1, 2)])
.unwrap();
assert!(!is_complete(&g).unwrap());
}
#[test]
fn ecount_too_low_short_circuit() {
// Graph deliberately too sparse — short-circuits on cardinality.
let mut g = Graph::with_vertices(10);
g.add_edge(0, 1).unwrap();
assert!(!is_complete(&g).unwrap());
}
#[test]
fn empty_n2_is_not_complete() {
// Two isolated vertices — needs 1 undirected edge to be K_2.
let g = Graph::with_vertices(2);
assert!(!is_complete(&g).unwrap());
}
#[test]
fn multi_edge_padding_to_target_but_missing_pair() {
// n=3, target_undir = 3 edges. Three parallel (0,1) edges
// hit the cardinality bound but vertex 2 is isolated.
let mut g = Graph::with_vertices(3);
g.add_edges(vec![(0u32, 1u32), (0, 1), (0, 1)]).unwrap();
assert!(!is_complete(&g).unwrap());
}
}