Created
February 3, 2015 00:52
-
-
Save ajtulloch/2299f8e00d15c6bd8882 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DR where | |
import Control.Applicative | |
import Data.Graph.Inductive.Graph | |
import Data.Graph.Inductive.PatriciaTree | |
import Data.Graph.Inductive.Query | |
import qualified Data.Map as M | |
newtype Task = Task Int deriving (Eq, Ord, Show) | |
example :: M.Map Task [Task] | |
example = M.fromList [ | |
(Task 1, [Task 2, Task 3, Task 4]), | |
(Task 2, [Task 3]), | |
(Task 3, []), | |
(Task 4, [Task 2]) | |
] | |
mdfs :: Graph gr => gr a b -> Maybe [Node] | |
mdfs g = mapM unique (scc g) | |
where | |
unique [x] = Just x | |
unique _ = Nothing | |
depGraph :: M.Map Task [Task] -> Gr () () | |
depGraph deps = mkUGraph nodes' edges' | |
where | |
nodes' = map ((\(Task p) -> p) . fst) (M.toList deps) | |
edges' = concatMap (uncurry reverseDeps) (M.toList deps) | |
reverseDeps (Task child) = map (\(Task p) -> (p, child)) | |
-- input - task -> [list of parents] | |
dr :: M.Map Task [Task] -> Maybe [Task] | |
dr deps = map Task <$> mdfs (depGraph deps) | |
main :: IO () | |
main = do | |
prettyPrint (depGraph example) | |
print $ dr example |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment