4 -- At present the only one with a "nice" external interface
5 stronglyConnComp, stronglyConnCompR, SCC(..), flattenSCC, flattenSCCs,
8 graphFromEdges, graphFromEdges',
9 buildG, transposeG, reverseE, outdegree, indegree,
24 # include "HsVersions.h"
26 ------------------------------------------------------------------------------
27 -- A version of the graph algorithms described in:
29 -- ``Lazy Depth-First Search and Linear Graph Algorithms in Haskell''
30 -- by David King and John Launchbury
32 -- Also included is some additional code for printing tree structures ...
33 ------------------------------------------------------------------------------
36 import Util ( sortLe )
47 #if __GLASGOW_HASKELL__ > 604
49 #elif __GLASGOW_HASKELL__ >= 504
50 import Data.Array.ST hiding ( indices, bounds )
57 %************************************************************************
61 %************************************************************************
64 data SCC vertex = AcyclicSCC vertex
67 flattenSCCs :: [SCC a] -> [a]
68 flattenSCCs = concatMap flattenSCC
70 flattenSCC (AcyclicSCC v) = [v]
71 flattenSCC (CyclicSCC vs) = vs
73 instance Outputable a => Outputable (SCC a) where
74 ppr (AcyclicSCC v) = text "NONREC" $$ (nest 3 (ppr v))
75 ppr (CyclicSCC vs) = text "REC" $$ (nest 3 (vcat (map ppr vs)))
81 => [(node, key, [key])] -- The graph; its ok for the
82 -- out-list to contain keys which arent
83 -- a vertex key, they are ignored
84 -> [SCC node] -- Returned in topologically sorted order
85 -- Later components depend on earlier ones, but not vice versa
87 stronglyConnComp edges
88 = map get_node (stronglyConnCompR edges)
90 get_node (AcyclicSCC (n, _, _)) = AcyclicSCC n
91 get_node (CyclicSCC triples) = CyclicSCC [n | (n,_,_) <- triples]
93 -- The "R" interface is used when you expect to apply SCC to
94 -- the (some of) the result of SCC, so you dont want to lose the dependency info
97 => [(node, key, [key])] -- The graph; its ok for the
98 -- out-list to contain keys which arent
99 -- a vertex key, they are ignored
100 -> [SCC (node, key, [key])] -- Topologically sorted
102 stronglyConnCompR [] = [] -- added to avoid creating empty array in graphFromEdges -- SOF
103 stronglyConnCompR edges
106 (graph, vertex_fn) = _scc_ "graphFromEdges" graphFromEdges edges
107 forest = _scc_ "Digraph.scc" scc graph
108 decode (Node v []) | mentions_itself v = CyclicSCC [vertex_fn v]
109 | otherwise = AcyclicSCC (vertex_fn v)
110 decode other = CyclicSCC (dec other [])
112 dec (Node v ts) vs = vertex_fn v : foldr dec vs ts
113 mentions_itself v = v `elem` (graph ! v)
116 %************************************************************************
120 %************************************************************************
125 type Table a = Array Vertex a
126 type Graph = Table [Vertex]
127 type Bounds = (Vertex, Vertex)
128 type Edge = (Vertex, Vertex)
132 vertices :: Graph -> [Vertex]
135 edges :: Graph -> [Edge]
136 edges g = [ (v, w) | v <- vertices g, w <- g!v ]
138 mapT :: (Vertex -> a -> b) -> Table a -> Table b
139 mapT f t = array (bounds t) [ (,) v (f v (t!v)) | v <- indices t ]
141 buildG :: Bounds -> [Edge] -> Graph
142 buildG bounds edges = accumArray (flip (:)) [] bounds edges
144 transposeG :: Graph -> Graph
145 transposeG g = buildG (bounds g) (reverseE g)
147 reverseE :: Graph -> [Edge]
148 reverseE g = [ (w, v) | (v, w) <- edges g ]
150 outdegree :: Graph -> Table Int
151 outdegree = mapT numEdges
152 where numEdges v ws = length ws
154 indegree :: Graph -> Table Int
155 indegree = outdegree . transposeG
162 => [(node, key, [key])]
163 -> (Graph, Vertex -> (node, key, [key]))
164 graphFromEdges edges =
165 case graphFromEdges' edges of (graph, vertex_fn, _) -> (graph, vertex_fn)
169 => [(node, key, [key])]
170 -> (Graph, Vertex -> (node, key, [key]), key -> Maybe Vertex)
171 graphFromEdges' edges
172 = (graph, \v -> vertex_map ! v, key_vertex)
174 max_v = length edges - 1
175 bounds = (0,max_v) :: (Vertex, Vertex)
177 (_,k1,_) `le` (_,k2,_) = case k1 `compare` k2 of { GT -> False; other -> True }
180 edges1 = zipWith (,) [0..] sorted_edges
182 graph = array bounds [(,) v (mapMaybe key_vertex ks) | (,) v (_, _, ks) <- edges1]
183 key_map = array bounds [(,) v k | (,) v (_, k, _ ) <- edges1]
184 vertex_map = array bounds edges1
187 -- key_vertex :: key -> Maybe Vertex
188 -- returns Nothing for non-interesting vertices
189 key_vertex k = find 0 max_v
193 find a b = case compare k (key_map ! mid) of
198 mid = (a + b) `div` 2
201 %************************************************************************
205 %************************************************************************
208 data Tree a = Node a (Forest a)
209 type Forest a = [Tree a]
211 mapTree :: (a -> b) -> (Tree a -> Tree b)
212 mapTree f (Node x ts) = Node (f x) (map (mapTree f) ts)
216 instance Show a => Show (Tree a) where
217 showsPrec p t s = showTree t ++ s
219 showTree :: Show a => Tree a -> String
220 showTree = drawTree . mapTree show
222 showForest :: Show a => Forest a -> String
223 showForest = unlines . map showTree
225 drawTree :: Tree String -> String
226 drawTree = unlines . draw
228 draw (Node x ts) = grp this (space (length this)) (stLoop ts)
229 where this = s1 ++ x ++ " "
231 space n = replicate n ' '
234 stLoop [t] = grp s2 " " (draw t)
235 stLoop (t:ts) = grp s3 s4 (draw t) ++ [s4] ++ rsLoop ts
237 rsLoop [t] = grp s5 " " (draw t)
238 rsLoop (t:ts) = grp s6 s4 (draw t) ++ [s4] ++ rsLoop ts
240 grp fst rst = zipWith (++) (fst:repeat rst)
242 [s1,s2,s3,s4,s5,s6] = ["- ", "--", "-+", " |", " `", " +"]
246 %************************************************************************
248 %* Depth first search
250 %************************************************************************
253 #if __GLASGOW_HASKELL__ >= 504
254 newSTArray :: Ix i => (i,i) -> e -> ST s (STArray s i e)
255 newSTArray = newArray
257 readSTArray :: Ix i => STArray s i e -> i -> ST s e
258 readSTArray = readArray
260 writeSTArray :: Ix i => STArray s i e -> i -> e -> ST s ()
261 writeSTArray = writeArray
264 type Set s = STArray s Vertex Bool
266 mkEmpty :: Bounds -> ST s (Set s)
267 mkEmpty bnds = newSTArray bnds False
269 contains :: Set s -> Vertex -> ST s Bool
270 contains m v = readSTArray m v
272 include :: Set s -> Vertex -> ST s ()
273 include m v = writeSTArray m v True
277 dff :: Graph -> Forest Vertex
278 dff g = dfs g (vertices g)
280 dfs :: Graph -> [Vertex] -> Forest Vertex
281 dfs g vs = prune (bounds g) (map (generate g) vs)
283 generate :: Graph -> Vertex -> Tree Vertex
284 generate g v = Node v (map (generate g) (g!v))
286 prune :: Bounds -> Forest Vertex -> Forest Vertex
287 prune bnds ts = runST (mkEmpty bnds >>= \m ->
290 chop :: Set s -> Forest Vertex -> ST s (Forest Vertex)
291 chop m [] = return []
292 chop m (Node v ts : us)
293 = contains m v >>= \visited ->
297 include m v >>= \_ ->
300 return (Node v as : bs)
304 %************************************************************************
308 %************************************************************************
310 ------------------------------------------------------------
311 -- Algorithm 1: depth first search numbering
312 ------------------------------------------------------------
315 --preorder :: Tree a -> [a]
316 preorder (Node a ts) = a : preorderF ts
318 preorderF :: Forest a -> [a]
319 preorderF ts = concat (map preorder ts)
321 tabulate :: Bounds -> [Vertex] -> Table Int
322 tabulate bnds vs = array bnds (zipWith (,) vs [1..])
324 preArr :: Bounds -> Forest Vertex -> Table Int
325 preArr bnds = tabulate bnds . preorderF
329 ------------------------------------------------------------
330 -- Algorithm 2: topological sorting
331 ------------------------------------------------------------
334 --postorder :: Tree a -> [a]
335 postorder (Node a ts) = postorderF ts ++ [a]
337 postorderF :: Forest a -> [a]
338 postorderF ts = concat (map postorder ts)
340 postOrd :: Graph -> [Vertex]
341 postOrd = postorderF . dff
343 topSort :: Graph -> [Vertex]
344 topSort = reverse . postOrd
348 ------------------------------------------------------------
349 -- Algorithm 3: connected components
350 ------------------------------------------------------------
353 components :: Graph -> Forest Vertex
354 components = dff . undirected
356 undirected :: Graph -> Graph
357 undirected g = buildG (bounds g) (edges g ++ reverseE g)
361 -- Algorithm 4: strongly connected components
364 scc :: Graph -> Forest Vertex
365 scc g = dfs g (reverse (postOrd (transposeG g)))
369 ------------------------------------------------------------
370 -- Algorithm 5: Classifying edges
371 ------------------------------------------------------------
374 back :: Graph -> Table Int -> Graph
375 back g post = mapT select g
376 where select v ws = [ w | w <- ws, post!v < post!w ]
378 cross :: Graph -> Table Int -> Table Int -> Graph
379 cross g pre post = mapT select g
380 where select v ws = [ w | w <- ws, post!v > post!w, pre!v > pre!w ]
382 forward :: Graph -> Graph -> Table Int -> Graph
383 forward g tree pre = mapT select g
384 where select v ws = [ w | w <- ws, pre!v < pre!w ] \\ tree!v
388 ------------------------------------------------------------
389 -- Algorithm 6: Finding reachable vertices
390 ------------------------------------------------------------
393 reachable :: Graph -> Vertex -> [Vertex]
394 reachable g v = preorderF (dfs g [v])
396 path :: Graph -> Vertex -> Vertex -> Bool
397 path g v w = w `elem` (reachable g v)
401 ------------------------------------------------------------
402 -- Algorithm 7: Biconnected components
403 ------------------------------------------------------------
406 bcc :: Graph -> Forest [Vertex]
407 bcc g = (concat . map bicomps . map (do_label g dnum)) forest
409 dnum = preArr (bounds g) forest
411 do_label :: Graph -> Table Int -> Tree Vertex -> Tree (Vertex,Int,Int)
412 do_label g dnum (Node v ts) = Node (v,dnum!v,lv) us
413 where us = map (do_label g dnum) ts
414 lv = minimum ([dnum!v] ++ [dnum!w | w <- g!v]
415 ++ [lu | Node (u,du,lu) xs <- us])
417 bicomps :: Tree (Vertex,Int,Int) -> Forest [Vertex]
418 bicomps (Node (v,dv,lv) ts)
419 = [ Node (v:vs) us | (l,Node vs us) <- map collect ts]
421 collect :: Tree (Vertex,Int,Int) -> (Int, Tree [Vertex])
422 collect (Node (v,dv,lv) ts) = (lv, Node (v:vs) cs)
423 where collected = map collect ts
424 vs = concat [ ws | (lw, Node ws us) <- collected, lw<dv]
425 cs = concat [ if lw<dv then us else [Node (v:ws) us]
426 | (lw, Node ws us) <- collected ]