Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite connected with findroot #51

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 28 additions & 28 deletions src/lctree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,60 +161,58 @@ impl<P: Path> LinkCutTree<P> {
/// assert!(lctree.connected(alice, bob)); // now connected
/// ```
pub fn connected(&mut self, v: usize, w: usize) -> bool {
self.reroot(v); // v is now the root of the tree
self.access(w);
// if access(w) messed with the root of the tree, then v and w are connected:
self.forest.parent_of(v).is_some() || v == w
v == w || self.findroot(v) == self.findroot(w)
}

/// Checks if two nodes are connected by a link
/// (i.e. v is the parent of w or vice versa).
/// Merges two trees into a single tree.
///
/// # Examples
/// ```
/// use lctree::LinkCutTree;
///
/// let mut lctree = LinkCutTree::default();
/// let alice = lctree.make_tree(0.0);
/// let bob = lctree.make_tree(0.0);
/// let clay = lctree.make_tree(0.0);
/// let bob = lctree.make_tree(1.0);
/// let clay = lctree.make_tree(2.0);
///
/// lctree.link(alice, bob);
/// lctree.link(bob, clay);
///
/// assert!(lctree.linked(alice, bob)); // alice and bob are connected by a link
/// assert!(!lctree.linked(alice, clay)); // alice and clay are not connected by a link
/// assert!(lctree.connected(alice, clay));
/// ```
pub fn linked(&mut self, v: usize, w: usize) -> bool {
if self.connected(v, w) {
self.forest.left_of(w) == Some(v) && self.forest.right_of(v).is_none()
} else {
false
pub fn link(&mut self, v: usize, w: usize) -> bool {
self.reroot(v);
self.access(w);
// if access(w) messed with the root of the tree, then v and w are connected:
if self.forest.parent_of(v).is_some() || v == w {
return false;
}
// v is the root of its represented tree:
self.forest.set_left(v, w);
true
}

/// Merges two trees into a single tree.
/// Checks if two nodes are connected by a link
/// (i.e. v is the parent of w or vice versa).
///
/// # Examples
/// ```
/// use lctree::LinkCutTree;
///
/// let mut lctree = LinkCutTree::default();
/// let alice = lctree.make_tree(0.0);
/// let bob = lctree.make_tree(1.0);
/// let clay = lctree.make_tree(2.0);
/// let bob = lctree.make_tree(0.0);
/// let clay = lctree.make_tree(0.0);
///
/// lctree.link(alice, bob);
/// lctree.link(bob, clay);
/// assert!(lctree.connected(alice, clay));
///
/// assert!(lctree.linked(alice, bob)); // alice and bob are connected by a link
/// assert!(!lctree.linked(alice, clay)); // alice and clay are not connected by a link
/// ```
pub fn link(&mut self, v: usize, w: usize) -> bool {
if self.connected(v, w) {
return false;
}
// v is the root of its represented tree:
self.forest.set_left(v, w);
true
pub fn linked(&mut self, v: usize, w: usize) -> bool {
self.reroot(v);
self.access(w);
self.forest.left_of(w) == Some(v) && self.forest.right_of(v).is_none()
}

/// Cuts the link between two nodes (if it exists)
Expand Down Expand Up @@ -265,7 +263,9 @@ impl<P: Path> LinkCutTree<P> {
/// assert_eq!(richest_guy.weight, 10.0);
/// ```
pub fn path(&mut self, v: usize, w: usize) -> P {
if !self.connected(v, w) {
self.reroot(v);
self.access(w);
if self.forest.parent_of(v).is_none() && v != w {
return P::default(f64::INFINITY, usize::MAX);
}
self.forest.aggregated_path_of(w)
Expand Down