Skip to content

Commit 003b7a6

Browse files
jwaldmanntreeowl
authored andcommitted
Data.Graph.stronglyConnComp: document ordering (#562)
1 parent 1b17e57 commit 003b7a6

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

Data/Graph.hs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
-- The implementation is based on
3737
--
3838
-- * /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>
4040
--
4141
-----------------------------------------------------------------------------
4242

@@ -217,8 +217,13 @@ flattenSCC :: SCC vertex -> [vertex]
217217
flattenSCC (AcyclicSCC v) = [v]
218218
flattenSCC (CyclicSCC vs) = vs
219219

220-
-- | The strongly connected components of a directed graph, topologically
220+
-- | The strongly connected components of a directed graph, reverse topologically
221221
-- 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"]
222227
stronglyConnComp
223228
:: Ord key
224229
=> [(node, key, [key])]
@@ -234,20 +239,25 @@ stronglyConnComp edges0
234239
get_node (AcyclicSCC (n, _, _)) = AcyclicSCC n
235240
get_node (CyclicSCC triples) = CyclicSCC [n | (n,_,_) <- triples]
236241

237-
-- | The strongly connected components of a directed graph, topologically
242+
-- | The strongly connected components of a directed graph, reverse topologically
238243
-- sorted. The function is the same as 'stronglyConnComp', except that
239244
-- all the information about each node retained.
240245
-- This interface is used when you expect to apply 'SCC' to
241246
-- (some of) the result of 'SCC', so you don't want to lose the
242247
-- 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])]
243253
stronglyConnCompR
244254
:: Ord key
245255
=> [(node, key, [key])]
246256
-- ^ The graph: a list of nodes uniquely identified by keys,
247257
-- with a list of keys of nodes this node has edges to.
248258
-- The out-list may contain keys that don't correspond to
249259
-- nodes of the graph; such edges are ignored.
250-
-> [SCC (node, key, [key])] -- ^ Topologically sorted
260+
-> [SCC (node, key, [key])] -- ^ Reverse topologically sorted
251261

252262
stronglyConnCompR [] = [] -- added to avoid creating empty array in graphFromEdges -- SOF
253263
stronglyConnCompR edges0
@@ -620,7 +630,14 @@ undirected g = buildG (bounds g) (edges g ++ reverseE g)
620630

621631
-- Algorithm 4: strongly connected components
622632

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+
624641
scc :: Graph -> Forest Vertex
625642
scc g = dfs g (reverse (postOrd (transposeG g)))
626643

0 commit comments

Comments
 (0)