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
//! Submodule defining the properties of a monopartite graph.
//!
//! A monopartite graph is a graph where the nodes are of the same type, i.e
//! they are not divided into different partitions.
use num_traits::{AsPrimitive, SaturatingAdd, Zero};
use super::{BidirectionalVocabulary, Edges, Vocabulary};
use crate::traits::{PositiveInteger, SparseSquareMatrix, Symbol, TryFromUsize};
/// Trait defining the properties of the monopartited edges of a graph.
pub trait MonopartiteEdges:
Edges<
SourceNodeId = <Self as MonopartiteEdges>::NodeId,
DestinationNodeId = <Self as MonopartiteEdges>::NodeId,
Matrix = <Self as MonopartiteEdges>::MonopartiteMatrix,
>
{
/// The monopartited matrix of the graph.
type MonopartiteMatrix: SparseSquareMatrix<Index = Self::NodeId>;
/// The identifier of the node.
type NodeId: PositiveInteger + AsPrimitive<usize> + TryFromUsize + SaturatingAdd;
/// Returns whether the graph has self-loops.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "alloc")]
/// # {
/// use geometric_traits::{
/// impls::{SortedVec, SquareCSR2D},
/// prelude::*,
/// traits::{EdgesBuilder, MonopartiteEdges, VocabularyBuilder},
/// };
///
/// let nodes: Vec<usize> = vec![0, 1, 2, 3];
/// let edges: Vec<(usize, usize)> = vec![(0, 1), (0, 2), (1, 2), (3, 3)];
/// let nodes: SortedVec<usize> = GenericVocabularyBuilder::default()
/// .expected_number_of_symbols(nodes.len())
/// .symbols(nodes.into_iter().enumerate())
/// .build()
/// .unwrap();
/// let edges: SquareCSR2D<_> = DiEdgesBuilder::default()
/// .expected_number_of_edges(edges.len())
/// .expected_shape(nodes.len())
/// .edges(edges.into_iter())
/// .build()
/// .unwrap();
/// let graph: DiGraph<usize> = DiGraph::from((nodes, edges));
///
/// assert!(graph.edges().has_self_loops());
/// # }
/// ```
#[inline]
fn has_self_loops(&self) -> bool {
self.number_of_self_loops() > Self::NodeId::zero()
}
/// Returns the number of self-loops in the graph.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "alloc")]
/// # {
/// use geometric_traits::{
/// impls::{SortedVec, SquareCSR2D},
/// prelude::*,
/// traits::{EdgesBuilder, MonopartiteEdges, VocabularyBuilder},
/// };
///
/// let nodes: Vec<usize> = vec![0, 1, 2, 3];
/// let edges: Vec<(usize, usize)> = vec![(0, 1), (0, 2), (1, 2), (3, 3)];
/// let nodes: SortedVec<usize> = GenericVocabularyBuilder::default()
/// .expected_number_of_symbols(nodes.len())
/// .symbols(nodes.into_iter().enumerate())
/// .build()
/// .unwrap();
/// let edges: SquareCSR2D<_> = DiEdgesBuilder::default()
/// .expected_number_of_edges(edges.len())
/// .expected_shape(nodes.len())
/// .edges(edges.into_iter())
/// .build()
/// .unwrap();
/// let graph: DiGraph<usize> = DiGraph::from((nodes, edges));
///
/// assert_eq!(graph.edges().number_of_self_loops(), 1);
/// # }
/// ```
fn number_of_self_loops(&self) -> Self::NodeId;
}
impl<E> MonopartiteEdges for E
where
E: Edges<DestinationNodeId = <E as Edges>::SourceNodeId>,
E::Matrix: SparseSquareMatrix<Index = E::SourceNodeId>,
{
type MonopartiteMatrix = E::Matrix;
type NodeId = E::SourceNodeId;
#[inline]
fn number_of_self_loops(&self) -> Self::NodeId {
self.matrix().number_of_defined_diagonal_values()
}
}
/// Trait defining the properties of a monopartited graph.
pub trait MonopartiteGraph: super::Graph {
/// The dense identifier of the nodes in the graph.
type NodeId: PositiveInteger + AsPrimitive<usize> + TryFromUsize;
/// The symbol of the node.
type NodeSymbol: Symbol;
/// The vocabulary holding the symbols of the nodes.
type Nodes: BidirectionalVocabulary<SourceSymbol = Self::NodeId, DestinationSymbol = Self::NodeSymbol>;
/// Returns the nodes vocabulary.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "alloc")]
/// # {
/// use geometric_traits::{
/// impls::{SortedVec, SquareCSR2D},
/// prelude::*,
/// traits::{EdgesBuilder, VocabularyBuilder},
/// };
///
/// let nodes: Vec<usize> = vec![0, 1, 2, 3];
/// let edges: Vec<(usize, usize)> = vec![(0, 1), (0, 2), (1, 2)];
/// let nodes: SortedVec<usize> = GenericVocabularyBuilder::default()
/// .expected_number_of_symbols(nodes.len())
/// .symbols(nodes.into_iter().enumerate())
/// .build()
/// .unwrap();
/// let edges: SquareCSR2D<_> = DiEdgesBuilder::default()
/// .expected_number_of_edges(edges.len())
/// .expected_shape(nodes.len())
/// .edges(edges.into_iter())
/// .build()
/// .unwrap();
/// let graph: DiGraph<usize> = DiGraph::from((nodes, edges));
///
/// assert_eq!(graph.nodes_vocabulary().len(), 4);
/// # }
/// ```
fn nodes_vocabulary(&self) -> &Self::Nodes;
/// Returns the iterator over the node identifiers.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "alloc")]
/// # {
/// use geometric_traits::{
/// impls::{SortedVec, SquareCSR2D},
/// prelude::*,
/// traits::{EdgesBuilder, VocabularyBuilder},
/// };
///
/// let nodes: Vec<usize> = vec![0, 1, 2, 3];
/// let edges: Vec<(usize, usize)> = vec![(0, 1), (0, 2), (1, 2)];
/// let nodes: SortedVec<usize> = GenericVocabularyBuilder::default()
/// .expected_number_of_symbols(nodes.len())
/// .symbols(nodes.into_iter().enumerate())
/// .build()
/// .unwrap();
/// let edges: SquareCSR2D<_> = DiEdgesBuilder::default()
/// .expected_number_of_edges(edges.len())
/// .expected_shape(nodes.len())
/// .edges(edges.into_iter())
/// .build()
/// .unwrap();
/// let graph: DiGraph<usize> = DiGraph::from((nodes, edges));
///
/// let node_ids: Vec<usize> = graph.node_ids().collect();
/// assert_eq!(node_ids, vec![0, 1, 2, 3]);
/// # }
/// ```
#[inline]
fn node_ids(&self) -> <Self::Nodes as Vocabulary>::Sources<'_> {
self.nodes_vocabulary().sources()
}
/// Returns the iterator over the node symbols.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "alloc")]
/// # {
/// use geometric_traits::{
/// impls::{SortedVec, SquareCSR2D},
/// prelude::*,
/// traits::{EdgesBuilder, VocabularyBuilder},
/// };
///
/// let nodes: Vec<usize> = vec![0, 1, 2, 3];
/// let edges: Vec<(usize, usize)> = vec![(0, 1), (0, 2), (1, 2)];
/// let nodes: SortedVec<usize> = GenericVocabularyBuilder::default()
/// .expected_number_of_symbols(nodes.len())
/// .symbols(nodes.into_iter().enumerate())
/// .build()
/// .unwrap();
/// let edges: SquareCSR2D<_> = DiEdgesBuilder::default()
/// .expected_number_of_edges(edges.len())
/// .expected_shape(nodes.len())
/// .edges(edges.into_iter())
/// .build()
/// .unwrap();
/// let graph: DiGraph<usize> = DiGraph::from((nodes, edges));
///
/// let nodes: Vec<usize> = graph.nodes().collect();
/// assert_eq!(nodes, vec![0, 1, 2, 3]);
/// # }
/// ```
#[inline]
fn nodes(&self) -> <Self::Nodes as Vocabulary>::Destinations<'_> {
self.nodes_vocabulary().destinations()
}
/// Returns the number of nodes in the graph.
///
/// # Examples
///
/// ```
/// # #[cfg(feature = "alloc")]
/// # {
/// use geometric_traits::{
/// impls::{SortedVec, SquareCSR2D},
/// prelude::*,
/// traits::{EdgesBuilder, VocabularyBuilder},
/// };
///
/// let nodes: Vec<usize> = vec![0, 1, 2, 3];
/// let edges: Vec<(usize, usize)> = vec![(0, 1), (0, 2), (1, 2)];
/// let nodes: SortedVec<usize> = GenericVocabularyBuilder::default()
/// .expected_number_of_symbols(nodes.len())
/// .symbols(nodes.into_iter().enumerate())
/// .build()
/// .unwrap();
/// let edges: SquareCSR2D<_> = DiEdgesBuilder::default()
/// .expected_number_of_edges(edges.len())
/// .expected_shape(nodes.len())
/// .edges(edges.into_iter())
/// .build()
/// .unwrap();
/// let graph: DiGraph<usize> = DiGraph::from((nodes, edges));
///
/// assert_eq!(graph.number_of_nodes(), 4);
/// # }
/// ```
#[inline]
fn number_of_nodes(&self) -> Self::NodeId {
if let Ok(number_of_nodes) =
Self::NodeId::try_from(Vocabulary::len(self.nodes_vocabulary()))
{
number_of_nodes
} else {
panic!("The number of nodes exceeds the capacity of the node identifier.")
}
}
}