verify graph path search
This commit is contained in:
parent
687b5f1040
commit
fa1a39d1da
1 changed files with 18 additions and 0 deletions
|
@ -35,3 +35,21 @@ Just ["succ","succ","succ","succ","succ","succ","succ","succ","succ","succ"]
|
||||||
ghci> bfs 13 0 (\x -> [("double", x+x), ("add", x+1)])
|
ghci> bfs 13 0 (\x -> [("double", x+x), ("add", x+1)])
|
||||||
Just ["add","double","add","double","double","add"]
|
Just ["add","double","add","double","double","add"]
|
||||||
-}
|
-}
|
||||||
|
|
||||||
|
data VerifyPathError vertex edge
|
||||||
|
= NoSuchEdge vertex edge
|
||||||
|
|
||||||
|
verifyPath
|
||||||
|
:: (Eq vertex, Ord vertex, Eq edge)
|
||||||
|
=> GetEdges vertex edge
|
||||||
|
-> vertex
|
||||||
|
-> vertex
|
||||||
|
-> [edge]
|
||||||
|
-> Either (VerifyPathError vertex edge) Bool
|
||||||
|
verifyPath getEdges goal start path =
|
||||||
|
case path of
|
||||||
|
[] -> Right $ start == goal
|
||||||
|
(thisEdge:nextEdges) ->
|
||||||
|
case map fst $ filter (\(next, realEdge) -> realEdge == thisEdge) $ getEdges start of
|
||||||
|
[] -> Left $ NoSuchEdge start thisEdge
|
||||||
|
(next:_) -> verifyPath getEdges goal next nextEdges
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue