Skip to main content

ConnectivityGraph

Struct ConnectivityGraph 

Source
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

Source

pub fn new(config: EngineConfig) -> Self

Creates an empty graph. Every typed configuration is supported.

Source

pub fn with_limits(config: EngineConfig, limits: GraphLimits) -> Self

Creates an empty graph with immutable cardinality ceilings.

Source

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)?);
Source

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.

Source

pub fn engine(&self) -> EngineConfig

Returns the engine that actually owns this graph’s state.

Source

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));
Source

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).

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.

Source

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.

Source

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.

Source

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.

Source

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());
Source

pub fn node_count(&self) -> usize

Returns the number of registered vertices.

Source

pub fn edge_count(&self) -> usize

Returns the number of undirected edges.

Source

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]);
Source

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)]);

Trait Implementations§

Source§

impl Debug for ConnectivityGraph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ConnectivityGraph

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<Graph> for ConnectivityGraph

Source§

fn from(graph: Graph) -> Self

Takes ownership without rebuilding or losing existing graph state.

Auto Trait Implementations§

§

impl Forget for ConnectivityGraph

§

impl Freeze for ConnectivityGraph

§

impl Move for ConnectivityGraph

§

impl RefUnwindSafe for ConnectivityGraph

§

impl Send for ConnectivityGraph
where RawTable<(u64, usize)>: Send, RawVecInner: Send, NodeRef<Owned, usize, SetValZST, LeafOrInternal>: Send, BoxRaw<(usize, SetValZST)>: Send, RawTable<((usize, usize), Option<(usize, usize)>)>: Send, NonZero<usize>: Send, RawTable<((usize, usize), Edge)>: Send, RawTable<(usize, usize)>: Send, BoxRaw<ClusterForestGraph>: Send,

§

impl Sync for ConnectivityGraph
where RawTable<(u64, usize)>: Sync, RawVecInner: Sync, NodeRef<Owned, usize, SetValZST, LeafOrInternal>: Sync, BoxRaw<(usize, SetValZST)>: Sync, RawTable<((usize, usize), Option<(usize, usize)>)>: Sync, NonZero<usize>: Sync, RawTable<((usize, usize), Edge)>: Sync, RawTable<(usize, usize)>: Sync, BoxRaw<ClusterForestGraph>: Sync,

§

impl Unpin for ConnectivityGraph
where *const u8 is !null: Unpin, *const LeafNode<usize, SetValZST> is !null: Unpin, Box<(usize, SetValZST)>: Unpin, NonZero<usize>: Unpin, (usize, usize): Unpin, Box<ClusterForestGraph>: Unpin,

§

impl UnsafeUnpin for ConnectivityGraph

§

impl UnwindSafe for ConnectivityGraph

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.