X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=Data%2FGraph.hs;h=6627f0476ac4591c16016c15882ab3810943ca23;hb=793cc56fbad9da3087e0b26800aec46687ce82dd;hp=12b3a013f560e5f5f4bb1c2ef4f6ab4ef6a23165;hpb=0b07d2ffaf434e9cc0bc08556f5d86b032eb5ac8;p=haskell-directory.git diff --git a/Data/Graph.hs b/Data/Graph.hs index 12b3a01..6627f04 100644 --- a/Data/Graph.hs +++ b/Data/Graph.hs @@ -6,7 +6,7 @@ -- -- Maintainer : libraries@haskell.org -- Stability : experimental --- Portability : non-portable (requires non-portable module ST) +-- Portability : non-portable (uses Control.Monad.ST) -- -- A version of the graph algorithms described in: -- @@ -28,7 +28,7 @@ module Data.Graph( -- ** Building graphs - graphFromEdges, buildG, transposeG, + graphFromEdges, graphFromEdges', buildG, transposeG, -- reverseE, -- ** Graph properties @@ -43,7 +43,7 @@ module Data.Graph( components, scc, bcc, - -- back, cross, forward, + -- tree, back, cross, forward, reachable, path, module Data.Tree @@ -60,6 +60,10 @@ import Data.Maybe import Data.Array import Data.List +#ifdef __HADDOCK__ +import Prelude +#endif + ------------------------------------------------------------------------- -- - -- External interface @@ -117,7 +121,7 @@ stronglyConnCompR [] = [] -- added to avoid creating empty array in graphFromEd stronglyConnCompR edges0 = map decode forest where - (graph, vertex_fn) = graphFromEdges edges0 + (graph, vertex_fn,_) = graphFromEdges edges0 forest = scc graph decode (Node v []) | mentions_itself v = CyclicSCC [vertex_fn v] | otherwise = AcyclicSCC (vertex_fn v) @@ -175,6 +179,16 @@ outdegree = mapT numEdges indegree :: Graph -> Table Int indegree = outdegree . transposeG +-- | Identical to 'graphFromEdges', except that the return value +-- does not include the function which maps keys to vertices. This +-- version of 'graphFromEdges' is for backwards compatibility. +graphFromEdges' + :: Ord key + => [(node, key, [key])] + -> (Graph, Vertex -> (node, key, [key])) +graphFromEdges' x = (a,b) where + (a,b,_) = graphFromEdges x + -- | Build a graph from a list of nodes uniquely identified by keys, -- with a list of keys of nodes this node should have edges to. -- The out-list may contain keys that don't correspond to @@ -182,9 +196,9 @@ indegree = outdegree . transposeG graphFromEdges :: Ord key => [(node, key, [key])] - -> (Graph, Vertex -> (node, key, [key])) + -> (Graph, Vertex -> (node, key, [key]), key -> Maybe Vertex) graphFromEdges edges0 - = (graph, \v -> vertex_map ! v) + = (graph, \v -> vertex_map ! v, key_vertex) where max_v = length edges0 - 1 bounds0 = (0,max_v) :: (Vertex, Vertex) @@ -321,6 +335,10 @@ scc g = dfs g (reverse (postOrd (transposeG g))) -- Algorithm 5: Classifying edges ------------------------------------------------------------ +tree :: Bounds -> Forest Vertex -> Graph +tree bnds ts = buildG bnds (concat (map flat ts)) + where flat (Node v ts) = [ (v, w) | Node w _us <- ts ] ++ concat (map flat ts) + back :: Graph -> Table Int -> Graph back g post = mapT select g where select v ws = [ w | w <- ws, post!v < post!w ]