xgraph 2.0.0

A comprehensive Rust library providing efficient graph algorithms for solving real-world problems in social network analysis, transportation optimization, recommendation systems, and more
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! Shortest path algorithms for heterogeneous multigraphs.
//!
//! This module provides implementations of Dijkstra's algorithm for computing shortest paths
//! in heterogeneous multigraphs, supporting both directed and undirected graphs with multiple
//! edge types. Key functionalities include:
//! - Computing shortest path distances and predecessors from a single source.
//! - Finding the shortest path between two nodes.
//! - Edge type filtering for type-specific shortest path analysis.
//!
//! The module is available only when the `hgraph` feature is enabled in your `Cargo.toml`:
//! ```toml
//! [dependencies.xgraph]
//! version = "x.y.z"
//! features = ["hgraph"]
//! ```

use crate::h_search::HeteroSearch;
#[cfg(feature = "hgraph")]
use crate::hgraph::h_graph::HeterogeneousGraph;
#[cfg(feature = "hgraph")]
use std::cmp::Ordering;
#[cfg(feature = "hgraph")]
use std::collections::{BinaryHeap, HashMap};
#[cfg(feature = "hgraph")]
use std::fmt::Debug;
#[cfg(feature = "hgraph")]
use std::hash::Hash;
#[cfg(feature = "hgraph")]
use std::ops::Add;

// Error handling additions

/// Error type for shortest path computation failures.
///
/// Represents errors that may occur during shortest path calculations, such as invalid node references.
#[cfg(feature = "hgraph")]
#[derive(Debug)]
pub enum ShortestPathError {
    /// Indicates a node referenced in the graph does not exist.
    InvalidNodeReference(usize),
}

#[cfg(feature = "hgraph")]
impl std::fmt::Display for ShortestPathError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ShortestPathError::InvalidNodeReference(id) => {
                write!(f, "Invalid node reference: node ID {} not found", id)
            }
        }
    }
}

#[cfg(feature = "hgraph")]
impl std::error::Error for ShortestPathError {}

/// Result type alias for shortest path operations.
///
/// Wraps the result of shortest path methods to enable error handling without panicking.
#[cfg(feature = "hgraph")]
pub type Result<T> = std::result::Result<T, ShortestPathError>;

#[cfg(feature = "hgraph")]
/// Trait for shortest path operations in heterogeneous multigraphs.
///
/// Provides methods for computing shortest path distances and reconstructing paths using
/// Dijkstra's algorithm, with support for edge type filtering. Methods that may fail due to
/// invalid inputs return a `Result` to handle errors gracefully.
pub trait HeteroShortestPath<W, N, E>
where
    W: Add<Output = W> + PartialOrd + Copy + Default + From<u8> + Debug + PartialEq,
    N: Clone + Eq + Hash + Debug + crate::hgraph::h_node::NodeType,
    E: Clone + Default + Debug + crate::hgraph::h_edge::EdgeType,
{
    /// Computes shortest path distances and predecessors from a start node using Dijkstra's algorithm.
    ///
    /// Uses all edges regardless of type to find the shortest paths from the start node to all reachable nodes.
    ///
    /// # Arguments
    /// * `start` - The starting node ID.
    ///
    /// # Returns
    /// A `Result` containing a tuple of:
    /// - `HashMap<usize, W>`: Shortest distances from `start` to each reachable node.
    /// - `HashMap<usize, usize>`: Predecessor nodes for path reconstruction.
    ///
    /// # Errors
    /// Returns `ShortestPathError::InvalidNodeReference` if `start` does not exist in the graph.
    fn dijkstra(&self, start: usize) -> Result<(HashMap<usize, W>, HashMap<usize, usize>)>;

    /// Computes shortest path distances and predecessors from a start node, considering only specific edge types.
    ///
    /// Filters edges by type to compute shortest paths in a subgraph defined by `edge_types`.
    ///
    /// # Arguments
    /// * `start` - The starting node ID.
    /// * `edge_types` - A slice of edge type strings to filter the computation.
    ///
    /// # Returns
    /// A `Result` containing a tuple of:
    /// - `HashMap<usize, W>`: Shortest distances from `start` to each reachable node.
    /// - `HashMap<usize, usize>`: Predecessor nodes for path reconstruction.
    ///
    /// # Errors
    /// Returns `ShortestPathError::InvalidNodeReference` if `start` does not exist in the graph.
    fn dijkstra_by_types(
        &self,
        start: usize,
        edge_types: &[&str],
    ) -> Result<(HashMap<usize, W>, HashMap<usize, usize>)>;

    /// Finds the shortest path between two nodes using Dijkstra's algorithm.
    ///
    /// Returns the path as a vector of node IDs from `start` to `end`, using all edges.
    ///
    /// # Arguments
    /// * `start` - The starting node ID.
    /// * `end` - The target node ID.
    ///
    /// # Returns
    /// A `Result` containing `Some(Vec<usize>)` with the shortest path if found, `None` if no path exists,
    /// or an error if either node is invalid.
    fn dijkstra_path(&self, start: usize, end: usize) -> Result<Option<Vec<usize>>>;

    /// Finds the shortest path between two nodes using Dijkstra's algorithm, considering only specific edge types.
    ///
    /// Returns the path as a vector of node IDs from `start` to `end`, filtered by `edge_types`.
    ///
    /// # Arguments
    /// * `start` - The starting node ID.
    /// * `end` - The target node ID.
    /// * `edge_types` - A slice of edge type strings to filter the computation.
    ///
    /// # Returns
    /// A `Result` containing `Some(Vec<usize>)` with the shortest path if found, `None` if no path exists,
    /// or an error if either node is invalid.
    fn dijkstra_path_by_types(
        &self,
        start: usize,
        end: usize,
        edge_types: &[&str],
    ) -> Result<Option<Vec<usize>>>;
}

