spqr-tree 5.1.0

A representation of the SPQR tree in Rust with support for I/O
Documentation
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#![expect(clippy::type_complexity)]

use smallvec::SmallVec;
use tagged_vec::TaggedVec;

use crate::{
    decomposition::indices::{
        BlockIndex, ComponentIndex, CutNodeIndex, GraphIndex, GraphIndexInteger,
        OptionalBlockIndex, OptionalCutNodeIndex, OptionalSPQRNodeIndex, SPQREdgeIndex,
        SPQRNodeIndex,
    },
    graph::StaticGraph,
};

pub mod builder;
pub mod indices;

/// Represents the SPQR decomposition as an augmentation over a graph.
///
/// The SPQR decomposition is a hierarchical decomposition of the graph first into its connected components,
/// then into the biconnected components of each connected component,
/// and finally into the triconnected components of each biconnected component.
///
/// The decomposition of a connected component into its biconnected components is called the [block cut tree](https://en.wikipedia.org/wiki/Biconnected_component#Block-cut_tree).
/// The decomposition of a biconnected component into its triconnected components is called the [SPQR tree](https://en.wikipedia.org/wiki/SPQR_tree).
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SPQRDecomposition<'graph, Graph: StaticGraph> {
    pub(crate) graph: &'graph Graph,
    pub(crate) components: TaggedVec<
        ComponentIndex<Graph::IndexType>,
        Component<Graph::NodeIndex, Graph::EdgeIndex, Graph::IndexType>,
    >,
    pub(crate) blocks: TaggedVec<
        BlockIndex<Graph::IndexType>,
        Block<Graph::NodeIndex, Graph::EdgeIndex, Graph::IndexType>,
    >,
    pub(crate) cut_nodes:
        TaggedVec<CutNodeIndex<Graph::IndexType>, CutNode<Graph::NodeIndex, Graph::IndexType>>,
    pub(crate) spqr_nodes: TaggedVec<
        SPQRNodeIndex<Graph::IndexType>,
        SPQRNode<Graph::NodeIndex, Graph::EdgeIndex, Graph::IndexType>,
    >,
    pub(crate) spqr_edges:
        TaggedVec<SPQREdgeIndex<Graph::IndexType>, SPQREdge<Graph::NodeIndex, Graph::IndexType>>,
    pub(crate) node_data: TaggedVec<Graph::NodeIndex, SPQRDecompositionNodeData<Graph::IndexType>>,
    pub(crate) edge_data: TaggedVec<Graph::EdgeIndex, SPQRDecompositionEdgeData<Graph::IndexType>>,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Component<NodeIndex, EdgeIndex, IndexType> {
    pub(crate) nodes: Vec<NodeIndex>,
    /// Only populated if the components has exactly one node.
    pub(crate) edges: Vec<EdgeIndex>,
    /// Only populated if the component has at least two nodes.
    pub(crate) blocks: Vec<BlockIndex<IndexType>>,
    /// Only populated if the component has at least two nodes.
    pub(crate) cut_nodes: Vec<CutNodeIndex<IndexType>>,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Block<NodeIndex, EdgeIndex, IndexType> {
    pub(crate) component: ComponentIndex<IndexType>,
    pub(crate) nodes: Vec<NodeIndex>,
    pub(crate) cut_nodes: Vec<CutNodeIndex<IndexType>>,
    /// Only populated if the block has less than three nodes.
    pub(crate) edges: Vec<EdgeIndex>,
    /// Only populated if the block has at least three nodes.
    pub(crate) spqr_nodes: Vec<SPQRNodeIndex<IndexType>>,
    /// Only populated if the block has at least three nodes.
    pub(crate) spqr_edges: Vec<SPQREdgeIndex<IndexType>>,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CutNode<NodeIndex, IndexType> {
    pub(crate) component: ComponentIndex<IndexType>,
    pub(crate) node: NodeIndex,
    pub(crate) adjacent_blocks: SmallVec<[BlockIndex<IndexType>; 2]>,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SPQRNode<NodeIndex, EdgeIndex, IndexType> {
    pub(crate) block: BlockIndex<IndexType>,
    pub(crate) nodes: Vec<NodeIndex>,
    pub(crate) edges: Vec<EdgeIndex>,
    pub(crate) spqr_node_type: SPQRNodeType,
    pub(crate) spqr_edges: SmallVec<[SPQREdgeIndex<IndexType>; 2]>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SPQRNodeType {
    SNode,
    PNode,
    RNode,
}

/// An edge in the SPQR tree connecting two SPQR nodes.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SPQREdge<NodeIndex, IndexType> {
    pub(crate) endpoints: (SPQRNodeIndex<IndexType>, SPQRNodeIndex<IndexType>),
    pub(crate) virtual_edge: (NodeIndex, NodeIndex),
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct SPQRDecompositionNodeData<IndexType: GraphIndexInteger> {
    pub(crate) component_index: ComponentIndex<IndexType>,
    pub(crate) block_indices: SmallVec<[BlockIndex<IndexType>; 1]>,
    pub(crate) cut_node_index: OptionalCutNodeIndex<IndexType>,
    pub(crate) spqr_node_indices: SmallVec<[SPQRNodeIndex<IndexType>; 1]>,
    pub(crate) extra_data: String,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct SPQRDecompositionEdgeData<IndexType: GraphIndexInteger> {
    pub(crate) component_index: ComponentIndex<IndexType>,
    pub(crate) block_index: OptionalBlockIndex<IndexType>,
    pub(crate) spqr_node_index: OptionalSPQRNodeIndex<IndexType>,
    pub(crate) extra_data: String,
}

impl<'graph, Graph: StaticGraph> SPQRDecomposition<'graph, Graph> {
    pub fn graph(&self) -> &'graph Graph {
        self.graph
    }

    pub fn component_count(&self) -> usize {
        self.components.len()
    }

    pub fn block_count(&self) -> usize {
        self.blocks.len()
    }

    pub fn spqr_node_count(&self) -> usize {
        self.spqr_nodes.len()
    }

    pub fn iter_component_indices(&self) -> impl Iterator<Item = ComponentIndex<Graph::IndexType>> {
        self.components.iter_indices(..)
    }

    pub fn iter_components(
        &self,
    ) -> impl Iterator<
        Item = (
            ComponentIndex<Graph::IndexType>,
            &Component<Graph::NodeIndex, Graph::EdgeIndex, Graph::IndexType>,
        ),
    > {
        self.components.iter(..)
    }

    pub fn iter_blocks(
        &self,
    ) -> impl Iterator<
        Item = (
            BlockIndex<Graph::IndexType>,
            &Block<Graph::NodeIndex, Graph::EdgeIndex, Graph::IndexType>,
        ),
    > {
        self.blocks.iter(..)
    }

    pub fn iter_spqr_nodes(
        &self,
    ) -> impl Iterator<
        Item = (
            SPQRNodeIndex<Graph::IndexType>,
            &SPQRNode<Graph::NodeIndex, Graph::EdgeIndex, Graph::IndexType>,
        ),
    > {
        self.spqr_nodes.iter(..)
    }

    pub fn iter_blocks_in_component(
        &self,
        component_index: ComponentIndex<Graph::IndexType>,
    ) -> impl Iterator<
        Item = (
            BlockIndex<Graph::IndexType>,
            &Block<Graph::NodeIndex, Graph::EdgeIndex, Graph::IndexType>,
        ),
    > {
        self.components[component_index]
            .blocks
            .iter()
            .copied()
            .map(move |block_index| (block_index, &self.blocks[block_index]))
    }

    pub fn iter_spqr_nodes_in_block(
        &self,
        block_index: BlockIndex<Graph::IndexType>,
    ) -> impl Iterator<
        Item = (
            SPQRNodeIndex<Graph::IndexType>,
            &SPQRNode<Graph::NodeIndex, Graph::EdgeIndex, Graph::IndexType>,
        ),
    > {
        self.blocks[block_index]
            .spqr_nodes
            .iter()
            .copied()
            .map(move |spqr_node_index| (spqr_node_index, &self.spqr_nodes[spqr_node_index]))
    }

    pub fn iter_spqr_edges_in_block(
        &self,
        block_index: BlockIndex<Graph::IndexType>,
    ) -> impl Iterator<
        Item = (
            SPQREdgeIndex<Graph::IndexType>,
            &SPQREdge<Graph::NodeIndex, Graph::IndexType>,
        ),
    > {
        self.blocks[block_index]
            .spqr_edges
            .iter()
            .copied()
            .map(move |spqr_edge_index| (spqr_edge_index, &self.spqr_edges[spqr_edge_index]))
    }

    pub fn iter_nodes(&self) -> impl Iterator<Item = Graph::NodeIndex> {
        self.graph.node_indices()
    }

    pub fn node_extra_data(&self, node_index: Graph::NodeIndex) -> &str {
        &self.node_data[node_index].extra_data
    }

    pub fn edge_extra_data(&self, edge_index: Graph::EdgeIndex) -> &str {
        &self.edge_data[edge_index].extra_data
    }

    pub fn spqr_node_name(&self, spqr_node_index: SPQRNodeIndex<Graph::IndexType>) -> String {
        match self.spqr_nodes[spqr_node_index].spqr_node_type() {
            SPQRNodeType::SNode => format!("S{spqr_node_index}"),
            SPQRNodeType::PNode => format!("P{spqr_node_index}"),
            SPQRNodeType::RNode => format!("R{spqr_node_index}"),
        }
    }

    pub fn node_component_index(
        &self,
        node_index: Graph::NodeIndex,
    ) -> ComponentIndex<Graph::IndexType> {
        self.node_data[node_index].component_index
    }

    pub fn node_block_indices(
        &self,
        node_index: Graph::NodeIndex,
    ) -> impl Iterator<Item = BlockIndex<Graph::IndexType>> {
        self.node_data[node_index].block_indices.iter().copied()
    }

    pub fn node_spqr_node_indices(
        &self,
        node_index: Graph::NodeIndex,
    ) -> impl Iterator<Item = SPQRNodeIndex<Graph::IndexType>> {
        self.node_data[node_index].spqr_node_indices.iter().copied()
    }

    pub fn cut_node_index_to_node_index(
        &self,
        cut_node_index: CutNodeIndex<Graph::IndexType>,
    ) -> Graph::NodeIndex {
        self.cut_nodes[cut_node_index].node
    }

    pub fn cut_node(
        &self,
        cut_node_index: CutNodeIndex<Graph::IndexType>,
    ) -> &CutNode<Graph::NodeIndex, Graph::IndexType> {
        &self.cut_nodes[cut_node_index]
    }

    /// Returns true if the given node is a cut node.
    pub fn is_cut_node(&self, node_index: Graph::NodeIndex) -> bool {
        self.node_data[node_index].cut_node_index.is_some()
    }

    pub fn spqr_edge(
        &self,
        spqr_edge_index: SPQREdgeIndex<Graph::IndexType>,
    ) -> &SPQREdge<Graph::NodeIndex, Graph::IndexType> {
        &self.spqr_edges[spqr_edge_index]
    }

    /// Returns true if the given node has an incident virtual edge.
    ///
    /// This also means that the node is part of at least two SPQR nodes.
    pub fn has_incident_virtual_edge(&self, node_index: Graph::NodeIndex) -> bool {
        self.node_data[node_index].spqr_node_indices.len() >= 2
    }
}

impl<NodeIndex: Copy, EdgeIndex: Copy, IndexType: Copy> Component<NodeIndex, EdgeIndex, IndexType> {
    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }

    pub fn edge_count(&self) -> usize {
        self.edges.len()
    }

    pub fn block_count(&self) -> usize {
        self.blocks.len()
    }

    pub fn iter_nodes(&self) -> impl Iterator<Item = NodeIndex> {
        self.nodes.iter().copied()
    }

    pub fn iter_edges(&self) -> impl Iterator<Item = EdgeIndex> {
        self.edges.iter().copied()
    }

    pub fn iter_cut_nodes(&self) -> impl Iterator<Item = CutNodeIndex<IndexType>> {
        self.cut_nodes.iter().copied()
    }
}

impl<NodeIndex: Copy, EdgeIndex: Copy, IndexType: Copy> Block<NodeIndex, EdgeIndex, IndexType> {
    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }

    pub fn edge_count(&self) -> usize {
        self.edges.len()
    }

    pub fn spqr_node_count(&self) -> usize {
        self.spqr_nodes.len()
    }

    pub fn iter_nodes(&self) -> impl Iterator<Item = NodeIndex> {
        self.nodes.iter().copied()
    }

    pub fn iter_cut_nodes(&self) -> impl Iterator<Item = CutNodeIndex<IndexType>> {
        self.cut_nodes.iter().copied()
    }

    pub fn iter_edges(&self) -> impl Iterator<Item = EdgeIndex> {
        self.edges.iter().copied()
    }
}

impl<NodeIndex: Copy, IndexType: Copy> CutNode<NodeIndex, IndexType> {
    pub fn component(&self) -> ComponentIndex<IndexType> {
        self.component
    }

    pub fn node(&self) -> NodeIndex {
        self.node
    }

    pub fn iter_adjacent_blocks(&self) -> impl Iterator<Item = BlockIndex<IndexType>> {
        self.adjacent_blocks.iter().copied()
    }
}

impl<NodeIndex: Copy, EdgeIndex: Copy, IndexType: Copy> SPQRNode<NodeIndex, EdgeIndex, IndexType> {
    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }

    /// The amount of real edges in the SPQR node, also known as Q-nodes.
    pub fn real_edge_count(&self) -> usize {
        self.edges.len()
    }

    /// The amount of virtual edges in the SPQR node.
    pub fn virtual_edge_count(&self) -> usize {
        self.spqr_edges.len()
    }

    /// The amount of edges in the skeleton graph of the SPQR node.
    pub fn skeleton_edge_count(&self) -> usize {
        self.real_edge_count() + self.virtual_edge_count()
    }

    pub fn block(&self) -> BlockIndex<IndexType> {
        self.block
    }

    pub fn iter_nodes(&self) -> impl Iterator<Item = NodeIndex> {
        self.nodes.iter().copied()
    }

    pub fn p_node_poles(&self) -> Option<(NodeIndex, NodeIndex)> {
        if self.spqr_node_type == SPQRNodeType::PNode {
            Some((self.nodes[0], self.nodes[1]))
        } else {
            None
        }
    }

    pub fn iter_edges(&self) -> impl Iterator<Item = EdgeIndex> {
        self.edges.iter().copied()
    }

    pub fn spqr_node_type(&self) -> SPQRNodeType {
        self.spqr_node_type
    }

    pub fn iter_incident_spqr_edges(&self) -> impl Iterator<Item = SPQREdgeIndex<IndexType>> {
        self.spqr_edges.iter().copied()
    }

    pub fn is_s_node(&self) -> bool {
        self.spqr_node_type == SPQRNodeType::SNode
    }

    pub fn is_p_node(&self) -> bool {
        self.spqr_node_type == SPQRNodeType::PNode
    }

    pub fn is_r_node(&self) -> bool {
        self.spqr_node_type == SPQRNodeType::RNode
    }
}

impl<NodeIndex: GraphIndex, IndexType: GraphIndexInteger> SPQREdge<NodeIndex, IndexType> {
    pub fn endpoints(&self) -> (SPQRNodeIndex<IndexType>, SPQRNodeIndex<IndexType>) {
        self.endpoints
    }

    /// Returns the endpoint with the smaller index.
    pub fn min_endpoint(&self) -> SPQRNodeIndex<IndexType> {
        self.endpoints.0.min(self.endpoints.1)
    }

    /// Returns the endpoint with the larger index.
    pub fn max_endpoint(&self) -> SPQRNodeIndex<IndexType> {
        self.endpoints.0.max(self.endpoints.1)
    }

    pub fn virtual_edge(&self) -> (NodeIndex, NodeIndex) {
        self.virtual_edge
    }

    pub fn ordered_virtual_edge(&self) -> (NodeIndex, NodeIndex) {
        let (u, v) = self.virtual_edge();
        if u < v { (u, v) } else { (v, u) }
    }
}