9

我对用于计算一元运算符下的容器闭包的高效函数算法(最好在 Haskell 中,甚至更优选已经作为库的一部分实现!)感兴趣。

对于列表,我想到的一个基本且低效的示例是:

closure :: Ord a => (a -> a) -> [a] -> [a]
closure f xs = first_dup (iterate (\xs -> nub $ sort $ xs ++ map f xs) xs) where
    first_dup (xs:ys:rest) = if xs == ys then xs else first_dup (ys:rest)

更有效的实现会跟踪每个阶段(“边缘”)生成的新元素,并且不会将该函数应用于已应用的元素:

closure' :: Ord a => (a -> a) -> [a] -> [a]
closure' f xs = stable (iterate close (xs, [])) where
    -- return list when it stabilizes, i.e., when fringe is empty
    stable ((fringe,xs):iterates) = if null fringe then xs else stable iterates

    -- one iteration of closure on (fringe, rest);  key invariants:
    -- (1) fringe and rest are disjoint; (2) (map f rest) subset (fringe ++ rest)
    close (fringe, xs) = (fringe', xs') where
        xs' = sort (fringe ++ xs)
        fringe' = filter (`notElem` xs') (map f fringe)

例如,如果xs是 的非空子列表[0..19]closure' (\x->(x+3)`mod`20) xs则为 [0..19],并且迭代稳定在 20 步[0]、13 步[0,1]和 4 步[0,4,8,12,16]

使用基于树的有序集实现可以获得更高的效率。这已经完成了吗?在二元(或更高元数)运算符下的闭包相关但更难的问题呢?

4

1 回答 1

7

像这样使用 Hash Array Mapped Trie 数据结构的东西怎么样unordered-containers?对于无序容器member,并且insertO(min(n,W)),其中W是散列的长度。

module Closed where

import Data.HashSet (HashSet)
import Data.Hashable
import qualified Data.HashSet as Set

data Closed a = Closed { seen :: HashSet a, iter :: a -> a } 

insert :: (Hashable a, Eq a) => a -> Closed a -> Closed a
insert a c@(Closed set iter)
  | Set.member a set = c
  | otherwise        = insert (iter a) $ Closed (Set.insert a set) iter

empty :: (a -> a) -> Closed a
empty = Closed Set.empty

close :: (Hashable a, Eq a) => (a -> a) -> [a] -> Closed a
close iter = foldr insert (empty iter)

这是上面的一个变体,它以广度优先的方式更懒惰地生成解决方案集。

data Closed' a = Unchanging | Closed' (a -> a) (HashSet a) (Closed' a)

close' :: (Hashable a, Eq a) => (a -> a) -> [a] -> Closed' a
close' iter = build Set.empty where
  inserter :: (Hashable a, Eq a) => a -> (HashSet a, [a]) -> (HashSet a, [a])
  inserter a (set, fresh) | Set.member a set = (set, fresh)
                          | otherwise        = (Set.insert a set, a:fresh)
  build curr [] = Unchanging
  build curr as =
    Closed' iter curr $ step (foldr inserter (curr, []) as)
  step (set, added) = build set (map iter added)

-- Only computes enough iterations of the closure to 
-- determine whether a particular element has been generated yet
-- 
-- Returns both a boolean and a new 'Closed'' value which will 
-- will be more precisely defined and thus be faster to query
member :: (Hashable a, Eq a) => a -> Closed' a -> (Bool, Closed' a)
member _ Unchanging = False
member a c@(Closed' _ set next) | Set.member a set = (True, c)
                                | otherwise        = member a next

improve :: Closed' a -> Maybe ([a], Closed' a)
improve Unchanging = Nothing
improve (Closed' _ set next) = Just (Set.toList set, next)

seen' :: Closed' a -> HashSet a
seen' Unchanging = Set.empty
seen' (Closed' _ set Unchanging) = set
seen' (Closed' _ set next)       = seen' next

并检查

>>> member 6 $ close (+1) [0]
...

>>> fst . member 6 $ close' (+1) [0]
True
于 2013-10-21T02:46:49.403 回答