NHacker Next
  • new
  • past
  • show
  • ask
  • show
  • jobs
  • submit
▲Binary Lifting (emi-h.com)
wingman-jr 8 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 7 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:

    Node 1:               1
    Node 2:              11
    Node 3:             111
    Node 4:            1111
    Node 5:           10111
    Node 6:          110111
    Node 7:         1010111
    Node 8:        10000011
    Node 9:       100000011
    Node 10:     1100000011
    Node 11:    11100000011
    Node 12:   111100000011
    Node 13:  1011100000011
    Node 14: 10000000000001

    Or with node IDs shown in hex:

                              1
                             0x1
                             /  \
                            /    \
                           2      14
                          0x3    0x2001
                         / | \
                        /  |  \
                       /   |   \
                      3    8    9
                    0x7   0x83  0x103
                     / \           \
                    /   \           \
                  4     5            10
                0xF   0x17          0x303
                        / \           \
                       /   \           \
                      6    7            11
                     0x37  0x57        0x703
                                        / \
                                       /   \
                                      12    13
                                    0xF03   0x1703
                               
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:

    Node lca(HashMap<BitVector, Node> map, Node lhs, Node rhs) 
    {
        return map[BitVector.AND(lhs.ID, rhs.ID)];
    }
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 20 hours 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 18 hours 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.

sparkie 6 hours ago [-]
GP probably asked his clanker for a summary.
Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact
Rendered at 09:34:27 GMT+0000 (Coordinated Universal Time) with Vercel.