graphina 0.3.0-alpha.4

A graph data science library for Rust
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*!
# Advanced Graph Builders

Builder patterns for complex graph construction.
These builders support fluent APIs, validation, and preset configurations for common graph types.
*/

use crate::core::error::{GraphinaError, Result};
use crate::core::types::{BaseGraph, Directed, GraphConstructor, NodeId, Undirected};
use petgraph::EdgeType;
use std::marker::PhantomData;

/// Ergonomic aliases for common builder types.
pub type DirectedGraphBuilder<A, W> = AdvancedGraphBuilder<A, W, Directed>;
/// A builder for creating undirected graphs with optional node capacity.
pub type UndirectedGraphBuilder<A, W> = AdvancedGraphBuilder<A, W, Undirected>;

/// Advanced builder for graphs with validation and configuration options.
///
/// # Example
///
/// ```rust
/// use graphina::core::builders::AdvancedGraphBuilder;
/// use graphina::core::types::Directed;
///
/// let graph = AdvancedGraphBuilder::<i32, f64, Directed>::directed()
///     .with_capacity(100, 200)
///     .allow_self_loops(false)
///     .allow_parallel_edges(false)
///     .build();
/// ```
pub struct AdvancedGraphBuilder<A, W, Ty: GraphConstructor<A, W> + EdgeType> {
    capacity_nodes: usize,
    capacity_edges: usize,
    allow_self_loops: bool,
    allow_parallel_edges: bool,
    nodes: Vec<A>,
    edges: Vec<(usize, usize, W)>,
    _marker: PhantomData<Ty>,
}

impl<A, W> AdvancedGraphBuilder<A, W, Directed> {
    /// Creates a new builder for a directed graph.
    pub fn directed() -> Self {
        Self::new()
    }
}

impl<A, W> AdvancedGraphBuilder<A, W, Undirected> {
    /// Creates a new builder for an undirected graph.
    pub fn undirected() -> Self {
        Self::new()
    }
}

impl<A, W, Ty: GraphConstructor<A, W> + EdgeType> Default for AdvancedGraphBuilder<A, W, Ty> {
    fn default() -> Self {
        Self::new()
    }
}

impl<A, W, Ty: GraphConstructor<A, W> + EdgeType> AdvancedGraphBuilder<A, W, Ty> {
    /// Creates a new advanced graph builder.
    pub fn new() -> Self {
        Self {
            capacity_nodes: 0,
            capacity_edges: 0,
            allow_self_loops: true,
            allow_parallel_edges: true,
            nodes: Vec::new(),
            edges: Vec::new(),
            _marker: PhantomData,
        }
    }

    /// Sets the pre-allocated capacity for nodes and edges.
    pub fn with_capacity(mut self, nodes: usize, edges: usize) -> Self {
        self.capacity_nodes = nodes;
        self.capacity_edges = edges;
        self
    }

    /// Sets whether self-loops (edges from a node to itself) are allowed.
    pub fn allow_self_loops(mut self, allow: bool) -> Self {
        self.allow_self_loops = allow;
        self
    }

    /// Sets whether parallel edges (multiple edges between the same pair of nodes) are allowed.
    pub fn allow_parallel_edges(mut self, allow: bool) -> Self {
        self.allow_parallel_edges = allow;
        self
    }

    /// Adds a node to the builder.
    pub fn add_node(mut self, attr: A) -> Self {
        self.nodes.push(attr);
        self
    }

    /// Adds multiple nodes from an iterator.
    pub fn add_nodes<I>(mut self, iter: I) -> Self
    where
        I: IntoIterator<Item = A>,
    {
        self.nodes.extend(iter);
        self
    }

    /// Adds an edge to the builder.
    pub fn add_edge(mut self, source: usize, target: usize, weight: W) -> Self {
        self.edges.push((source, target, weight));
        self
    }

    /// Adds multiple edges from an iterator.
    pub fn add_edges<I>(mut self, iter: I) -> Self
    where
        I: IntoIterator<Item = (usize, usize, W)>,
    {
        self.edges.extend(iter);
        self
    }

    /// Validates the configuration and constructs the graph.
    pub fn build(self) -> Result<BaseGraph<A, W, Ty>> {
        self.validate()?;

        let mut graph = BaseGraph::with_capacity(
            self.capacity_nodes.max(self.nodes.len()),
            self.capacity_edges.max(self.edges.len()),
        );

        // Add nodes
        let node_ids: Vec<NodeId> = self
            .nodes
            .into_iter()
            .map(|attr| graph.add_node(attr))
            .collect();

        // Add edges with validation
        for (source, target, weight) in self.edges {
            if source >= node_ids.len() || target >= node_ids.len() {
                return Err(GraphinaError::invalid_argument(format!(
                    "Edge references invalid node index: ({}, {})",
                    source, target
                )));
            }

            if !self.allow_self_loops && source == target {
                return Err(GraphinaError::invalid_argument(
                    "Self-loops are not allowed in this graph configuration",
                ));
            }

            if !self.allow_parallel_edges && graph.contains_edge(node_ids[source], node_ids[target])
            {
                return Err(GraphinaError::invalid_argument(format!(
                    "Parallel edges are not allowed: edge ({}, {}) already exists",
                    source, target
                )));
            }

            graph.add_edge(node_ids[source], node_ids[target], weight);
        }

        Ok(graph)
    }

