36
36
-- The implementation is based on
37
37
--
38
38
-- * /Structuring Depth-First Search Algorithms in Haskell/,
39
- -- by David King and John Launchbury.
39
+ -- by David King and John Launchbury, <http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.52.6526>
40
40
--
41
41
-----------------------------------------------------------------------------
42
42
@@ -217,8 +217,13 @@ flattenSCC :: SCC vertex -> [vertex]
217
217
flattenSCC (AcyclicSCC v) = [v]
218
218
flattenSCC (CyclicSCC vs) = vs
219
219
220
- -- | The strongly connected components of a directed graph, topologically
220
+ -- | The strongly connected components of a directed graph, reverse topologically
221
221
-- sorted.
222
+ --
223
+ -- ==== __Examples__
224
+ --
225
+ -- > stronglyConnComp [("a",0,[1]),("b",1,[2,3]),("c",2,[1]),("d",3,[3])]
226
+ -- > == [CyclicSCC ["d"],CyclicSCC ["b","c"],AcyclicSCC "a"]
222
227
stronglyConnComp
223
228
:: Ord key
224
229
=> [(node , key , [key ])]
@@ -234,20 +239,25 @@ stronglyConnComp edges0
234
239
get_node (AcyclicSCC (n, _, _)) = AcyclicSCC n
235
240
get_node (CyclicSCC triples) = CyclicSCC [n | (n,_,_) <- triples]
236
241
237
- -- | The strongly connected components of a directed graph, topologically
242
+ -- | The strongly connected components of a directed graph, reverse topologically
238
243
-- sorted. The function is the same as 'stronglyConnComp', except that
239
244
-- all the information about each node retained.
240
245
-- This interface is used when you expect to apply 'SCC' to
241
246
-- (some of) the result of 'SCC', so you don't want to lose the
242
247
-- dependency information.
248
+ --
249
+ -- ==== __Examples__
250
+ --
251
+ -- > stronglyConnCompR [("a",0,[1]),("b",1,[2,3]),("c",2,[1]),("d",3,[3])]
252
+ -- > == [CyclicSCC [("d",3,[3])],CyclicSCC [("b",1,[2,3]),("c",2,[1])],AcyclicSCC ("a",0,[1])]
243
253
stronglyConnCompR
244
254
:: Ord key
245
255
=> [(node , key , [key ])]
246
256
-- ^ The graph: a list of nodes uniquely identified by keys,
247
257
-- with a list of keys of nodes this node has edges to.
248
258
-- The out-list may contain keys that don't correspond to
249
259
-- nodes of the graph; such edges are ignored.
250
- -> [SCC (node , key , [key ])] -- ^ Topologically sorted
260
+ -> [SCC (node , key , [key ])] -- ^ Reverse topologically sorted
251
261
252
262
stronglyConnCompR [] = [] -- added to avoid creating empty array in graphFromEdges -- SOF
253
263
stronglyConnCompR edges0
@@ -620,7 +630,14 @@ undirected g = buildG (bounds g) (edges g ++ reverseE g)
620
630
621
631
-- Algorithm 4: strongly connected components
622
632
623
- -- | The strongly connected components of a graph.
633
+ -- | The strongly connected components of a graph, in reverse topological order.
634
+ --
635
+ -- ==== __Examples__
636
+ --
637
+ -- > scc (buildG (0,3) [(3,1),(1,2),(2,0),(0,1)])
638
+ -- > == [Node {rootLabel = 0, subForest = [Node {rootLabel = 1, subForest = [Node {rootLabel = 2, subForest = []}]}]}
639
+ -- > ,Node {rootLabel = 3, subForest = []}]
640
+
624
641
scc :: Graph -> Forest Vertex
625
642
scc g = dfs g (reverse (postOrd (transposeG g)))
626
643
0 commit comments