| Crates.io | graphix_algs |
| lib.rs | graphix_algs |
| version | 0.1.0 |
| created_at | 2025-05-04 14:33:00.73144+00 |
| updated_at | 2025-05-04 14:33:00.73144+00 |
| description | Algorithm extensions for graphix: connected-components, contraction, and MST routines |
| homepage | https://github.com/PlotoZypresse/graphix-algs |
| repository | https://github.com/PlotoZypresse/graphix-algs |
| max_upload_size | |
| id | 1659679 |
| size | 7,906 |
A minimal, dependency-free crate to compute connected components via iterative DFS on any CSR-style graph.
GraphAccess trait for plugging in your own graph typestd – works in no_std contextsAdd this to your Cargo.toml:
[dependencies]
graph-dfs = "0.1"
use graph_dfs::{GraphAccess, compute_cc_by_dfs};
/// A simple CSR-style graph:
struct MyGraph {
/// CSR offsets: `v[u]..v[u+1]` are the half-edges from `u`
v: Vec<usize>,
/// half-edges: `(neighbor, weight, orig_eid)`
e: Vec<(usize, usize, usize)>,
}
impl GraphAccess for MyGraph {
type Weight = usize;
fn num_vertices(&self) -> usize {
self.v.len() - 1
}
fn edges_from(&self, u: usize) -> &[(usize, Self::Weight, usize)] {
let start = self.v[u];
let end = self.v[u + 1];
&self.e[start..end]
}
}
fn main() {
// build a tiny triangle graph: 0–1, 1–2, 2–0
let v = vec![0, 2, 4, 6];
let e = vec![
(1, 1, 0), (2, 1, 2), // from 0
(0, 1, 0), (2, 1, 1), // from 1
(1, 1, 1), (0, 1, 2), // from 2
];
let graph = MyGraph { v, e };
// compute connected components
let cc_ids = compute_cc_by_dfs(&graph);
assert_eq!(cc_ids, vec![0, 0, 0]); // all in one component
}
See the full API docs on docs.rs.
See the LICENSE file for details.