4 releases (2 breaking)
new 0.3.0 | Apr 28, 2025 |
---|---|
0.2.1 | Apr 27, 2025 |
0.2.0 | Apr 27, 2025 |
0.1.0 | Apr 25, 2025 |
#415 in Data structures
78 downloads per month
Used in graphix_io
10KB
128 lines
graphix
A lightweight Rust library providing a compact and efficient representation of undirected graphs using a compressed adjacency list format. Optimized for sparse graphs and supports weighted edges with customizable types.
Features
- GraphRep: a compressed adjacency list representation for undirected graphs.
- Efficient memory layout using flat vectors.
- Supports edge weights of any
Copy + Ord
type. - Fast adjacency list access.
- Unit tested and ready for algorithmic use cases.
Installation
Add graphix
to your Cargo.toml
dependencies:
[dependencies]
graphix = "0.1"
Or with cargo
:
cargo add graphix
Quick Start
use graphix::GraphRep;
fn main() {
// Create a graph with 3 vertices and space for 2 edges
let mut g: GraphRep<i32> = GraphRep::new(3, 2);
// Add edges (undirected, so both directions are added)
g.add_edge(0, 1, 10);
g.add_edge(0, 2, 20);
// Finalize internal layout for fast adjacency access
g.finish_v();
// Iterate over neighbors of vertex 0
for (neighbor, weight) in g.edges_from(0) {
println!("0 -> {} (weight {})", neighbor, weight);
}
}
API
GraphRep<K>
-
GraphRep::new(n: usize, m: usize) -> Self
Create a graph withn
vertices and space form
undirected edges. -
add_edge(&mut self, u: usize, v: usize, weight: K)
Add an undirected edge betweenu
andv
with a given weight. Internally stores each undirected edge as two directed entries. -
finish_v(&mut self)
Finalize the internal adjacency list structure. Must be called before usingedges_from
. -
edges_from(&self, vertex: usize) -> Vec<(usize, K)>
Get a list of neighbor–weight pairs for a given vertex. -
num_vertices(&self) -> usize
Return the number of vertices. -
num_edges(&self) -> usize
Return the number of undirected edges.
License
This project is licensed under the MIT License. See the LICENSE file for details.