Skip to content

Instantly share code, notes, and snippets.

@rasendubi
Created February 8, 2013 20:13
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 rasendubi/4741618 to your computer and use it in GitHub Desktop.
Save rasendubi/4741618 to your computer and use it in GitHub Desktop.
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