We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I cheated, and rather than doing a breadth first search, I simply used black magic:
dist startNode graph = dists where
dists = map getDist graph
getDist (Node n ns)
| n == start = Z
| otherwise = S . infMin . map ((dists !!) . subtract 1 . index) $ ns
So, infMin is just a specialized version of minimium :: Ord a => [a] -> a, that works even when a is potentially infinite, and the list is potentially empty. All we're saying is that the start node is distance zero (Z) and any other node is 1 more (S aka Successor) than the minimum distance of its neighbours. No need to check for disconnected components, they are simply an infinite distance away.
N.B. Declaring that the minimum of an empty list is in fact infinity allows the invariant:
min (infMin a) (infMin b) == infMin (a ++ b)
Which is handy.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Breadth First Search: Shortest Reach
You are viewing a single comment's thread. Return to all comments →
I cheated, and rather than doing a breadth first search, I simply used black magic:
So, infMin is just a specialized version of minimium :: Ord a => [a] -> a, that works even when a is potentially infinite, and the list is potentially empty. All we're saying is that the start node is distance zero (Z) and any other node is 1 more (S aka Successor) than the minimum distance of its neighbours. No need to check for disconnected components, they are simply an infinite distance away.
N.B. Declaring that the minimum of an empty list is in fact infinity allows the invariant:
Which is handy.