pub struct DynamicConnectivity { /* private fields */ }Expand description
Dynamic connectivity data structure with Euler Tour Tree backend
Maintains connected components of an undirected graph with support for edge insertions and deletions. Uses Euler Tour Trees for O(log n) operations with union-find fallback for robustness.
§Examples
let mut dc = DynamicConnectivity::new();
dc.add_vertex(0);
dc.add_vertex(1);
dc.add_vertex(2);
dc.insert_edge(0, 1);
assert!(dc.connected(0, 1));
assert!(!dc.connected(0, 2));
dc.insert_edge(1, 2);
assert!(dc.is_connected()); // All vertices connected
dc.delete_edge(1, 2);
assert!(!dc.connected(0, 2));Implementations§
Source§impl DynamicConnectivity
impl DynamicConnectivity
Sourcepub fn add_vertex(&mut self, v: VertexId)
pub fn add_vertex(&mut self, v: VertexId)
Sourcepub fn insert_edge(&mut self, u: VertexId, v: VertexId)
pub fn insert_edge(&mut self, u: VertexId, v: VertexId)
Inserts an edge between two vertices
Automatically adds vertices if they don’t exist. If vertices are already connected, updates internal state but doesn’t change connectivity.
§Arguments
u- First vertexv- Second vertex
§Time Complexity
O(log n) via Euler Tour Tree link operation
§Examples
let mut dc = DynamicConnectivity::new();
dc.insert_edge(0, 1);
assert!(dc.connected(0, 1));Sourcepub fn delete_edge(&mut self, u: VertexId, v: VertexId)
pub fn delete_edge(&mut self, u: VertexId, v: VertexId)
Deletes an edge between two vertices
Triggers a full rebuild of the data structure from the remaining edges. The ETT is also rebuilt to maintain O(log n) queries.
§Arguments
u- First vertexv- Second vertex
§Time Complexity
O(m·α(n)) where m is the number of edges (includes ETT rebuild)
§Examples
let mut dc = DynamicConnectivity::new();
dc.insert_edge(0, 1);
dc.delete_edge(0, 1);
assert!(!dc.connected(0, 1));Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Checks if the entire graph is connected (single component)
§Returns
true if all vertices are in a single connected component,
false otherwise
§Time Complexity
O(1)
§Examples
let mut dc = DynamicConnectivity::new();
dc.add_vertex(0);
dc.add_vertex(1);
assert!(!dc.is_connected());
dc.insert_edge(0, 1);
assert!(dc.is_connected());Sourcepub fn connected(&mut self, u: VertexId, v: VertexId) -> bool
pub fn connected(&mut self, u: VertexId, v: VertexId) -> bool
Checks if two vertices are in the same connected component
§Arguments
u- First vertexv- Second vertex
§Returns
true if vertices are connected, false otherwise.
Returns false if either vertex doesn’t exist.
§Time Complexity
O(α(n)) amortized via union-find with path compression
§Examples
let mut dc = DynamicConnectivity::new();
dc.insert_edge(0, 1);
dc.insert_edge(1, 2);
assert!(dc.connected(0, 2));Sourcepub fn connected_fast(&self, u: VertexId, v: VertexId) -> Option<bool>
pub fn connected_fast(&self, u: VertexId, v: VertexId) -> Option<bool>
Fast connectivity check using Euler Tour Tree (O(log n))
Returns None if ETT is out of sync and result is unreliable.
Use connected() for the reliable version.
Sourcepub fn component_count(&self) -> usize
pub fn component_count(&self) -> usize
Sourcepub fn vertex_count(&self) -> usize
pub fn vertex_count(&self) -> usize
Trait Implementations§
Source§impl Clone for DynamicConnectivity
impl Clone for DynamicConnectivity
Source§fn clone(&self) -> DynamicConnectivity
fn clone(&self) -> DynamicConnectivity
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for DynamicConnectivity
impl Debug for DynamicConnectivity
Auto Trait Implementations§
impl Freeze for DynamicConnectivity
impl RefUnwindSafe for DynamicConnectivity
impl Send for DynamicConnectivity
impl Sync for DynamicConnectivity
impl Unpin for DynamicConnectivity
impl UnwindSafe for DynamicConnectivity
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more