@@ -119,6 +119,7 @@ import Data.IntSet (IntSet)
119119import qualified Data.IntSet as Set
120120#endif
121121import Data.Tree (Tree (.. ), Forest )
122+ import qualified Data.Tree as Tree
122123
123124-- std interfaces
124125import Data.Foldable as F
@@ -647,15 +648,6 @@ run _ f = fst (runSetM (f contains include) Set.empty)
647648-- Algorithm 1: depth first search numbering
648649------------------------------------------------------------
649650
650- preorder' :: Tree a -> [a ] -> [a ]
651- preorder' (Node a ts) = (a : ) . preorderF' ts
652-
653- preorderF' :: [Tree a ] -> [a ] -> [a ]
654- preorderF' ts = foldr (.) id $ map preorder' ts
655-
656- preorderF :: [Tree a ] -> [a ]
657- preorderF ts = preorderF' ts []
658-
659651tabulate :: Bounds -> [Vertex ] -> UArray Vertex Int
660652tabulate bnds vs = UA. array bnds (zipWith (flip (,)) [1 .. ] vs)
661653-- Why zipWith (flip (,)) instead of just using zip with the
@@ -664,38 +656,33 @@ tabulate bnds vs = UA.array bnds (zipWith (flip (,)) [1..] vs)
664656-- list argument.
665657
666658preArr :: Bounds -> [Tree Vertex ] -> UArray Vertex Int
667- preArr bnds = tabulate bnds . preorderF
659+ preArr bnds = tabulate bnds . concatMap Tree. flatten
668660
669661------------------------------------------------------------
670662-- Algorithm 2: topological sorting
671663------------------------------------------------------------
672664
673- postorder :: Tree a -> [a ] -> [a ]
674- postorder (Node a ts) = postorderF ts . (a : )
675-
676- postorderF :: [Tree a ] -> [a ] -> [a ]
677- postorderF ts = foldr (.) id $ map postorder ts
678-
679- postOrd :: Graph -> [Vertex ]
680- postOrd g = postorderF (dff g) []
681-
682665-- | \(O(V+E)\). A topological sort of the graph.
683666-- The order is partially specified by the condition that a vertex /i/
684667-- precedes /j/ whenever /j/ is reachable from /i/ but not vice versa.
685668--
686669-- Note: A topological sort exists only when there are no cycles in the graph.
687670-- If the graph has cycles, the output of this function will not be a
688671-- topological sort. In such a case consider using 'scc'.
689- topSort :: Graph -> [Vertex ]
690- topSort = reverse . postOrd
672+ topSort :: Graph -> [Vertex ]
673+ topSort = reversePostOrder . dff
674+ where
675+ reversePostOrder :: [Tree a ] -> [a ]
676+ reversePostOrder =
677+ F. foldl' (\ vs t -> F. foldl' (flip (:) ) vs (Tree. PostOrder t)) []
691678
692679-- | \(O(V+E)\). Reverse ordering of `topSort`.
693680--
694681-- See note in 'topSort'.
695682--
696683-- @since 0.6.4
697684reverseTopSort :: Graph -> [Vertex ]
698- reverseTopSort = postOrd
685+ reverseTopSort = concatMap ( F. toList . Tree. PostOrder ) . dff
699686
700687------------------------------------------------------------
701688-- Algorithm 3: connected components
@@ -722,7 +709,7 @@ undirected g = buildG (bounds g) (edges g ++ reverseE g)
722709-- > ,Node {rootLabel = 3, subForest = []}]
723710
724711scc :: Graph -> [Tree Vertex ]
725- scc g = dfs g (reverse (postOrd ( transposeG g) ))
712+ scc g = dfs g (topSort ( transposeG g))
726713
727714------------------------------------------------------------
728715-- Algorithm 5: Classifying edges
@@ -764,7 +751,7 @@ mapT f t = array (bounds t) [ (,) v (f v (t!v)) | v <- indices t ]
764751--
765752-- > reachable (buildG (0,2) [(0,1), (1,2)]) 0 == [0,1,2]
766753reachable :: Graph -> Vertex -> [Vertex ]
767- reachable g v = preorderF (dfs g [v])
754+ reachable g v = concatMap Tree. flatten (dfs g [v])
768755
769756-- | \(O(V+E)\). Returns @True@ if the second vertex reachable from the first.
770757--
0 commit comments