    /// Validates the builder configuration without constructing the graph.
    pub fn validate(&self) -> Result<()> {
        // Check for node index validity in edges
        for (source, target, _) in &self.edges {
            if *source >= self.nodes.len() {
                let max_info = if self.nodes.is_empty() {
                    "none".to_string()
                } else {
                    (self.nodes.len() - 1).to_string()
                };
                return Err(GraphinaError::invalid_argument(format!(
                    "Edge source index {} is out of bounds (max: {})",
                    source, max_info
                )));
            }
            if *target >= self.nodes.len() {
                let max_info = if self.nodes.is_empty() {
                    "none".to_string()
                } else {
                    (self.nodes.len() - 1).to_string()
                };
                return Err(GraphinaError::invalid_argument(format!(
                    "Edge target index {} is out of bounds (max: {})",
                    target, max_info
                )));
            }
        }

        Ok(())
    }
}

/// Builder for common graph topologies.
pub struct TopologyBuilder;

impl TopologyBuilder {
    /// Creates a complete graph with n nodes.
    pub fn complete<A, W>(n: usize, node_attr: A, edge_weight: W) -> BaseGraph<A, W, Undirected>
    where
        A: Clone,
        W: Clone,
    {
        let mut builder = AdvancedGraphBuilder::undirected().with_capacity(n, n * (n - 1) / 2);

        // Add nodes
        for _ in 0..n {
            builder = builder.add_node(node_attr.clone());
        }

        // Add all possible edges
        for i in 0..n {
            for j in (i + 1)..n {
                builder = builder.add_edge(i, j, edge_weight.clone());
            }
        }

        builder.build().unwrap_or_else(|_| BaseGraph::new())
    }

    /// Creates a cycle graph with n nodes.
    pub fn cycle<A, W>(n: usize, node_attr: A, edge_weight: W) -> BaseGraph<A, W, Undirected>
    where
        A: Clone,
        W: Clone,
    {
        let mut builder = AdvancedGraphBuilder::undirected().with_capacity(n, n);

        // Add nodes
        for _ in 0..n {
            builder = builder.add_node(node_attr.clone());
        }

        // Add cycle edges
        for i in 0..n {
            let next = (i + 1) % n;
            builder = builder.add_edge(i, next, edge_weight.clone());
        }

        builder.build().unwrap_or_else(|_| BaseGraph::new())
    }

    /// Creates a path graph with n nodes.
    pub fn path<A, W>(n: usize, node_attr: A, edge_weight: W) -> BaseGraph<A, W, Undirected>
    where
        A: Clone,
        W: Clone,
    {
        if n == 0 {
            return AdvancedGraphBuilder::undirected()
                .build()
                .unwrap_or_else(|_| BaseGraph::new());
        }

        let mut builder = AdvancedGraphBuilder::undirected().with_capacity(n, n.saturating_sub(1));

        // Add nodes
        for _ in 0..n {
            builder = builder.add_node(node_attr.clone());
        }

        // Add path edges
        for i in 0..(n - 1) {
            builder = builder.add_edge(i, i + 1, edge_weight.clone());
        }

        builder.build().unwrap_or_else(|_| BaseGraph::new())
    }

    /// Creates a star graph with n nodes (1 central node + (n-1) peripheral nodes).
    pub fn star<A, W>(n: usize, node_attr: A, edge_weight: W) -> BaseGraph<A, W, Undirected>
    where
        A: Clone,
        W: Clone,
    {
        if n == 0 {
            return AdvancedGraphBuilder::undirected()
                .build()
                .unwrap_or_else(|_| BaseGraph::new());
        }

        let mut builder = AdvancedGraphBuilder::undirected().with_capacity(n, n - 1);

        // Add nodes
        for _ in 0..n {
            builder = builder.add_node(node_attr.clone());
        }

        // Add edges from center (node 0) to all others
        for i in 1..n {
            builder = builder.add_edge(0, i, edge_weight.clone());
        }

        builder.build().unwrap_or_else(|_| BaseGraph::new())
    }

