Created
February 8, 2013 20:13
-
-
Save rasendubi/4741618 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
simpleLoops :: Graph -> [Path] | |
simpleLoops g = filterUniqueCycles $ concatMap (simpleLoopsFrom g) (map (:[]) $ vertices g) | |
simpleLoopsFrom :: Graph -> Path -> [Path] | |
simpleLoopsFrom g p | |
| isSimpleLoop p = [p] | |
| otherwise = concatMap (simpleLoopsFrom g) (map (:p) ov) | |
where | |
first = head p | |
ov = filter (not . (`elem` (init p))) $ outVertices g first | |
isSimpleLoop :: Path -> Bool | |
isSimpleLoop p = length p > 1 && head p == last p | |
filterUniqueCycles :: [Path] -> [Path] | |
filterUniqueCycles = (flip foldr []) $ \p ac -> | |
let cycles = map (concat . replicate 2 . tail) ac :: [Path] | |
in if any (p `isInfixOf`) cycles then ac | |
else p:ac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment