Rendered at 18:33:31 GMT+0000 (Coordinated Universal Time) with Cloudflare Workers.
wingman-jr 17 hours ago [-]
Fun article! I was trying to think about applications of this algorithm. I suppose things like deep GUI trees could maybe be an area when you are doing e.g. pick code, but I suppose for the algorithmic part to matter with size maybe you get into like internet routing or something else big like that.
sparkie 16 hours ago [-]
There's a use in programming languages for type inference under subtyping (single inheritance) - ie, get the most derived class which is a supertype of two others.
A neat trick to implement LCA for small trees is to use dynamically sized bitvectors. You do a preorder traversal on the tree with a count `n` starting at 0 for the root. Each node visited increments `n`, and each node is given an ID by setting bit `n` to 1 in a bit vector and performing a bitwise OR with the parent node's ID.
Eg, for the given example where the preorder traversal is already in numerical order: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], the node ID's (as bit vectors) would become:
LCA then is just the bitwise AND of any two node IDs (assume zero-extended to the width of the largest one).
You could perhaps have a hashmap of ID->Node which has constant time lookup - would be fairly trivial to write since preorder traversal is the simplest to implement with recursion - eg, something like:
int prepare(HashMap<BitVector, Node> map, Node node, BitVector parentID, int n)
{
node.ID = BitVector.OR(BitVector.SetBit(n), parentID);
map[node.ID] = node;
foreach (var child in node.children)
n = prepare(map, child, node.ID, ++n);
return n;
}
var map = new HashMap<BitVector, Node>();
prepare(map, root, BitVector.Empty, 0);
This obviously has O(n) complexity as it visits every node once.
After preparation, you can perform the LCA in sub-linear time:
It's not quite O(1) because the size of our bitvectors depends on the size of the tree - but lets say we implement our bitvector as an array of `uint64` words, the problem scales logarithmically, but not at log₂(n) like a plain binary tree would - it would be ⅙log₂(n), since we only need to add an extra word to the bit vector for every 64 (2⁶) items in the tree. We're also not following a bunch of pointers - our bit vector is cache friendly.
Probably not suitable for bigger trees, mainly because the size of our node IDs would grow too big.
andsoitis 1 days ago [-]
Central algorithm and its implementation are correct.
BUT several mathematical inaccuracies, misleading complexity statements, unnecessary restrictions, and at least one demonstrably false claim about heavy-light decomposition.
emih 1 days ago [-]
You saw all that in 6 minutes?
And can you at least explain what is wrong exactly? Because I can't seem to find these mistakes and misleading claims.
A neat trick to implement LCA for small trees is to use dynamically sized bitvectors. You do a preorder traversal on the tree with a count `n` starting at 0 for the root. Each node visited increments `n`, and each node is given an ID by setting bit `n` to 1 in a bit vector and performing a bitwise OR with the parent node's ID.
Eg, for the given example where the preorder traversal is already in numerical order: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], the node ID's (as bit vectors) would become:
LCA then is just the bitwise AND of any two node IDs (assume zero-extended to the width of the largest one).You could perhaps have a hashmap of ID->Node which has constant time lookup - would be fairly trivial to write since preorder traversal is the simplest to implement with recursion - eg, something like:
This obviously has O(n) complexity as it visits every node once.After preparation, you can perform the LCA in sub-linear time:
It's not quite O(1) because the size of our bitvectors depends on the size of the tree - but lets say we implement our bitvector as an array of `uint64` words, the problem scales logarithmically, but not at log₂(n) like a plain binary tree would - it would be ⅙log₂(n), since we only need to add an extra word to the bit vector for every 64 (2⁶) items in the tree. We're also not following a bunch of pointers - our bit vector is cache friendly.Probably not suitable for bigger trees, mainly because the size of our node IDs would grow too big.
BUT several mathematical inaccuracies, misleading complexity statements, unnecessary restrictions, and at least one demonstrably false claim about heavy-light decomposition.
And can you at least explain what is wrong exactly? Because I can't seem to find these mistakes and misleading claims.