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

Benchmarks #32

Merged
merged 5 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ name = "lctree"
version = "0.1.2"
edition = "2021"
rust-version = "1.63"
description = "Rust implementation of Link-Cut-Tree: self-balancing data structure to maintain a forest of rooted trees."
readme = "README.md"
documentation = "https://docs.rs/lctree"
homepage = "https://github.com/azizkayumov/lctree"
repository = "https://github.com/azizkayumov/lctree"
license = "Apache-2.0"
keywords = ["tree", "dynamic-connectivity"]
categories = ["algorithms", "data-structures"]
authors = ["Kayumov A.I. <[email protected]>"]
exclude = ["./github"]
description = "Rust implementation of Link-Cut-Tree: self-balancing data structure to maintain a forest of rooted trees."
repository = "https://github.com/azizkayumov/lctree"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
[![crates.io](https://img.shields.io/crates/v/lctree)](https://crates.io/crates/lctree)

# lctree
Rust implementation of [Link-Cut-Tree](https://dl.acm.org/doi/10.1145/253262.253347](https://dl.acm.org/doi/pdf/10.1145/800076.802464)): self-balancing data structure to maintain a forest of rooted trees through linking and cutting edges.
Rust implementation of [Link-Cut-Tree](https://dl.acm.org/doi/10.1145/253262.253347](https://dl.acm.org/doi/pdf/10.1145/800076.802464)): self-balancing data structure to maintain a forest of rooted trees through dynamically linking and cutting edges.

## Example
This example shows how to link and cut edges:
```rust
use lctree::LinkCutTree;

fn main() {
// We form a link-cut tree from a rooted tree with the following structure:
// We form a link-cut tree from the following rooted tree:
// 0
// / \
// 1 4
Expand Down
35 changes: 31 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ impl LinkCutTree {
}
// detach w from its parent (which is v)
if let Some(left) = self.forest[w].left {
if left != v {
eprintln!("Error: no link between {v} and {w}"); // maybe this should be a panic?
return;
}
self.forest[w].left = None;
self.forest[left].parent = Parent::Root;
}
Expand Down Expand Up @@ -236,7 +240,7 @@ mod tests {

#[test]
pub fn cut() {
// We form a link-cut tree from a rooted tree with the following structure:
// We form a link-cut tree from the following rooted tree:
// 0
// / \
// 1 6
Expand Down Expand Up @@ -299,9 +303,32 @@ mod tests {
}
}

#[test]
pub fn cut_non_existing_edge() {
// We form a link-cut tree from the following rooted tree:
// 0
// / \
// 1 2
// |
// 3
let mut lctree = super::LinkCutTree::new();
for i in 0..4 {
lctree.make_tree(i as f64);
}
lctree.link(1, 0);
lctree.link(2, 0);
lctree.link(3, 2);

// Try to cut non-existing edge:
lctree.cut(1, 3); // should do nothing

// They should still be connected:
assert!(lctree.connected(1, 3));
}

#[test]
pub fn findroot() {
// We form a link-cut tree from a rooted tree with the following structure:
// We form a link-cut tree from the following rooted tree:
// 0
// / \
// 1 6
Expand Down Expand Up @@ -356,7 +383,7 @@ mod tests {

#[test]
pub fn reroot() {
// We form a link-cut tree from a rooted tree with the following structure:
// We form a link-cut tree from the following rooted tree:
// 0
// / \
// 1 4
Expand Down Expand Up @@ -392,7 +419,7 @@ mod tests {
lctree.make_tree(weights[i]);
}

// We form a link-cut tree from a rooted tree with the following structure
// We form a link-cut tree from the following rooted tree
// (the numbers in parentheses are the weights of the nodes):
// 0(9)
// / \
Expand Down
11 changes: 11 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### Benchmark
This benchmark report contains overall running time analysis of Link-Cut trees in comparison to its brute-force counterpart.
Each test performs a number of random operations (`link(v, w)`, `cut(v, w)`, `connected(v, w)` or `findmax(v, w)`) on forests of varying sizes.

| # Nodes | # Operations | Random seed | [lctree](https://github.com/azizkayumov/lctree/blob/main/src/lib.rs) | [brute-force](https://github.com/azizkayumov/lctree/blob/main/tests/test_random.rs) |
| :--- | :--- | :--- | :--- | :--- |
| 100 | 10K | 564315935137013477 | 6.567967ms | 53.48109ms |
| 100 | 100K | 5233351911053448040 | 44.379304ms | 321.900746ms |
| 100 | 1M | 10905789823848117209 | 476.117191ms | 3.915883695s |
| 500 | 2M | 5863263585868731364 | 984.139022ms | 11.542679321s |
| 1000 | 5M | 11338885474432604336 | 2.371485318s | 23.710666403s |
Loading