____ ____._____ .______ .______ ._______ .___.__
\ \_/ /:_ ___\ : __ \ : \ : ____ |: | \
\___ ___/ | |___| \____|| . || : || : |
/ _ \ | / || : \ | : || |___|| . |
/___/ \___\|. __ || |___\|___| ||___| |___| |
:/ |. ||___| |___| |___|
: :/
:
XGraph: Advanced Graph Algorithms in Rust
XGraph is a Rust library providing high-performance graph algorithms for real-world problems such as social network analysis, transportation network optimization, and recommendation systems.
It offers both a generic homogeneous Graph and a HeterogeneousGraph multigraph in
which nodes and edges can carry multiple distinct types β letting you model intricate
systems featuring:
- Diverse node types (e.g. users, items, or locations)
- Multiple edge types between the same pair of nodes (e.g. friendships, transactions, or routes)
π Why XGraph?
- High performance: efficient algorithms backed by slab-based storage and an edge index for fast lookups.
- Flexible data model: custom node/edge data, weighted edges, and arbitrary string attributes, for both homogeneous and heterogeneous multigraphs.
- Practical algorithms: connectivity, shortest paths, centrality, community detection, bridges, dominating sets, and cycle detection.
- Easy integration: a small API with CSV import/export.
πΌ Applications
- Social network analysis: detect key influencers and communities.
- Logistics & routing: find shortest and most reliable paths.
- Telecommunication networks: identify critical nodes and links.
- Recommendation systems: analyze userβitem interaction graphs.
π¦ Installation
Add XGraph to your Cargo.toml:
[]
= "2.1"
Both the graph and hgraph features are enabled by default. To pull in only the
homogeneous graph:
[]
= { = "2.1", = false, = ["graph"] }
Features π
Flexible Graph Structure
- Directed and undirected graphs
- Custom node/edge data types and weighted edges
- Arbitrary string attributes on nodes and edges
- Heterogeneous multigraphs: multiple node/edge types and parallel edges between the same pair of nodes
Heterogeneous Multigraph Support
HeterogeneousGraph (in the hgraph module) extends the core graph with:
- Heterogeneous nodes: nodes can hold different data types within the same graph, representing distinct entity types (e.g. users and items in a recommendation system).
- Multiple edge types: several edges between the same pair of nodes are allowed, each with its own data type and attributes (e.g. "friendship" and "colleague" relationships).
- Static typing: node and edge types are defined through the
NodeTypeandEdgeTypetraits, so all core functionality is preserved while adding multigraph capabilities.
This is ideal for modeling multi-modal transportation networks (roads, rails, flights) or social networks with varied interaction types.
Core Algorithms
- Bridge detection: identifies critical edges whose removal would disconnect the graph; heterogeneous support via
hgraph::h_bridges::HeteroBridges. - Centrality measures: degree, betweenness, and closeness centrality to evaluate node importance.
- Connectivity analysis: strongly and weakly connected components (Kosaraju), plus connectivity checks.
- Leiden community detection: deterministic and non-deterministic variants, including clustering restricted to selected edge types.
- Shortest paths (Dijkstra): single-source distances, extensible to multigraphs with type-specific weights.
- Dominating set finding: greedy minimal dominating sets.
- Cycle detection: for directed and undirected graphs.
Advanced Operations
- Adjacency matrix conversion
- Graph validation
- Batch node/edge operations
- Attribute management
- CSV import/export
Quick Start π
Homogeneous graph
use Graph;
use ShortestPath;
let mut graph = new;
let a = graph.add_node;
let b = graph.add_node;
let c = graph.add_node;
graph.add_edge.unwrap;
graph.add_edge.unwrap;
let distances = graph.dijkstra.unwrap;
assert_eq!;
Heterogeneous multigraph
use HeterogeneousGraph;
use NodeType;
use EdgeType;
;
;
let mut graph = new;
let oslo = graph.add_node;
let bergen = graph.add_node;
// Parallel edges of different types between the same pair of nodes.
graph.add_edge.unwrap;
graph.add_edge.unwrap;
assert_eq!;
Runnable examples
The repository ships two complete, runnable examples (both features are on by default):
Cargo Features
graph(default): the homogeneousGraphtype and its algorithms.hgraph(default): theHeterogeneousGraphmultigraph type and its algorithms.
Testing & Validation β
Test coverage includes:
- Graph manipulation invariants (including non-contiguous node IDs after removals)
- Algorithm correctness checks
- Edge case handling
- CSV round-trip serialization
Run the suite with:
Documentation π
Full API documentation is available on docs.rs.
License π
MIT License β see LICENSE for details.