lca-0.2.3: O(log n) persistent on-line lowest common ancestor calculation without preprocessing

Portabilityportable
Stabilityexperimental
MaintainerEdward Kmett <ekmett@gmail.com>
Safe HaskellSafe-Inferred

Data.LCA.Online.Monoidal

Description

Provides online calculation of the the lowest common ancestor in O(log h) by compressing the spine of the paths using a skew-binary random access list.

This library implements the technique described in my talk

http://www.slideshare.net/ekmett/skewbinary-online-lowest-common-ancestor-search

to improve the known asymptotic bounds on both online lowest common ancestor search

http://en.wikipedia.org/wiki/Lowest_common_ancestor

and the online level ancestor problem:

http://en.wikipedia.org/wiki/Level_ancestor_problem

Algorithms used here assume that the key values chosen for k are globally unique.

This version provides access to a monoidal "summary" of the elided path for many operations.

Synopsis

Documentation

data Path a Source

A compressed Path as a skew binary random access list

Instances

Foldable Path 
Read a => Read (Path a) 
Show a => Show (Path a) 

toList :: Path a -> [(Int, a)]Source

Convert a Path to a list of (ID, value) pairs.

fromList :: Monoid a => [(Int, a)] -> Path aSource

Build a Path from a list of (ID, value) pairs.

map :: Monoid b => (a -> b) -> Path a -> Path bSource

O(n) Re-annotate a Path full of monoidal values using a different Monoid.

mapHom :: (a -> b) -> Path a -> Path bSource

O(n) Re-annotate a Path full of monoidal values/

Unlike map, mapHom f assumes that f is a Monoid homomorphism, that is to say you must ensure

 f a `'mappend'` f b = f (a `'mappend'` b)
 f mempty = mempty

mapWithKey :: Monoid b => (Int -> a -> b) -> Path a -> Path bSource

O(n) Re-annotate a Path full of monoidal values with access to the key.

traverse :: (Applicative f, Monoid b) => (a -> f b) -> Path a -> f (Path b)Source

Traverse a Path yielding a new monoidal annotation.

traverseWithKey :: (Applicative f, Monoid b) => (Int -> a -> f b) -> Path a -> f (Path b)Source

Traverse a Path with access to the node IDs.

empty :: Path aSource

The empty Path

cons :: Monoid a => Int -> a -> Path a -> Path aSource

O(1) Invariant: most operations assume that the keys k are globally unique

Extend the Path with a new node ID and value.

uncons :: Monoid a => Path a -> Maybe (Int, a, Path a)Source

O(1) Extract the node ID and value from the newest node on the Path.

view :: Monoid a => Path a -> View Path aSource

O(1) Extract the node ID and value from the newest node on the Path, slightly faster than uncons.

null :: Path a -> BoolSource

O(1) Returns True iff the path is empty.

length :: Path a -> IntSource

O(1) Determine the length of a Path.

measure :: Monoid a => Path a -> aSource

Extract a monoidal summary of a Path.

isAncestorOf :: Monoid b => Path a -> Path b -> BoolSource

O(log h) xs `'isAncestorOf'` ys holds when xs is a prefix starting at the root of path ys.

keep :: Monoid a => Int -> Path a -> Path aSource

O(log (h - k)) to keep k elements of Path of length h

This solves the online version of the "level ancestor problem" with no preprocessing in O(log h) time, improving known complexity bounds.

http://en.wikipedia.org/wiki/Level_ancestor_problem

mkeep :: Monoid a => Int -> Path a -> (a, Path a)Source

O(log (h - k)) to keep k elements of Path of length h, and provide a monoidal summary of the dropped elements

drop :: Monoid a => Int -> Path a -> Path aSource

O(log k) to drop k elements from a Path

mdrop :: Monoid a => Int -> Path a -> (a, Path a)Source

O(log k) to drop k elements from a Path and provide a monoidal summary of the dropped elements

(~=) :: Path a -> Path b -> BoolSource

O(1) Compare to see if two trees have the same leaf key

lca :: (Monoid a, Monoid b) => Path a -> Path b -> Path aSource

O(log h) Compute the lowest common ancestor of two paths

mlca :: (Monoid a, Monoid b) => Path a -> Path b -> (a, Path a, b, Path b)Source

O(log h) Compute the lowest common ancestor of two paths along with a monoidal summary of their respective tails.