|
3 | 3 | * Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved. |
4 | 4 | */ |
5 | 5 |
|
6 | | -use crate::{GraphError, GraphView}; |
| 6 | +pub use crate::traits::graph_algo_centrality::*; |
| 7 | +pub use crate::traits::graph_algo_pathfinder::*; |
| 8 | +pub use crate::traits::graph_algo_structural::*; |
| 9 | +pub use crate::traits::graph_algo_topological::*; |
7 | 10 |
|
8 | | -/// Defines a suite of high-performance, read-only analytical algorithms. |
| 11 | +/// A comprehensive suite of graph algorithms. |
9 | 12 | /// |
10 | | -/// This trait is intended for implementation on static, optimized graph structures |
11 | | -/// like `next_graph::CsmGraph` to validate their structure and properties. |
12 | | -pub trait GraphAlgorithms<N, W>: GraphView<N, W> { |
13 | | - // --- Structural Validation Algorithms --- |
14 | | - |
15 | | - /// Finds a single cycle in the graph and returns the path of nodes that form it. |
16 | | - /// |
17 | | - /// This is the most powerful cycle detection method, as it not only confirms the |
18 | | - /// presence of a cycle but also identifies the specific nodes involved. This is |
19 | | - /// invaluable for debugging dynamically generated graphs. |
20 | | - /// |
21 | | - /// # Returns |
22 | | - /// `Some(Vec<usize>)` containing the sequence of node indices that form a cycle |
23 | | - /// (e.g., `[0, 1, 0]`). Returns `None` if the graph is a DAG. |
24 | | - fn find_cycle(&self) -> Result<Option<Vec<usize>>, GraphError>; |
25 | | - |
26 | | - /// Checks if the graph contains any directed cycles. |
27 | | - /// |
28 | | - /// This method should be implemented as a simple call to `self.find_cycle().is_some()`. |
29 | | - fn has_cycle(&self) -> Result<bool, GraphError>; |
30 | | - |
31 | | - /// Computes a topological sort of the graph, if it is a Directed Acyclic Graph (DAG). |
32 | | - /// Returns `None` if the graph contains a cycle. |
33 | | - fn topological_sort(&self) -> Result<Option<Vec<usize>>, GraphError>; |
34 | | - |
35 | | - // --- Pathfinding and Reachability Algorithms --- |
36 | | - |
37 | | - /// Checks if a path of any length exists from a start to a stop index. |
38 | | - fn is_reachable(&self, start_index: usize, stop_index: usize) -> Result<bool, GraphError>; |
39 | | - |
40 | | - /// Returns the length of the shortest path (in number of nodes) from a start to a stop index. |
41 | | - fn shortest_path_len( |
42 | | - &self, |
43 | | - start_index: usize, |
44 | | - stop_index: usize, |
45 | | - ) -> Result<Option<usize>, GraphError>; |
46 | | - |
47 | | - /// Finds the complete shortest path from a start to a stop index. |
48 | | - fn shortest_path( |
49 | | - &self, |
50 | | - start_index: usize, |
51 | | - stop_index: usize, |
52 | | - ) -> Result<Option<Vec<usize>>, GraphError>; |
53 | | - |
54 | | - /// Finds the shortest path in a weighted graph using Dijkstra's algorithm. |
55 | | - /// |
56 | | - /// The edge weight type `W` must support addition, comparison, and have a zero value. |
57 | | - /// |
58 | | - /// # Returns |
59 | | - /// A tuple containing the sequence of node indices in the path and the total cost of that path. |
60 | | - /// Returns `None` if no path exists. |
61 | | - fn shortest_weighted_path( |
62 | | - &self, |
63 | | - start_index: usize, |
64 | | - stop_index: usize, |
65 | | - ) -> Result<Option<(Vec<usize>, W)>, GraphError> |
66 | | - where |
67 | | - W: Copy + Ord + Default + std::ops::Add<Output = W>; |
68 | | - |
69 | | - /// Finds all Strongly Connected Components in the graph using Tarjan's algorithm. |
70 | | - /// |
71 | | - /// # Returns |
72 | | - /// A vector of vectors, where each inner vector is a list of node indices |
73 | | - /// belonging to a single SCC. |
74 | | - fn strongly_connected_components(&self) -> Result<Vec<Vec<usize>>, GraphError>; |
| 13 | +/// This trait aggregates several focused algorithm traits into a single, convenient |
| 14 | +/// supertrait. A type that implements `GraphAlgorithms` has access to all methods |
| 15 | +/// from the component traits. |
| 16 | +pub trait GraphAlgorithms<N, W>: |
| 17 | + TopologicalGraphAlgorithms<N, W> |
| 18 | + + PathfindingGraphAlgorithms<N, W> |
| 19 | + + StructuralGraphAlgorithms<N, W> |
| 20 | + + CentralityGraphAlgorithms<N, W> |
| 21 | +{ |
75 | 22 | } |
0 commit comments