knotrel-core
Exact, in-memory dynamic connectivity for undirected simple graphs.
knotrel-core is a specialized, synchronous Rust library that maintains exact reachability
queries ("is vertex u connected to vertex v via some path?") as edges are dynamically
inserted (link) and deleted (cut) over time.
Quick Start
Add the latest published release to your project:
Use ConnectivityGraph to create and query a dynamic graph:
use ;
Graph Model
- Undirected simple graphs: No directed edges, self-loops, or multi-edges.
- Node identifiers: Vertices are identified by
NodeId(u64). - Dynamic registration:
add_nodeadds isolated vertices;linkimplicitly creates endpoints. - Cuts retain vertices:
cutremoves an edge while preserving its endpoint nodes. - Atomic validation: Invalid operations (e.g. self-loops, capacity violations) leave state completely unchanged.
Bulk Initialization
Use ConnectivityGraph::try_from_edges to initialize a graph from existing slices of nodes and edges:
use ;
let nodes = ; // isolated standby node
let edges = ;
let mut graph = try_from_edges?;
assert!;
assert!;
# Ok::
Node and Edge Lifecycle
LifecycleGraph (compact-bfs, and cluster-forest from 1.1;
ett and hdt are rejected with LifecycleError::UnsupportedEngine) keeps the registered topology with a
node-active flag per node and an edge-enabled flag per edge, and answers
connected over the effective graph: an edge counts only when it is
enabled and both endpoints are active. Deactivating a node keeps its edges and
their flags; remove_node deletes a node and all its edges, and re-adding the
ID creates a fresh isolated node.
use ;
let mut graph = new?;
graph.link?;
graph.link?;
graph.set_node_active?;
assert!;
graph.set_node_active?;
assert!;
# Ok::
It is available on compact-bfs and, from 1.1, on the
experimental cluster-forest engine; lifecycle support for ett and hdt
is deferred. It is not free: deactivating or removing a
node costs work proportional to its degree, and the graph keeps a topology
catalog besides the engine's effective graph. ConnectivityGraph is
unchanged. See the
lifecycle contract
for exact semantics, errors and costs.
Listing Neighbors (since 0.8)
Neighbors can be listed page by page with a cursor, for browsing tools:
Graph::degree and Graph::neighbors_after on compact-bfs (after the
one-time opt-in ConnectivityGraph::enable_neighbor_listing, which keeps an
8-byte-per-node index), and LifecycleGraph::incident_after, which also
reports each edge's state. A cursor is the last neighbor returned; pages are
not a snapshot of a graph that changes between them.
Listing Every Node and Edge (experimental, since 1.3)
ConnectivityGraph::for_each_node and for_each_edge visit every node and
every undirected edge (low < high) once, in unspecified order, on every
engine; LifecycleGraph::for_each_node and for_each_edge also report each
node's active and each edge's enabled flag. They are bulk reads for
snapshots and exports, O(V + E), and are classified Experimental.
Engine Selection
Knotrel provides four dynamic connectivity engines via EngineConfig:
- Compact BFS (
EngineConfig::CompactBfs) — Default: Sorted adjacency vectors of stable internal indices;O(V + E)worst-case queries (since 1.2, a bidirectional search that stops when the two sides meet);O(deg(u) + deg(v))amortized link and cut;O(V + E)logical memory. Best for general use or mutation-heavy workloads. - Euler Tour Trees (
EngineConfig::EulerTour) — Experimental engine, stable interface: Spanning forest using AVL Euler tour sequences.O(log V)algorithmic queries and link operations. Non-tree cuts costO(log V + log E); tree cuts scan the smaller side (O(log V + log E + s + c log V)amortized) using candidate-pruned subtrees. Effective for query-dominated workloads on tree-like topologies. - HDT (
EngineConfig::Hdt) — Experimental engine, stable interface: Deterministic Holm–de Lichtenberg–Thorup dynamic connectivity with an append-only growth policy for expanding vertices. Maintains a hierarchy ofO(log V)levels materialized lazily as promotions require them. Bounds amortized update costs toO(log² V), eliminating repeated scan storms in dense bridge-churn workloads. - Cluster forest (
EngineConfig::ClusterForest) — Experimental engine (since 1.1), stable interface: Sequential cluster forest (Wulff-Nilsen 2013, with the path compression of De Man et al. 2025).O(log N)queries by walking to the roots,O(V + E)logical memory;O(log² N)amortized updates by this project's derivation for this variant (not a published theorem). Also supported byLifecycleGraph. Its 1.1 engine comparison is on the performance site.
Complexity & Memory Notes
- Algorithmic vs. API costs: Algorithmic complexity bounds apply to internal index structures. The public API maps external
NodeId(u64) keys via hash maps (FxHashMap), which rely on expectedO(1)lookups and do not offer deterministic worst-case constant bounds for arbitrary ID distributions. - Logical memory vs. heap/RSS: Stated memory bounds (
O(V + E)andO(E + V log V)) describe logical graph entity storage. Allocated vector capacities are retained after cuts and do not bound process heap memory or RSS. - Amortized bounds: HDT update bounds are amortized sequence averages, not per-request latency guarantees; large promotion searches or vector growths can produce latency spikes, and it may have higher constant factors on small or sparse graphs; verify against your actual workload.
Recommendation
Start with Compact BFS (EngineConfig::CompactBfs); evaluate ETT (EngineConfig::EulerTour) for query-dominated workloads or HDT (EngineConfig::Hdt) for workloads with repeated bridge replacement searches, empirically measuring actual topology, scale, and mutation mix.
Stability
Every public item is classified stable, experimental or internal
in the stability classification.
Stable items change only as the
compatibility policy
allows, after a deprecation. The ETT engine (EngineConfig::EulerTour,
ForestGraph, ForestStats) and the HDT engine (EngineConfig::Hdt,
HdtGraph, HdtStats, HdtStorageStats, HdtLevelStorage) are stable
interfaces with an experimental engine: their public API stays compatible
across 1.x, while their performance, maturity and usage recommendation carry
no promise (EngineConfig::is_experimental returns true).
LifecycleStorage is stable. For these diagnostic structs, the fields, units
and the properties their documentation lists as guaranteed are stable; the
exact values are implementation-dependent. ReferenceGraph is an internal
test oracle.
Examples
Run the included Cargo examples:
# Basic dynamic connectivity
# Bulk initialization and error handling
# Engine selection and equivalence checks
# Node activation, edge enabling and node removal
License
Licensed under the Elastic License 2.0 (ELv2).