Permalink
Browse files

use 1/0 as infinity

  • Loading branch information...
1 parent 68039e4 commit a8a20f1398330281c1faba0830816a51e51c7e08 @ollielo committed Mar 8, 2012
Showing with 27 additions and 0 deletions.
  1. +27 −0 DynamicProgramming/shortestPath.hs
@@ -0,0 +1,27 @@
+import Data.Array
+
+-- assuming graph is connected
+shortestPath graph start terminal = table ! (start, nnodes)
+ where
+ nnodes = length graph
+ bnds = ((0, 0), (nnodes, nnodes))
+ table = array bnds [(ij, cost ij) | ij <- range bnds]
+ -- the cost of pathes from terminal to terminal of any number of hops
+ -- is zero.
+ -- TODO: a better to deal with Infinity?
+ cost (_, 0) = 1/0
+ cost (node, hops)
+ | node == terminal = 0
+ | otherwise = min (table!(node, hops-1))
+ (minimum [c + table!(n, hops-1) | (n, c) <- graph!!node])
+
+main :: IO ()
+main =
+ let graph = [[(1, 1), (3, 2), (4, 5)],
+ [(0, 1), (2, 2), (4, 5)],
+ [(1, 2), (4, 1), (5, 4)],
+ [(0, 2), (4, 3)],
+ [(0, 5), (1, 5), (2, 1), (3, 3), (5, 1)],
+ [(2, 4), (4, 1)]]
+ in
+ print $ shortestPath graph 0 5

0 comments on commit a8a20f1

Please sign in to comment.