pub struct ConnectivityGraph { /* private fields */ }Expand description
An owned graph with an explicitly selected, immutable implementation.
All engines preserve the Graph operation contract, including growing
u64 IDs and retained vertices after cuts. This wrapper adds constant-time
enum dispatch; operation costs are those of Graph, ForestGraph, HdtGraph
or the internal cluster forest (EngineConfig::ClusterForest).
It reads no environment variables or files and performs no internal locking.
use knotrel_core::{ConnectivityGraph, EngineConfig};
let mut graph = ConnectivityGraph::new(EngineConfig::EulerTour);
graph.link(1, u64::MAX)?;
assert!(graph.connected(1, u64::MAX)?);Implementations§
Source§impl ConnectivityGraph
impl ConnectivityGraph
Sourcepub fn new(config: EngineConfig) -> Self
pub fn new(config: EngineConfig) -> Self
Creates an empty graph. Every typed configuration is supported.
Sourcepub fn with_limits(config: EngineConfig, limits: GraphLimits) -> Self
pub fn with_limits(config: EngineConfig, limits: GraphLimits) -> Self
Creates an empty graph with immutable cardinality ceilings.
Sourcepub fn try_from_edges(
engine: EngineConfig,
limits: GraphLimits,
nodes: &[NodeId],
edges: &[(NodeId, NodeId)],
) -> Result<Self, GraphError>
pub fn try_from_edges( engine: EngineConfig, limits: GraphLimits, nodes: &[NodeId], edges: &[(NodeId, NodeId)], ) -> Result<Self, GraphError>
Builds an owned graph by registering nodes, then inserting edges,
in the supplied order using Self::add_node and Self::link.
Duplicate nodes and duplicate/reversed edges are no-ops, including at
capacity. Edges create missing endpoints; nodes is not a closed universe.
Empty inputs, isolated vertices and disconnected components are valid.
The returned graph supports the same subsequent dynamic operations and
retains the selected engine and limits.
Construction is atomic with respect to publication: on error the local graph is dropped, not returned partially built. This is not an in-place batch or snapshot restore, and returns no per-item change flags.
Work and allocation behavior are those of the ordered incremental calls; the constructor adds O(nodes.len() + edges.len()) iteration overhead and O(1) auxiliary space. It does not reserve, sort or stage the input. No performance improvement is promised.
§Errors
Returns the first GraphError from the ordered calls: all explicit
nodes precede every edge; each edge checks self-loop, node limit, then
edge limit. Later invalid input is not inspected ahead of that error.
Limits bound cardinality, not memory. As with incremental construction,
allocation failure is not a recoverable GraphError; collection capacity
overflow may panic and memory exhaustion may abort.
use knotrel_core::{ConnectivityGraph, EngineConfig, GraphLimits};
let mut graph = ConnectivityGraph::try_from_edges(
EngineConfig::EulerTour,
GraphLimits::default(),
&[99, 99],
&[(1, 2), (2, 1), (2, 3)],
)?;
assert_eq!(graph.node_count(), 4);
assert_eq!(graph.edge_count(), 2);
assert!(graph.connected(1, 3)?);
assert!(!graph.connected(99, 1)?);
assert!(graph.cut(2, 3)?);
assert!(!graph.connected(1, 3)?);Sourcepub fn limits(&self) -> GraphLimits
pub fn limits(&self) -> GraphLimits
Returns the immutable limits configured for this instance, as passed at construction.
They are the enforced limits for every engine except
EngineConfig::ClusterForest, whose own capacity (2²⁸ vertices and
2²⁸ edges) also applies: when it is lower, the error carries the
capacity instead of the configured value, which this method still
returns.
Sourcepub fn engine(&self) -> EngineConfig
pub fn engine(&self) -> EngineConfig
Returns the engine that actually owns this graph’s state.
Sourcepub fn contains_node(&self, node: NodeId) -> bool
pub fn contains_node(&self, node: NodeId) -> bool
Reports whether node is registered, in expected O(1) time with every
engine.
Limit checks use this ID-map lookup instead of a reflexive
Self::connected query, which costs O(log V) tree-root walks on the
Euler-tour and HDT engines.
use knotrel_core::{ConnectivityGraph, EngineConfig};
let mut graph = ConnectivityGraph::new(EngineConfig::Hdt);
graph.add_node(u64::MAX)?;
assert!(graph.contains_node(u64::MAX));
assert!(!graph.contains_node(1));Sourcepub fn add_node(&mut self, node: NodeId) -> Result<bool, GraphError>
pub fn add_node(&mut self, node: NodeId) -> Result<bool, GraphError>
Adds a vertex; returns whether it was absent.
§Errors
Returns GraphError::NodeLimitExceeded without mutation at capacity.
An existing vertex always returns Ok(false).
Sourcepub fn link(
&mut self,
source: NodeId,
target: NodeId,
) -> Result<bool, GraphError>
pub fn link( &mut self, source: NodeId, target: NodeId, ) -> Result<bool, GraphError>
Adds an undirected edge, creating absent endpoints.
Returns whether the edge was absent. A self-loop returns
GraphError::SelfLoop without creating its endpoint.
Limit checks precede all mutation; node limits take precedence over edge
limits. Duplicate edges succeed unchanged even at capacity. Each check
uses expected O(1) ID-map lookups, never a graph traversal, tree-root
walk or speculative insert.
§Errors
Self-loops or cardinality violations leave the entire graph unchanged.
Sourcepub fn cut(
&mut self,
source: NodeId,
target: NodeId,
) -> Result<bool, GraphError>
pub fn cut( &mut self, source: NodeId, target: NodeId, ) -> Result<bool, GraphError>
Removes an edge while retaining vertices.
Returns false for an absent edge or endpoint. A self-loop returns
GraphError::SelfLoop.
Sourcepub fn connected(
&self,
source: NodeId,
target: NodeId,
) -> Result<bool, GraphError>
pub fn connected( &self, source: NodeId, target: NodeId, ) -> Result<bool, GraphError>
Returns exact connectivity in the current graph.
Returns GraphError::UnknownNode for an absent endpoint, checking
the source first. A known vertex is connected to itself.
Sourcepub fn enable_neighbor_listing(&mut self) -> bool
pub fn enable_neighbor_listing(&mut self) -> bool
Enables neighbor listing on the Graph engine
(Graph::enable_neighbor_listing); does nothing for the experimental
engines, which cannot list neighbors. Returns whether listing is
available.
Sourcepub fn as_compact(&self) -> Option<&Graph>
pub fn as_compact(&self) -> Option<&Graph>
Returns the Graph engine when this graph uses
EngineConfig::CompactBfs, for adjacency inspection with
Graph::degree and Graph::neighbors_after. The experimental engines
keep no per-node adjacency list, so they return None.
use knotrel_core::{ConnectivityGraph, EngineConfig};
let mut graph = ConnectivityGraph::new(EngineConfig::CompactBfs);
graph.link(1, 2)?;
assert_eq!(graph.as_compact().and_then(|g| g.degree(1)), Some(1));
assert!(graph.enable_neighbor_listing());
assert!(!ConnectivityGraph::new(EngineConfig::Hdt).enable_neighbor_listing());
assert!(ConnectivityGraph::new(EngineConfig::EulerTour).as_compact().is_none());Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Returns the number of registered vertices.
Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
Returns the number of undirected edges.
Sourcepub fn for_each_node(&self, visit: impl FnMut(NodeId))
pub fn for_each_node(&self, visit: impl FnMut(NodeId))
Calls visit once for every registered vertex, isolated ones included,
in unspecified order.
Experimental (new in 1.3): a bulk read for snapshots and exports,
available on every engine. O(V) time; allocates nothing.
use knotrel_core::{ConnectivityGraph, EngineConfig};
let mut graph = ConnectivityGraph::new(EngineConfig::Hdt);
graph.link(1, 2)?;
graph.add_node(7)?;
let mut nodes = Vec::new();
graph.for_each_node(|node| nodes.push(node));
nodes.sort_unstable();
assert_eq!(nodes, [1, 2, 7]);Sourcepub fn for_each_edge(&self, visit: impl FnMut(NodeId, NodeId))
pub fn for_each_edge(&self, visit: impl FnMut(NodeId, NodeId))
Calls visit(low, high) once for every undirected edge, with
low < high, in unspecified order.
Experimental (new in 1.3): a bulk read for snapshots and exports,
available on every engine. O(V + E) time. It may allocate one
temporary vector of the vertex IDs (at most 8 bytes per vertex, or
per internal forest node on cluster-forest), freed before it returns.
use knotrel_core::{ConnectivityGraph, EngineConfig};
let mut graph = ConnectivityGraph::new(EngineConfig::EulerTour);
graph.link(9, 4)?;
graph.link(4, 5)?;
let mut edges = Vec::new();
graph.for_each_edge(|low, high| edges.push((low, high)));
edges.sort_unstable();
assert_eq!(edges, [(4, 5), (4, 9)]);