1
0
Fork 0

Problem 83: Partially implemented priority queue implementation of Dijkstra's for finding shortest path.

This commit is contained in:
Jonathan Chan 2017-10-23 20:45:38 -07:00
parent 5a49c8845f
commit d09a6587ff
5 changed files with 64 additions and 2 deletions

4
83.hs
View File

@ -16,8 +16,8 @@ instance Monoid Element where
case getMin v1 v2 of
0 -> mempty
v -> if v == v1
then Element v1 Visited p1
else Element v2 Visited p2
then Element v1 Visited p1
else Element v2 Visited p2
getMin :: Value -> Value -> Value
getMin v1 0 = v1

BIN
83_alt Normal file

Binary file not shown.

BIN
83_alt.hi Normal file

Binary file not shown.

62
83_alt.hs Normal file
View File

@ -0,0 +1,62 @@
import Data.List.Split
import Data.Matrix
import Data.Maybe
import Data.Foldable
import Data.PQueue.Min
type Value = Integer
type Distance = Maybe Integer -- Nothing represents infinity
type Position = (Int, Int)
data Element = Element {
value :: Value,
distance :: Distance,
position :: Position
} deriving (Eq, Show)
data PQE = PQE Element Distance deriving (Eq, Show)
instance Ord PQE where
PQE _ Nothing <= _ = False
_ <= PQE _ Nothing = True
PQE _ (Just d1) <= PQE _ (Just d2) = d1 <= d2
dijkstra :: Matrix Element -> MinQueue PQE -> Position -> Distance
dijkstra m q p =
let PQE minElement minDistance = findMin q
in if position minElement == p
then distance minElement
else undefined --TODO
findShortestPathLength :: Matrix Element -> Distance
findShortestPathLength m =
let initialElement = m ! (1, 1)
initialMinQ = singleton $ PQE initialElement $ distance initialElement
lastPos = (nrows m, ncols m)
in dijkstra m initialMinQ lastPos
setInitial :: Matrix Element -> Matrix Element
setInitial m =
let v = value $ m ! (1, 1)
maybeMatrix = safeSet (Element v (Just v) (1, 1)) (1, 1) m
in case maybeMatrix of
Just mm -> mm
Nothing -> m
initElement :: Matrix Integer -> Position -> Element
initElement m p =
let value = m ! p
in Element value Nothing p
toElementMatrix :: Matrix Integer -> Matrix Element
toElementMatrix m =
matrix (nrows m) (ncols m) (initElement m)
main :: IO ()
main = do
contents <- readFile "p083_matrix.txt"
let listsMatrix = fmap (fmap read . (splitOn ",")) $ lines contents :: [[Integer]]
valueMatrix = fromLists listsMatrix
unvisitedMatrix = toElementMatrix valueMatrix
mtrx = setInitial unvisitedMatrix
print $ findShortestPathLength mtrx

BIN
83_alt.o Normal file

Binary file not shown.