Skip to content

Instantly share code, notes, and snippets.

@stevekrenzel
Created January 1, 2013 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevekrenzel/e211379e888a4326f68d to your computer and use it in GitHub Desktop.
Save stevekrenzel/e211379e888a4326f68d to your computer and use it in GitHub Desktop.
import Data.Ord (comparing)
import Data.Map (fromList, lookup)
import Data.List (union, sortBy)
import Data.Graph (buildG, reachable, topSort)
import Data.Maybe (fromJust)
graph = buildG (0, 4) [(3,2), (2,4), (2,1), (1,0)]
dependencies = buildG (0, 4) [(0, 1), (1, 2)]
ordering = fromList $ zip (topSort graph) [0..]
f nodes = sortBy cmp (deps `union` nodes)
where deps = concatMap (reachable dependencies) nodes
cmp = comparing (fromJust . lookup ordering)
lookup = flip Data.Map.lookup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment