Skip to content

Commit

Permalink
While we're at it we might as well improve the known asymptotics for …
Browse files Browse the repository at this point in the history
…the online level ancestor problem.
  • Loading branch information
ekmett committed May 8, 2013
1 parent ae01fc8 commit 53a721c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
0.2.3
-----
* Improved documentation to also note that this package also provides an improvement in the online version of the [level ancestor problem](http://en.wikipedia.org/wiki/Level_ancestor_problem).

0.2.2
-----
* Added README
Expand Down
2 changes: 1 addition & 1 deletion lca.cabal
@@ -1,6 +1,6 @@
name: lca
category: Algorithms, Data Structures
version: 0.2.2
version: 0.2.3
license: BSD3
cabal-version: >= 1.6
license-file: LICENSE
Expand Down
13 changes: 12 additions & 1 deletion src/Data/LCA/Online.hs
Expand Up @@ -16,7 +16,13 @@
--
-- <http://www.slideshare.net/ekmett/skewbinary-online-lowest-common-ancestor-search>
--
-- to improve the known asymptotic bounds on 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.
Expand Down Expand Up @@ -155,6 +161,11 @@ view (Cons _ w (Bin k a l r) ts) = Node k a (consT w2 l (consT w2 r ts)) where w
{-# INLINE view #-}

-- | /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>
keep :: Int -> Path a -> Path a
keep = go where
go _ Nil = Nil
Expand Down
21 changes: 21 additions & 0 deletions src/Data/LCA/Online/Monoidal.hs
Expand Up @@ -12,9 +12,24 @@
-- 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.
--
----------------------------------------------------------------------------
module Data.LCA.Online.Monoidal
( Path
Expand Down Expand Up @@ -198,6 +213,7 @@ view (Cons _ _ w (Bin _ k a l r) ts) = Node k a (consT w2 l (consT w2 r ts)) whe
{-# INLINE view #-}

-- | /O(log (h - k))/ to keep @k@ elements of 'Path' of 'length' @h@, and provide a monoidal summary of the dropped elements
--
mkeep :: Monoid a => Int -> Path a -> (a, Path a)
mkeep = go mempty where
go as _ Nil = (as, Nil)
Expand All @@ -218,6 +234,11 @@ mkeep = go mempty where
{-# INLINE mkeep #-}

-- | /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>
keep :: Monoid a => Int -> Path a -> Path a
keep k xs = snd (mkeep k xs)
{-# INLINE keep #-}
Expand Down

0 comments on commit 53a721c

Please sign in to comment.