refactor dsfSearch

This commit is contained in:
JasterV 2025-03-23 17:36:01 +01:00
parent d9621f7d55
commit c959e7a9e1

View file

@ -113,17 +113,13 @@ bfsSearch initialGraph start end
else else
bfsSearch' (frontier', graph', predecessors') bfsSearch' (frontier', graph', predecessors')
type Path a = [a] dfsSearch :: forall a. (Eq a) => DiGraph a -> a -> a -> Maybe [a]
type DfsSearchResult a = Either (DiGraph a) (Path a)
dfsSearch :: forall a. (Eq a) => DiGraph a -> a -> a -> Maybe (Path a)
dfsSearch initialGraph start end = dfsSearch initialGraph start end =
case dfsSearch' initialGraph start of case dfsSearch' initialGraph start of
Right path -> Just path Right path -> Just path
Left _ -> Nothing Left _ -> Nothing
where where
dfsSearch' :: DiGraph a -> a -> DfsSearchResult a dfsSearch' :: DiGraph a -> a -> Either (DiGraph a) [a]
dfsSearch' graph node dfsSearch' graph node
| node == end = Right [node] | node == end = Right [node]
| otherwise = | otherwise =
@ -133,7 +129,7 @@ dfsSearch initialGraph start end =
Right path -> Right (node : path) Right path -> Right (node : path)
Left graph'' -> Left graph'' Left graph'' -> Left graph''
searchNeighbours :: [a] -> DiGraph a -> DfsSearchResult a searchNeighbours :: [a] -> DiGraph a -> Either (DiGraph a) [a]
searchNeighbours [] graph = Left graph searchNeighbours [] graph = Left graph
searchNeighbours (x : xs) graph = case dfsSearch' graph x of searchNeighbours (x : xs) graph = case dfsSearch' graph x of
-- If a path was found, just return it -- If a path was found, just return it