-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstraSpec.hs
More file actions
97 lines (81 loc) · 3.25 KB
/
DijkstraSpec.hs
File metadata and controls
97 lines (81 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
module Algorithms.ShortestPaths.DijkstraSpec where
import Algorithms.ShortestPaths.Dijkstra
import Algorithms.TestUtil
import Algorithms.Utility
import Control.Monad
import Data.Bifunctor
import qualified Data.Map.Strict as M
import Test.Hspec
import Test.Hspec.QuickCheck
import Test.QuickCheck
import Test.QuickCheck.Monadic
spec :: Spec
spec = do
describe "Dijkstra's algorithm" $ do
prop "works on Manhattan distances" dijkstrasManhattan
prop "works on line graphs" dijkstrasQuadraticLine
prop "halts on negative cycles" dijkstrasNegativeCostCycle
manhattanCost :: (Int, Int) -> (Int, Int) -> Int
manhattanCost (x, y) (x', y') = abs (x - x') + abs (y - y')
manhattanGridNeighbors :: Int -> (Int, Int) -> [(Int, Int)]
manhattanGridNeighbors width (x, y) = filter inBounds [(x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)]
where
inBounds = uncurry (&&) . dup bimap (between 0 width)
dijkstrasManhattan :: Positive Int -> (Int, Int) -> (Int, Int) -> Property
dijkstrasManhattan (Positive width) source' sink' =
let mkValid = bimap f f
where
f = (`mod` (width + 1))
-- Map the source and sink to valid indices
source = mkValid source'
sink = mkValid sink'
adjacent u v = manhattanCost u v == 1
in monadicIO $ do
case dijkstraPath manhattanCost (manhattanGridNeighbors width) source sink of
Nothing ->
assertWith False "to find the sink"
Just (path, actual) -> do
-- Is the cost correct?
manhattanCost source sink ==? actual
-- Do we start at the source?
case path of
x : _ -> source ==? x
_ -> failure "empty path"
-- Do we end at the sink?
case reverse path of
x : _ -> assertWith (x == sink) (show path) -- sink ==? x
_ -> failure "empty path"
-- Are all path elements adjacent?
forM_ (zip path (tail path)) $ \(u, v) ->
assertWith (adjacent u v) (show u <> " not adjacent to " <> show v)
quadraticLineCost :: Int -> Int -> Int
quadraticLineCost x y = (x - y) ^ (2 :: Int)
lineNeighbors :: Int -> Int -> [Int]
lineNeighbors nodes = const [0 .. nodes - 1]
dijkstrasQuadraticLine :: Positive Int -> Int -> Int -> Property
dijkstrasQuadraticLine (Positive nodes) source' sink' =
let mkValid = (`mod` nodes)
source = mkValid source'
sink = mkValid sink'
in monadicIO $ do
case dijkstraPath quadraticLineCost (lineNeighbors nodes) source sink of
Nothing ->
failure "to find the sink"
Just (path, actual) -> do
-- Is the cost correct?
abs (sink - source) ==? actual
-- Do we start at the source?
case path of
x : _ -> source ==? x
_ -> failure "empty path"
-- Do we end at the sink?
case reverse path of
x : _ -> assertWith (x == sink) (show path) -- sink ==? x
_ -> failure "empty path"
dijkstrasNegativeCostCycle :: Property
dijkstrasNegativeCostCycle =
let nodes = [0 .. 4] :: [Int]
tree = dijkstraTree (\_ _ -> (-1 :: Int)) (const nodes) 0
in monadicIO $
forM_ nodes $ \u ->
assertWith (M.member u tree) ("to find " <> show u)