#[cfg(feature = "hgraph")]
#[derive(Copy, Clone, PartialEq)]
/// Represents a state in Dijkstra's algorithm with a cost and node ID.
///
/// Used in the priority queue to prioritize nodes with lower costs.
struct State<W> {
    cost: W,
    node: usize,
}

#[cfg(feature = "hgraph")]
impl<W: PartialOrd> PartialOrd for State<W> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

#[cfg(feature = "hgraph")]
impl<W: PartialEq> Eq for State<W> {}

#[cfg(feature = "hgraph")]
impl<W: PartialOrd> Ord for State<W> {
    fn cmp(&self, other: &Self) -> Ordering {
        // Reverse order for min-heap behavior (lower cost has higher priority)
        other
            .cost
            .partial_cmp(&self.cost)
            .unwrap_or(Ordering::Equal)
    }
}

#[cfg(feature = "hgraph")]
impl<W, N, E> HeteroShortestPath<W, N, E> for HeterogeneousGraph<W, N, E>
where
    W: Add<Output = W> + PartialOrd + Copy + Default + From<u8> + Debug + PartialEq,
    N: Clone + Eq + Hash + Debug + crate::hgraph::h_node::NodeType,
    E: Clone + Default + Debug + crate::hgraph::h_edge::EdgeType,
{
    fn dijkstra(&self, start: usize) -> Result<(HashMap<usize, W>, HashMap<usize, usize>)> {
        if !self.has_node(start) {
            return Err(ShortestPathError::InvalidNodeReference(start));
        }

        let mut distances = HashMap::new();
        let mut previous = HashMap::new();
        let mut heap = BinaryHeap::new();

        distances.insert(start, W::default());
        heap.push(State {
            cost: W::default(),
            node: start,
        });

        while let Some(State { cost, node }) = heap.pop() {
            if let Some(current_cost) = distances.get(&node) {
                if cost.partial_cmp(current_cost) == Some(Ordering::Greater) {
                    continue;
                }
            } else {
                continue;
            }

            for &(neighbor, ref edges) in &self.nodes[node].neighbors {
                let min_weight = edges
                    .iter()
                    .map(|&(_, weight)| weight)
                    .filter(|w| w.partial_cmp(&W::default()).is_some())
                    .min_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));

                if let Some(min_weight) = min_weight {
                    let next_cost = cost + min_weight;
                    if self.has_node(neighbor) {
                        match distances.entry(neighbor) {
                            std::collections::hash_map::Entry::Occupied(mut entry) => {
                                if next_cost.partial_cmp(entry.get()) == Some(Ordering::Less) {
                                    entry.insert(next_cost);
                                    previous.insert(neighbor, node);
                                    heap.push(State {
                                        cost: next_cost,
                                        node: neighbor,
                                    });
                                }
                            }
                            std::collections::hash_map::Entry::Vacant(entry) => {
                                entry.insert(next_cost);
                                previous.insert(neighbor, node);
                                heap.push(State {
                                    cost: next_cost,
                                    node: neighbor,
                                });
                            }
                        }
                    }
                }
            }
        }

        Ok((distances, previous))
    }

    fn dijkstra_by_types(
        &self,
        start: usize,
        edge_types: &[&str],
    ) -> Result<(HashMap<usize, W>, HashMap<usize, usize>)> {
        if !self.has_node(start) {
            return Err(ShortestPathError::InvalidNodeReference(start));
        }

        let mut distances = HashMap::new();
        let mut previous = HashMap::new();
        let mut heap = BinaryHeap::new();

        distances.insert(start, W::default());
        heap.push(State {
            cost: W::default(),
            node: start,
        });

        while let Some(State { cost, node }) = heap.pop() {
            if let Some(current_cost) = distances.get(&node) {
                if cost.partial_cmp(current_cost) == Some(Ordering::Greater) {
                    continue;
                }
            } else {
                continue;
            }

            for &(neighbor, ref edges) in &self.nodes[node].neighbors {
                let min_weight = edges
                    .iter()
                    .filter(|&&(edge_id, _)| {
                        let edge = self.edges.get(edge_id).expect("Edge should exist");
                        edge_types.contains(&edge.data.as_string().as_str())
                    })
                    .map(|&(_, weight)| weight)
                    .filter(|w| w.partial_cmp(&W::default()).is_some())
                    .min_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal));

                if let Some(min_weight) = min_weight {
                    let next_cost = cost + min_weight;
                    if self.has_node(neighbor) {
                        match distances.entry(neighbor) {
                            std::collections::hash_map::Entry::Occupied(mut entry) => {
                                if next_cost.partial_cmp(entry.get()) == Some(Ordering::Less) {
                                    entry.insert(next_cost);
                                    previous.insert(neighbor, node);
                                    heap.push(State {
                                        cost: next_cost,
                                        node: neighbor,
                                    });
                                }
                            }
                            std::collections::hash_map::Entry::Vacant(entry) => {
                                entry.insert(next_cost);
                                previous.insert(neighbor, node);
                                heap.push(State {
                                    cost: next_cost,
                                    node: neighbor,
                                });
                            }
                        }
                    }
                }
            }
        }

        Ok((distances, previous))
    }

    fn dijkstra_path(&self, start: usize, end: usize) -> Result<Option<Vec<usize>>> {
        if !self.has_node(start) {
            return Err(ShortestPathError::InvalidNodeReference(start));
        }
        if !self.has_node(end) {
            return Err(ShortestPathError::InvalidNodeReference(end));
        }

        let (_, previous) = self.dijkstra(start)?;

        if !previous.contains_key(&end) && start != end {
            return Ok(None);
        }

        let mut path = Vec::new();
        let mut current = end;

        while current != start {
            path.push(current);
            if let Some(&prev) = previous.get(&current) {
                current = prev;
            } else {
                break;
            }
        }
        path.push(start);
        path.reverse();

        Ok(Some(path))
    }

    fn dijkstra_path_by_types(
        &self,
        start: usize,
        end: usize,
        edge_types: &[&str],
    ) -> Result<Option<Vec<usize>>> {
        if !self.has_node(start) {
            return Err(ShortestPathError::InvalidNodeReference(start));
        }
        if !self.has_node(end) {
            return Err(ShortestPathError::InvalidNodeReference(end));
        }

        let (_, previous) = self.dijkstra_by_types(start, edge_types)?;

        if !previous.contains_key(&end) && start != end {
            return Ok(None);
        }

        let mut path = Vec::new();
        let mut current = end;

        while current != start {
            path.push(current);
            if let Some(&prev) = previous.get(&current) {
                current = prev;
            } else {
                break;
            }
        }
        path.push(start);
        path.reverse();

        Ok(Some(path))
    }
}

#[cfg(test)]
#[cfg(feature = "hgraph")]
mod tests {
    use super::*;
    use crate::hgraph::h_edge::EdgeType;
    use crate::hgraph::h_node::NodeType;

    #[derive(Clone, Eq, PartialEq, Hash, Debug)]
    struct TestNode(String);
    impl NodeType for TestNode {
        fn as_string(&self) -> String {
            self.0.clone()
        }
    }

    #[derive(Clone, Debug, Default)]
    struct TestEdge(String);
    impl EdgeType for TestEdge {
        fn as_string(&self) -> String {
            self.0.clone()
        }
    }

    #[test]
    fn test_dijkstra() {
        let mut graph = HeterogeneousGraph::<u32, TestNode, TestEdge>::new(true);
        let n0 = graph.add_node(TestNode("A".to_string()));
        let n1 = graph.add_node(TestNode("B".to_string()));
        let n2 = graph.add_node(TestNode("C".to_string()));

        graph
            .add_edge(n0, n1, 4, TestEdge("road".to_string()))
            .unwrap();
        graph
            .add_edge(n1, n2, 3, TestEdge("bridge".to_string()))
            .unwrap();
        graph
            .add_edge(n0, n2, 8, TestEdge("path".to_string()))
            .unwrap();

        let (distances, _) = graph.dijkstra(n0).unwrap();
        assert_eq!(distances[&n0], 0);
        assert_eq!(distances[&n1], 4);
        assert_eq!(distances[&n2], 7); // 0 -> 1 -> 2
    }

