lctree
Rust implementation of Link-cut tree: self-balancing data structure to maintain a dynamic forest of (un)rooted trees under the following operations that take O(logn) amortized time:
link(v, w): creates an edge between nodesvandw.cut(v, w): removes the edge between nodesvandw.connected(v, w): returnstrueif nodesvandware in the same tree.path(v, w): performs calculations on a path between nodesvandw.
Usage
This example shows how to link and cut edges:
use LinkCutTree;
Advanced usage include operations on paths:
Various kinds of calculations can be performed on a path between two nodes, such as findmax, findmin, or findsum:
use ;
A custom path aggregate function can be defined by using the Path trait:
use ;
Benchmark
The overall running time for performing a number of random operations (link(v, w), cut(v, w), connected(v, w) or findmax(v, w)) on forests of varying sizes (check benchmark details here).
| # Nodes | # Operations | lctree | brute-force |
|---|---|---|---|
| 100 | 10K | 4.8161 ms | 18.013 ms |
| 200 | 20K | 11.091 ms | 69.855 ms |
| 500 | 50K | 31.623 ms | 429.53 ms |
| 1000 | 100K | 68.649 ms | 1.8746 s |
| 5000 | 500K | 445.83 ms | 46.854 s |
| 10K | 1M | 964.64 ms | 183.24 s |
Credits
This crate applies the core concepts and ideas presented in the following sources:
- "A data structure for dynamic trees" by D. Sleator and R. E. TarJan (published in STOC '81).
- Link-cut tree source code by the author D. Sleator.
- MIT's lecture on dynamic graphs: lecture, notes, and source code.
- Helpful blog posts on the concepts of rooted trees, rerooting and splay operations.
License
This project is licensed under the Apache License, Version 2.0 - See the LICENSE.md file for details.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.