/lca-python

Lowest Common Ancestor in Python

Primary LanguagePythonMozilla Public License 2.0MPL-2.0

Lowest Common Ancestor in Python

This repository has 3 implementations.

  • lca.impl.n_1::build:

    • naive and simplest approach
    • $O(n)$ preprocessing time & space
    • $O(\log n)$ query time
  • lca.impl.nlogn_1::build:

    • Euler tour then RMQ
    • $O(n \log n)$ preprocessing time & space
    • $O(1)$ query time
  • lca.impl.n_1::build:

Example Usage

Simply pass the tree and its children iterator.

import lca.constant_time_linear_space as lca_factory

# Nested tuple as tree
T = ((('a', 'b'), ('c',)), ('d', ('e', 'f')))


def v2vs(v):
    """Children iterator for nested tuple"""
    return v if isinstance(v, tuple) else ()


lca = lca_factory.build(T, v2vs)
assert lca(T[0], T[1]) == T
assert lca(T[1], T[0]) == T
assert lca(T[0][0], T[0][0]) == T[0][0]
assert lca(T[0][0], T[0][0][0]) == T[0][0]
assert lca(T[0][0], T[0][1]) == T[0]
assert lca(T[0][0], T[0][1][0]) == T[0]
assert lca(T[0][0], T[1][1][0]) == T
assert lca(T[0][0], T[1][1][1]) == T
assert lca(T[1][0], T[1][1][1]) == T[1]

Benchmark

benchmark.png

Note