    #[test]
    fn test_dijkstra_by_types() {
        let mut graph = HeterogeneousGraph::<u32, TestNode, TestEdge>::new(true);
        let n0 = graph.add_node(TestNode("A".to_string()));
        let n1 = graph.add_node(TestNode("B".to_string()));
        let n2 = graph.add_node(TestNode("C".to_string()));

        graph
            .add_edge(n0, n1, 4, TestEdge("road".to_string()))
            .unwrap();
        graph
            .add_edge(n1, n2, 3, TestEdge("bridge".to_string()))
            .unwrap();
        graph
            .add_edge(n0, n2, 8, TestEdge("path".to_string()))
            .unwrap();

        let (distances, _) = graph.dijkstra_by_types(n0, &["road", "bridge"]).unwrap();
        assert_eq!(distances[&n0], 0);
        assert_eq!(distances[&n1], 4);
        assert_eq!(distances[&n2], 7); // 0 -> 1 -> 2

        let (distances, _) = graph.dijkstra_by_types(n0, &["road"]).unwrap();
        assert_eq!(distances[&n0], 0);
        assert_eq!(distances[&n1], 4);
        assert_eq!(distances.get(&n2), None); // n2 unreachable with only "road"
    }

    #[test]
    fn test_dijkstra_path() {
        let mut graph = HeterogeneousGraph::<u32, TestNode, TestEdge>::new(true);
        let n0 = graph.add_node(TestNode("A".to_string()));
        let n1 = graph.add_node(TestNode("B".to_string()));
        let n2 = graph.add_node(TestNode("C".to_string()));

        graph
            .add_edge(n0, n1, 4, TestEdge("road".to_string()))
            .unwrap();
        graph
            .add_edge(n1, n2, 3, TestEdge("bridge".to_string()))
            .unwrap();
        graph
            .add_edge(n0, n2, 8, TestEdge("path".to_string()))
            .unwrap();

        let path = graph.dijkstra_path(n0, n2).unwrap();
        assert_eq!(path, Some(vec![n0, n1, n2]));
    }

    #[test]
    fn test_dijkstra_path_by_types() {
        let mut graph = HeterogeneousGraph::<u32, TestNode, TestEdge>::new(true);
        let n0 = graph.add_node(TestNode("A".to_string()));
        let n1 = graph.add_node(TestNode("B".to_string()));
        let n2 = graph.add_node(TestNode("C".to_string()));

        graph
            .add_edge(n0, n1, 4, TestEdge("road".to_string()))
            .unwrap();
        graph
            .add_edge(n1, n2, 3, TestEdge("bridge".to_string()))
            .unwrap();
        graph
            .add_edge(n0, n2, 8, TestEdge("path".to_string()))
            .unwrap();

        let path = graph
            .dijkstra_path_by_types(n0, n2, &["road", "bridge"])
            .unwrap();
        assert_eq!(path, Some(vec![n0, n1, n2]));

        let path = graph.dijkstra_path_by_types(n0, n2, &["path"]).unwrap();
        assert_eq!(path, Some(vec![n0, n2]));

        let path = graph.dijkstra_path_by_types(n0, n2, &["road"]).unwrap();
        assert_eq!(path, None); // n2 unreachable with only "road"
    }

    #[test]
    fn test_unreachable_node() {
        let mut graph = HeterogeneousGraph::<u32, TestNode, TestEdge>::new(true);
        let n0 = graph.add_node(TestNode("A".to_string()));
        let n1 = graph.add_node(TestNode("B".to_string()));

        let (distances, _) = graph.dijkstra(n0).unwrap();
        assert_eq!(distances[&n0], 0);
        assert_eq!(distances.get(&n1), None);

        let path = graph.dijkstra_path(n0, n1).unwrap();
        assert_eq!(path, None);

        let path = graph.dijkstra_path_by_types(n0, n1, &["road"]).unwrap();
        assert_eq!(path, None);
    }

    #[test]
    fn test_same_start_end() {
        let mut graph = HeterogeneousGraph::<u32, TestNode, TestEdge>::new(true);
        let n0 = graph.add_node(TestNode("A".to_string()));
        let n1 = graph.add_node(TestNode("B".to_string()));
        graph
            .add_edge(n0, n1, 4, TestEdge("road".to_string()))
            .unwrap();

        let path = graph.dijkstra_path(n0, n0).unwrap();
        assert_eq!(path, Some(vec![n0]));

        let path = graph.dijkstra_path_by_types(n0, n0, &["road"]).unwrap();
        assert_eq!(path, Some(vec![n0]));
    }

    #[test]
    fn test_invalid_node() {
        let graph = HeterogeneousGraph::<u32, TestNode, TestEdge>::new(true);
        assert!(matches!(
            graph.dijkstra(0),
            Err(ShortestPathError::InvalidNodeReference(0))
        ));
        assert!(matches!(
            graph.dijkstra_path(0, 1),
            Err(ShortestPathError::InvalidNodeReference(0))
        ));
        assert!(matches!(
            graph.dijkstra_path_by_types(0, 1, &["road"]),
            Err(ShortestPathError::InvalidNodeReference(0))
        ));
    }
}