    /// Creates a grid graph with dimensions rows x cols.
    pub fn grid<A, W>(
        rows: usize,
        cols: usize,
        node_attr: A,
        edge_weight: W,
    ) -> BaseGraph<A, W, Undirected>
    where
        A: Clone,
        W: Clone,
    {
        let n = rows * cols;
        let mut builder = AdvancedGraphBuilder::undirected().with_capacity(n, 2 * n - rows - cols);

        // Add nodes
        for _ in 0..n {
            builder = builder.add_node(node_attr.clone());
        }

        // Add grid edges
        for i in 0..rows {
            for j in 0..cols {
                let current = i * cols + j;

                // Connect to right neighbor
                if j < cols - 1 {
                    builder = builder.add_edge(current, current + 1, edge_weight.clone());
                }

                // Connect to bottom neighbor
                if i < rows - 1 {
                    builder = builder.add_edge(current, current + cols, edge_weight.clone());
                }
            }
        }

        builder.build().unwrap_or_else(|_| BaseGraph::new())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_advanced_builder_basic() {
        let graph = AdvancedGraphBuilder::directed()
            .add_node(1)
            .add_node(2)
            .add_edge(0, 1, 1.0)
            .build()
            .unwrap();

        assert_eq!(graph.node_count(), 2);
        assert_eq!(graph.edge_count(), 1);
    }

    #[test]
    fn test_builder_with_capacity() {
        let graph: BaseGraph<i32, f64, Undirected> = AdvancedGraphBuilder::undirected()
            .with_capacity(100, 200)
            .build()
            .unwrap();

        assert_eq!(graph.node_count(), 0);
        assert_eq!(graph.edge_count(), 0);
    }

    #[test]
    fn test_self_loop_validation() {
        let result = AdvancedGraphBuilder::directed()
            .allow_self_loops(false)
            .add_node(1)
            .add_edge(0, 0, 1.0)
            .build();

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Self-loops"));
    }

    #[test]
    fn test_parallel_edge_validation() {
        let result = AdvancedGraphBuilder::undirected()
            .allow_parallel_edges(false)
            .add_node(1)
            .add_node(2)
            .add_edge(0, 1, 1.0)
            .add_edge(0, 1, 2.0)
            .build();

        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("Parallel edges"));
    }

    #[test]
    fn test_invalid_node_index() {
        let result = AdvancedGraphBuilder::directed()
            .add_node(1)
            .add_edge(0, 5, 1.0)
            .build();

        assert!(result.is_err());
    }

    #[test]
    fn test_validate_no_nodes_with_edge() {
        // Make sure that validate() does not panic when edges reference missing nodes and node list is empty.
        let result = AdvancedGraphBuilder::<i32, f64, Directed>::directed()
            .add_edge(0, 0, 1.0)
            .build();
        assert!(result.is_err());
        let err = result.err().unwrap().to_string();
        assert!(err.contains("out of bounds"));
    }

    #[test]
    fn test_topology_complete_graph() {
        let graph = TopologyBuilder::complete(5, (), 1.0);
        assert_eq!(graph.node_count(), 5);
        assert_eq!(graph.edge_count(), 10); // 5 * 4 / 2
    }

    #[test]
    fn test_topology_cycle_graph() {
        let graph = TopologyBuilder::cycle(6, (), 1.0);
        assert_eq!(graph.node_count(), 6);
        assert_eq!(graph.edge_count(), 6);
    }

    #[test]
    fn test_topology_path_graph() {
        let graph = TopologyBuilder::path(5, (), 1.0);
        assert_eq!(graph.node_count(), 5);
        assert_eq!(graph.edge_count(), 4);
    }

    #[test]
    fn test_topology_path_graph_zero_nodes() {
        let graph = TopologyBuilder::path::<(), f64>(0, (), 1.0);
        assert_eq!(graph.node_count(), 0);
        assert_eq!(graph.edge_count(), 0);
    }

    #[test]
    fn test_topology_star_graph() {
        let graph = TopologyBuilder::star(6, (), 1.0);
        assert_eq!(graph.node_count(), 6);
        assert_eq!(graph.edge_count(), 5);
    }

    #[test]
    fn test_topology_grid_graph() {
        let graph = TopologyBuilder::grid(3, 4, (), 1.0);
        assert_eq!(graph.node_count(), 12);
        // (3-1)*4 + 3*(4-1) = 8 + 9 = 17
        assert_eq!(graph.edge_count(), 17);
    }

    #[test]
    fn test_add_multiple_nodes() {
        let graph: BaseGraph<i32, f64, Directed> = AdvancedGraphBuilder::directed()
            .add_nodes(vec![1, 2, 3, 4, 5])
            .build()
            .unwrap();

        assert_eq!(graph.node_count(), 5);
    }

    #[test]
    fn test_add_multiple_edges() {
        let graph = AdvancedGraphBuilder::directed()
            .add_nodes(vec![1, 2, 3])
            .add_edges(vec![(0, 1, 1.0), (1, 2, 2.0)])
            .build()
            .unwrap();

        assert_eq!(graph.edge_count(), 2);
    }
}