pub struct ForestGraph { /* private fields */ }Expand description
Exact connectivity with deterministic Euler-tour trees.
Stable interface, experimental engine. This type, its methods and its trait implementations are stable for 1.x: they are removed or changed incompatibly only after a deprecation and in a major release. The experimental qualification covers performance, maturity and usage recommendation only: the cost bounds below describe the current implementation and are not a compatibility promise (compatibility policy).
Maintains a spanning forest: each graph component has exactly one tree.
Non-tree edges are stored at both endpoints. Removing a forest edge scans
non-tree edges incident to its smaller side; a crossing edge reconnects the
trees, or exhaustion proves a real split. This is not HDT: repeated scans have
no polylogarithmic amortized bound. The default crate::Graph is unchanged.
AVL root queries cost O(log V) algorithmically, with expected O(1) ID hash map translation. Link costs O(log V + log E) amortized including arena growth. A tree cut costs (amortized for free-list growth) O(log V + log E + min(s, (k+1) log V) + c log V), where s is the smaller side size, k the yielded candidate-bearing vertices, and c the examined non-tree incidences. Empty candidate sets require no enumeration. Non-tree cuts cost O(log V + log E). A single token-arena or free-list growth can add O(V) time. Logical storage is O(V + E); allocated arenas retain O(V) high-water capacity, removed tree-edge slots are reused and an emptied non-tree incidence set frees its storage. Queries allocate no scratch, cache no answers and use no internal locks.
Implementations§
Source§impl ForestGraph
impl ForestGraph
Sourcepub fn add_node(&mut self, node: NodeId) -> bool
pub fn add_node(&mut self, node: NodeId) -> bool
Registers an isolated vertex; returns false for an existing ID.
O(1) amortized; arena/vector growth can take O(V) in one call.
Sourcepub fn link(
&mut self,
source: NodeId,
target: NodeId,
) -> Result<bool, GraphError>
pub fn link( &mut self, source: NodeId, target: NodeId, ) -> Result<bool, GraphError>
Inserts a simple undirected edge, registering absent endpoints.
Returns false for an existing edge, including reversed endpoints.
§Errors
Rejects self-loops before registering either endpoint.
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 both vertices; absent edges return false.
Tree cuts synchronously complete replacement search before returning.
§Errors
Rejects self-loops without changing graph state.
Sourcepub fn connected(
&self,
source: NodeId,
target: NodeId,
) -> Result<bool, GraphError>
pub fn connected( &self, source: NodeId, target: NodeId, ) -> Result<bool, GraphError>
Answers exact connectivity in O(log V) algorithmic tree root time, plus expected O(1) hash map lookup for node IDs, without allocation.
Existing vertices connect to themselves, including isolated vertices.
§Errors
Reports an absent source before an absent target.
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Returns the number of registered vertices in O(1) time.
Sourcepub fn edge_count(&self) -> usize
pub fn edge_count(&self) -> usize
Returns the number of undirected graph edges in O(1) time.
Sourcepub fn stats(&self) -> ForestStats
pub fn stats(&self) -> ForestStats
Returns the replacement-search counters accumulated since creation or
the last Self::reset_stats, in O(1) time without changing any state.
ForestStats lists which properties of the values are guaranteed.
Sourcepub fn reset_stats(&mut self)
pub fn reset_stats(&mut self)
Sets every diagnostic counter to zero, preserving all graph state.