routingkit-cch
Rust/Python bindings for the Customizable Contraction Hierarchies (CCH) implementation from RoutingKit. CCH is a three‑phase shortest path acceleration technique for large directed graphs (e.g. road networks) that allows fast re-weighting while keeping very low query latency.
Why CCH?
CustomizableContractionHierarchies (CCH) are an index-based speedup technique for shortest paths in directed graphs that can quickly be adapted to new weights. CCHs use, contrary to regulars CHs, a three phase setup:
- Preprocessing
- Customization
- Query
The preprocessing is slow but does not rely on the arc weights. The Customization introduces the weights and is reasonably fast. Finally, the actual paths are computed in the query phase. A common setup consists of doing the preprocessing once and the customization per user upon login. Further one can use a customization to incorporate live-traffic updates.
Features
- Safe ergonomic Rust API on top of proven C++ core via
cxx. - Build indices from raw edge lists (tail/head arrays).
- Ordering helpers: nested dissection (inertial separator heuristic) and degree fallback.
- Sequential & parallel customization, and partial weight update afterwards.
- Cheap full re-customization via
CCHMetric::reset(reuses internal buffers). - Reusable query object supporting multi-source / multi-target searches.
- Batched one-to-many / many-to-one queries with pinned targets/sources (
CCHOneToMany,CCHManyToOne). - Path extraction: node sequence & original arc id sequence.
- Thread-safe sharing of immutable structures (
CCH,CCHMetric).
Installation
Rust stable release from crates.io:
[]
= "0.1"
Or track the repository:
[]
= { = "https://github.com/HellOwhatAs/routingkit-cch" }
Python stable release from pypi:
Or track the repository:
For the git form ensure the RoutingKit submodule is present:
Requirements: C++17 compiler (MSVC / gcc / clang).
OpenMP is enabled automatically by the build script; ensure your toolchain provides an OpenMP runtime (e.g. install libomp on macOS).
Without it the build may fail when CCHMetric::parallel_new is called.
Quick Start
For python examples, see
examplesfolder.
use ;
// Small toy graph: 0 -> 1 -> 2 -> 3
let tail = vec!;
let head = vec!;
let weights = vec!; // total 22 from 0 to 3
let node_count = 4u32;
// 1) Compute a (cheap) order; for real data prefer compute_order_inertial (requires lat,lon).
let order = compute_order_degree;
let cch = CCHnew;
// 2) Bind weights & customize (done inside CCHMetric::new here).
let metric = new;
// 3) Run a shortest path query 0 -> 3.
let mut q = new;
q.add_source;
q.add_target;
let res = q.run;
assert_eq!;
let node_path = res.node_path;
assert_eq!;
let arc_path = res.arc_path;
assert_eq!;
Building an Order
For production use prefer the inertial nested dissection based order:
use compute_order_inertial;
let order = compute_order_inertial;
Better separators -> faster customization & queries. External advanced orderers (e.g. FlowCutter) could be integrated offline; you only need to supply the permutation.
(Parallel) Customization
use ;
let metric = new; // single thread
let metric = parallel_new; // 0 -> auto threads
Use when graphs are large enough; for tiny graphs overhead may outweigh benefit.
Full Re-Customization (Metric Reset)
When all (or most) weights change (e.g. periodic traffic refresh), rebind the existing metric instead of building a new one; internal shortcut-weight buffers are reused:
let mut metric = new;
// ... later, new weights for the same CCH ...
metric.reset; // re-customizes in place
// or, with the `openmp` feature:
metric.parallel_reset; // 0 -> auto threads
Requires exclusive access: drop all queries borrowing the metric first (enforced by the borrow checker).
Incremental (Partial) Weight Updates
If only a small subset of arc weights change (e.g. traffic incidents), you can avoid a full re-customization:
let mut metric = parallel_new;
let mut updater = new;
// ... run queries ...
// Update two arcs (id 12 -> 900, id 77 -> 450)
updater.apply;
// New queries now see updated weights.
Query
let mut q = new;
q.add_source;
q.add_target;
// drop res before reusing q since res takes &mut q
q.add_source;
q.add_target;
CCHQuery is not thread-safe; create one instance per thread and reuse it is far cheaper than constructing a new one.
One-to-Many / Many-to-One (Batched) Queries
When you need distances from one node to many fixed destinations (distance tables, matrices, k-nearest), pin the destination set once and query it in a single sweep — much faster than a point-to-point query per destination:
use ;
let mut otm = new; // pin once (moderately expensive)
for s in sources
// Mirror image: distances from many fixed sources to a target.
let mut mto = new;
let dist = mto.distances_to; // aligned with `sources`
Multi-source/-target variants with initial distance offsets are available
(distances_from_multi, distances_to_multi). Note: batched queries return distances only
(no path reconstruction), matching the underlying RoutingKit API.
Path Reconstruction
After run() -> CCHQueryResult:
CCHQueryResult::distance()->Option<u32>(None = unreachable)CCHQueryResult::node_path()->Vec<node_id>(empty = unreachable)CCHQueryResult::arc_path()->Vec<original_arc_id>(empty = unreachable)
Thread Safety
| Type | Send | Sync | Notes |
|---|---|---|---|
CCH |
yes | yes | Immutable after build |
CCHMetric |
yes | yes | Read-only after customization / partial-update |
CCHQuery |
yes | no | Internal mutable labels; reuse it within thread |
CCHQueryResult |
yes | no | Runned state of CCHQuery, actually &mut of it |
CCHOneToMany |
yes | no | Internal mutable labels; reuse it within thread |
CCHManyToOne |
yes | no | Internal mutable labels; reuse it within thread |
CCHMetricPartialUpdater |
no | no | Should have nothing to do with parallel |
Create separate queries per thread for parallel batch querying.