mirror of
https://codeberg.org/JasterV/word-ladder.hs.git
synced 2026-04-27 02:15:44 +00:00
refactor dsfSearch
This commit is contained in:
parent
d9621f7d55
commit
c959e7a9e1
1 changed files with 3 additions and 7 deletions
10
src/Graph.hs
10
src/Graph.hs
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue