最近,我在Stackoverflow上提出了一个有关从图中构建DFS树的问题,并学习到可以通过使用State Monad来简单实现。
尽管DFS仅需要跟踪已访问的节点,因此我们可以使用'Set'或'List'或某种线性数据结构来跟踪已访问的节点,但BFS需要使用“已访问节点”和“队列”数据结构才能完成。
我的BFS伪代码如下:
Q = empty queue
T = empty Tree
mark all nodes except u as unvisited
while Q is nonempty do
u = deq(Q)
for each vertex v ∈ Adj(u)
if v is not visited
then add edge (u,v) to T
Mark v as visited and enq(v)
从伪代码中可以推断出,每次迭代只需执行3个步骤。
- 从队列中取消排队的点
- 将该点所有未访问的邻居添加到当前树的子节点、队列和“visited”列表中
- 重复上述步骤直至处理完队列中的下一个点
由于我们不使用递归遍历BFS搜索,因此需要其他遍历方法,如while循环。我查阅了hackage中的loop-while包,但它似乎已经过时。
我认为我需要类似于以下代码的东西:
{-...-}
... = evalState (bfs) ((Set.singleton start),[start])
where
neighbors x = Map.findWithDefault [] x adj
bfs =do (vis,x:queue)<-get
map (\neighbor ->
if (Set.member neighbor vis)
then put(vis,queue)
else put ((Set.insert neighbor vis), queue++[neighbor]) >> (addToTree neighbor)
) neighbors x
(vis,queue)<-get
while (length queue > 0)
我知道这个实现非常错误,但这应该能够提供我认为BFS应该如何实现的最简视图。另外,我真的不知道如何规避使用while循环来进行块操作。(例如,我应该使用递归算法来克服它,还是应该考虑完全不同的策略)
考虑到我在上面链接的先前问题中找到的一个答案,答案似乎应该像这样:
newtype Graph a = Graph (Map.Map a [a]) deriving (Ord, Eq, Show)
data Tree a = Tree a [Tree a] deriving (Ord, Eq, Show)
bfs :: (Ord a) => Graph a -> a -> Tree a
bfs (Graph adj) start = evalState (bfs') ((Set.singleton start),[start])
where
bfs' = {-part where I don't know-}
最后,如果由于某种原因无法使用状态单子实现BFS(我相信不会出现这种情况),请纠正我的错误假设。
我看过一些在Haskell中实现BFS的例子,但是它们没有使用状态单子。我想了解更多关于如何处理状态单子的内容,但是找不到任何使用状态单子实现BFS的例子。
提前感谢。
编辑: 我想出了一种使用状态单子的算法,但却陷入了无限循环。
bfs :: (Ord a) => Graph a -> a -> Tree a
bfs (Graph adj) start = evalState (bfs' (Graph adj) start) (Set.singleton start)
bfs' :: (Ord a) => Graph a -> a -> State (Set.Set a) (Tree a)
bfs' (Graph adj) point= do
vis <- get
let neighbors x = Map.findWithDefault [] x adj
let addableNeighbors (x:xs) = if Set.member x vis
then addableNeighbors(xs)
else x:addableNeighbors(xs)
let addVisited (vis) (ns) = Set.union (vis) $ Set.fromList ns
let newVisited = addVisited vis $ addableNeighbors $ neighbors point
put newVisited
return (Tree point $ map (flip evalState newVisited) (map (bfs' (Graph adj)) $ addableNeighbors $ neighbors point))
EDIT2: 通过使用图来返回和队列来处理,我想出了一种获取BFS图的解决方案。尽管它不是生成BFS树/图的最佳解决方案,但它可以工作。
bfs :: (Ord a) => Graph a -> a -> Graph a
bfs (Graph adj) start = evalState (bfs' (Graph adj) (Graph(Map.empty)) [start]) (Set.singleton start)
bfs':: (Ord a) => Graph a -> Graph a -> [a] -> State (Set.Set a) (Graph a)
bfs' _ (Graph ret) [] = return (Graph ret)
bfs' (Graph adj) (Graph ret) (p:points)= do
vis <- get
let neighbors x = Map.findWithDefault [] x adj
let addableNeighbors ns
| null ns = []
| otherwise = if Set.member (head ns) vis
then addableNeighbors(tail ns)
else (head ns):addableNeighbors(tail ns)
let addVisited (v) (ns) = Set.union (v) $ Set.fromList ns
let unVisited = addableNeighbors $ neighbors p
let newVisited = addVisited vis unVisited
let unionGraph (Graph g1) (Graph g2) = Graph (Map.union g1 g2)
put newVisited
bfs' (Graph adj) (unionGraph (Graph ret) (Graph (Map.singleton p unVisited))) (points ++ unVisited)
编辑3: 我添加了将图转换为树的转换函数。在编辑2和编辑3中运行该函数将产生BFS Tree。从计算时间方面来看,这不是最佳算法,但我相信对像我这样的新手来说它很直观易懂 :)
graphToTree :: (Ord a) => Graph a -> a -> Tree a
graphToTree (Graph adj) point = Tree point $ map (graphToTree (Graph adj)) $ neighbors point
where neighbors x = Map.findWithDefault [] x adj
bfs' :: Ord a => Graph a -> a -> State (S.Set a) (Tree a)
,它从给定节点开始执行BFS,S.Set a
是已访问节点。你不需要保留节点队列 - 在命令式环境中,这只是方便,但在这里并非如此。 - user2407038