bfs: use a Set for visited nodes
This commit is contained in:
parent
7f0afa76a6
commit
56a8dd2b30
2 changed files with 6 additions and 4 deletions
|
@ -1,7 +1,9 @@
|
|||
module Logic.Graph where
|
||||
|
||||
bfs :: Eq a => a -> a -> (a -> [(edge, a)]) -> Maybe [edge]
|
||||
bfs goal start getEdges = reverse <$> aux [([], start)] []
|
||||
import Data.Set (Set, insert)
|
||||
|
||||
bfs :: (Eq a, Ord a) => a -> a -> (a -> [(edge, a)]) -> Maybe [edge]
|
||||
bfs goal start getEdges = reverse <$> aux [([], start)] mempty
|
||||
where
|
||||
aux [] _ = Nothing
|
||||
aux ((path, vertex):queue) visited
|
||||
|
@ -12,7 +14,7 @@ bfs goal start getEdges = reverse <$> aux [([], start)] []
|
|||
case filter (\(_, v) -> v == goal) new of
|
||||
[] ->
|
||||
let queue' = queue ++ map (\(edge, next) -> (edge:path, next)) new
|
||||
in aux queue' (vertex:visited)
|
||||
in aux queue' $ insert vertex visited
|
||||
((edge, _):_) -> Just (edge:path)
|
||||
|
||||
getUnvisitedAdjacent vertex visited =
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue