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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//! Submodule providing the `ChordalDetection` trait and its blanket
//! implementation for undirected graphs.
use alloc::vec::Vec;
use num_traits::AsPrimitive;
use crate::traits::UndirectedMonopartiteMonoplexGraph;
fn maximum_cardinality_search_ordering_and_positions<G>(graph: &G) -> (Vec<G::NodeId>, Vec<usize>)
where
G: ?Sized + UndirectedMonopartiteMonoplexGraph,
{
let number_of_nodes = graph.number_of_nodes().as_();
let mut labels = vec![0usize; number_of_nodes];
let mut numbered = vec![false; number_of_nodes];
let mut positions = vec![usize::MAX; number_of_nodes];
let mut buckets: Vec<Vec<G::NodeId>> = vec![Vec::new(); number_of_nodes + 1];
let mut ordering = Vec::with_capacity(number_of_nodes);
let mut max_label = 0usize;
for node in graph.node_ids() {
buckets[0].push(node);
}
for reverse_position in 0..number_of_nodes {
let vertex = loop {
while buckets[max_label].is_empty() && max_label > 0 {
max_label -= 1;
}
let candidate = buckets[max_label].pop().expect("MCS bucket queue unexpectedly empty");
let candidate_index = candidate.as_();
if !numbered[candidate_index] && labels[candidate_index] == max_label {
break candidate;
}
};
let vertex_index = vertex.as_();
let position = number_of_nodes - reverse_position - 1;
numbered[vertex_index] = true;
positions[vertex_index] = position;
ordering.push(vertex);
for neighbor in graph.neighbors(vertex) {
let neighbor_index = neighbor.as_();
if numbered[neighbor_index] {
continue;
}
labels[neighbor_index] += 1;
let new_label = labels[neighbor_index];
buckets[new_label].push(neighbor);
if new_label > max_label {
max_label = new_label;
}
}
}
ordering.reverse();
(ordering, positions)
}
fn is_perfect_elimination_ordering_with_positions<G>(
graph: &G,
ordering: &[G::NodeId],
positions: &[usize],
) -> bool
where
G: ?Sized + UndirectedMonopartiteMonoplexGraph,
{
for &node in ordering {
let node_position = positions[node.as_()];
let mut parent_position = usize::MAX;
let mut parent = node;
for neighbor in graph.neighbors(node) {
let neighbor_position = positions[neighbor.as_()];
if neighbor_position > node_position && neighbor_position < parent_position {
parent_position = neighbor_position;
parent = neighbor;
}
}
if parent_position == usize::MAX {
continue;
}
for neighbor in graph.neighbors(node) {
let neighbor_position = positions[neighbor.as_()];
if neighbor_position > node_position
&& neighbor != parent
&& !graph.has_successor(parent, neighbor)
{
return false;
}
}
}
true
}
/// Trait providing chordality recognition utilities for undirected graphs.
///
/// The implementation uses Maximum Cardinality Search (MCS) to build a
/// candidate perfect elimination ordering (PEO), then verifies that the
/// ordering satisfies the clique condition on later neighbors.
///
/// # Examples
///
/// ```
/// use geometric_traits::{
/// impls::{SortedVec, SymmetricCSR2D},
/// prelude::*,
/// traits::{ChordalDetection, EdgesBuilder, VocabularyBuilder},
/// };
///
/// let nodes: Vec<usize> = vec![0, 1, 2, 3];
/// let edges: Vec<(usize, usize)> = vec![(0, 1), (1, 2), (1, 3), (2, 3)];
/// let nodes: SortedVec<usize> = GenericVocabularyBuilder::default()
/// .expected_number_of_symbols(nodes.len())
/// .symbols(nodes.into_iter().enumerate())
/// .build()
/// .unwrap();
/// let edges: SymmetricCSR2D<_> = UndiEdgesBuilder::default()
/// .expected_number_of_edges(edges.len())
/// .expected_shape(nodes.len())
/// .edges(edges.into_iter())
/// .build()
/// .unwrap();
/// let graph: UndiGraph<usize> = UndiGraph::from((nodes, edges));
///
/// assert!(graph.is_chordal());
/// assert!(graph.perfect_elimination_ordering().is_some());
/// ```
pub trait ChordalDetection: UndirectedMonopartiteMonoplexGraph {
/// Returns the vertex ordering produced by Maximum Cardinality Search.
///
/// The returned ordering is arranged from earliest to latest in the
/// candidate elimination order, so it is a perfect elimination ordering
/// exactly when the graph is chordal.
///
/// # Complexity
///
/// O(V + E) time and O(V + E) space.
///
/// # Examples
///
/// ```
/// use geometric_traits::{
/// impls::{SortedVec, SymmetricCSR2D},
/// prelude::*,
/// traits::{ChordalDetection, EdgesBuilder, VocabularyBuilder},
/// };
///
/// let nodes: Vec<usize> = vec![0, 1, 2, 3];
/// let edges: Vec<(usize, usize)> = vec![(0, 1), (1, 2), (1, 3)];
/// let nodes: SortedVec<usize> = GenericVocabularyBuilder::default()
/// .expected_number_of_symbols(nodes.len())
/// .symbols(nodes.into_iter().enumerate())
/// .build()
/// .unwrap();
/// let edges: SymmetricCSR2D<_> = UndiEdgesBuilder::default()
/// .expected_number_of_edges(edges.len())
/// .expected_shape(nodes.len())
/// .edges(edges.into_iter())
/// .build()
/// .unwrap();
/// let graph: UndiGraph<usize> = UndiGraph::from((nodes, edges));
///
/// assert!(graph.is_perfect_elimination_ordering(&graph.maximum_cardinality_search_ordering()));
/// ```
fn maximum_cardinality_search_ordering(&self) -> Vec<Self::NodeId> {
maximum_cardinality_search_ordering_and_positions(self).0
}
/// Returns whether the provided ordering is a valid perfect elimination
/// ordering for the graph.
///
/// The slice must contain every node exactly once and in elimination order
/// from earliest to latest.
///
/// # Complexity
///
/// O(V + E log V) time in the worst case and O(V) space.
///
/// # Examples
///
/// ```
/// use geometric_traits::{
/// impls::{SortedVec, SymmetricCSR2D},
/// prelude::*,
/// traits::{ChordalDetection, EdgesBuilder, VocabularyBuilder},
/// };
///
/// let nodes: Vec<usize> = vec![0, 1, 2];
/// let edges: Vec<(usize, usize)> = vec![(0, 1), (1, 2)];
/// let nodes: SortedVec<usize> = GenericVocabularyBuilder::default()
/// .expected_number_of_symbols(nodes.len())
/// .symbols(nodes.into_iter().enumerate())
/// .build()
/// .unwrap();
/// let edges: SymmetricCSR2D<_> = UndiEdgesBuilder::default()
/// .expected_number_of_edges(edges.len())
/// .expected_shape(nodes.len())
/// .edges(edges.into_iter())
/// .build()
/// .unwrap();
/// let graph: UndiGraph<usize> = UndiGraph::from((nodes, edges));
///
/// assert!(graph.is_perfect_elimination_ordering(&[0, 1, 2]));
/// assert!(!graph.is_perfect_elimination_ordering(&[1, 0, 2]));
/// ```
fn is_perfect_elimination_ordering(&self, ordering: &[Self::NodeId]) -> bool {
if self.has_self_loops() {
return false;
}
let number_of_nodes = self.number_of_nodes().as_();
if ordering.len() != number_of_nodes {
return false;
}
let mut positions = vec![usize::MAX; number_of_nodes];
for (position, &node) in ordering.iter().enumerate() {
let node_index = node.as_();
if node_index >= number_of_nodes || positions[node_index] != usize::MAX {
return false;
}
positions[node_index] = position;
}
is_perfect_elimination_ordering_with_positions(self, ordering, &positions)
}
/// Returns a perfect elimination ordering when the graph is chordal.
///
/// Returns `None` for non-chordal graphs or graphs with self-loops.
///
/// # Complexity
///
/// O(V + E log V) time in the worst case and O(V + E) space.
///
/// # Examples
///
/// ```
/// use geometric_traits::{
/// impls::{SortedVec, SymmetricCSR2D},
/// prelude::*,
/// traits::{ChordalDetection, EdgesBuilder, VocabularyBuilder},
/// };
///
/// let nodes: Vec<usize> = vec![0, 1, 2, 3];
/// let edges: Vec<(usize, usize)> = vec![(0, 1), (1, 2), (1, 3), (2, 3)];
/// let nodes: SortedVec<usize> = GenericVocabularyBuilder::default()
/// .expected_number_of_symbols(nodes.len())
/// .symbols(nodes.into_iter().enumerate())
/// .build()
/// .unwrap();
/// let edges: SymmetricCSR2D<_> = UndiEdgesBuilder::default()
/// .expected_number_of_edges(edges.len())
/// .expected_shape(nodes.len())
/// .edges(edges.into_iter())
/// .build()
/// .unwrap();
/// let graph: UndiGraph<usize> = UndiGraph::from((nodes, edges));
///
/// assert!(graph.perfect_elimination_ordering().is_some());
/// ```
#[inline]
fn perfect_elimination_ordering(&self) -> Option<Vec<Self::NodeId>> {
if self.has_self_loops() {
return None;
}
let (ordering, positions) = maximum_cardinality_search_ordering_and_positions(self);
is_perfect_elimination_ordering_with_positions(self, &ordering, &positions)
.then_some(ordering)
}
/// Returns whether the graph is chordal.
///
/// Chordal graphs are exactly the undirected graphs that admit a perfect
/// elimination ordering.
///
/// # Complexity
///
/// O(V + E log V) time in the worst case and O(V + E) space.
///
/// # Examples
///
/// ```
/// use geometric_traits::{
/// impls::{SortedVec, SymmetricCSR2D},
/// prelude::*,
/// traits::{ChordalDetection, EdgesBuilder, VocabularyBuilder},
/// };
///
/// let nodes: Vec<usize> = vec![0, 1, 2, 3];
/// let edges: Vec<(usize, usize)> = vec![(0, 1), (0, 3), (1, 2), (2, 3)];
/// let nodes: SortedVec<usize> = GenericVocabularyBuilder::default()
/// .expected_number_of_symbols(nodes.len())
/// .symbols(nodes.into_iter().enumerate())
/// .build()
/// .unwrap();
/// let edges: SymmetricCSR2D<_> = UndiEdgesBuilder::default()
/// .expected_number_of_edges(edges.len())
/// .expected_shape(nodes.len())
/// .edges(edges.into_iter())
/// .build()
/// .unwrap();
/// let graph: UndiGraph<usize> = UndiGraph::from((nodes, edges));
///
/// assert!(!graph.is_chordal());
/// ```
#[inline]
fn is_chordal(&self) -> bool {
if self.has_self_loops() {
return false;
}
let (ordering, positions) = maximum_cardinality_search_ordering_and_positions(self);
is_perfect_elimination_ordering_with_positions(self, &ordering, &positions)
}
}
impl<G: ?Sized + UndirectedMonopartiteMonoplexGraph> ChordalDetection for G {}