# HG changeset patch
# User Nicolas M. Thiery
# Date 1301163722 3600
# Node ID 95da6f51feb80c17918e4f64a75da2ea2fa86cbd
# Parent 2451f6925fa71f5f34208240903705e9ff39461c
#10874: Add support for keep_labels in Digraph.strongly_connected_components_digraph
diff git a/sage/graphs/digraph.py b/sage/graphs/digraph.py
 a/sage/graphs/digraph.py
+++ b/sage/graphs/digraph.py
@@ 2601,11 +2601,15 @@ class DiGraph(GenericGraph):
return map(self.subgraph, self.strongly_connected_components())
 def strongly_connected_components_digraph(self):
+ def strongly_connected_components_digraph(self, keep_labels = False):
r"""
Returns the digraph of the strongly connected components
 The digraph of the strongly connected components of a graph `G` has
+ INPUT:
+
+  ``keep_labels``  boolean (default: False)
+
+ The digraph of the strongly connected components of a graph `G` has
a vertex per strongly connected component included in `G`. There
is an edge from a component `C_1` to a component `C_2` if there is
an edge from one to the other in `G`.
@@ 2632,12 +2636,30 @@ class DiGraph(GenericGraph):
The following digraph has three strongly connected components,
and the digraph of those is a chain::
 sage: g = DiGraph({0:[1,2,3],1:[2],2:[1,3]})
+ sage: g = DiGraph({0:{1:"01", 2: "02", 3: 03}, 1: {2: "12"}, 2:{1: "21", 3: "23"}})
sage: scc_digraph = g.strongly_connected_components_digraph()
sage: scc_digraph.vertices()
[{0}, {3}, {1, 2}]
sage: scc_digraph.edges()
[({0}, {3}, None), ({0}, {1, 2}, None), ({1, 2}, {3}, None)]
+
+ By default, the labels are discarded, and the result has no
+ loops nor multiple edges. If ``keep_labels`` is ``True``, then
+ the labels are kept, and the result is a multi digraph,
+ possibly with multiple edges and loops. However, edges in the
+ result with same source, target, and label are not duplicated
+ (see the edges from 0 to the strongly connected component
+ `\{1,2\}` below)::
+
+ sage: g = DiGraph({0:{1:"012", 2: "012", 3: "03"}, 1: {2: "12", 3: "13"}, 2:{1: "21", 3: "23"}})
+ sage: scc_digraph = g.strongly_connected_components_digraph(keep_labels = True)
+ sage: scc_digraph.vertices()
+ [{0}, {3}, {1, 2}]
+ sage: scc_digraph.edges()
+ [({0}, {3}, '03'), ({0}, {1, 2}, '012'),
+ ({1, 2}, {3}, '13'), ({1, 2}, {3}, '23'),
+ ({1, 2}, {1, 2}, '12'), ({1, 2}, {1, 2}, '21')]
+
"""
from sage.sets.set import Set
@@ 2650,14 +2672,17 @@ class DiGraph(GenericGraph):
for v in c:
d[v] = i
 g = DiGraph()
 g.add_vertices(scc_set)
 g.allow_multiple_edges(False)

 g.add_edges( (scc_set[d[u]], scc_set[d[v]]) for u,v in self.edges(labels=False) )
+ if keep_labels:
+ g = DiGraph(multiedges=True, loops=True)
+ g.add_vertices(scc_set)
+ g.add_edges( set((scc_set[d[u]], scc_set[d[v]], label) for (u,v,label) in self.edges() ) )
+ else:
+ g = DiGraph(multiple_edges=False, loops=False)
+ g.add_vertices(scc_set)
+ g.add_edges( (scc_set[d[u]], scc_set[d[v]]) for u,v in self.edges(labels=False) )
+
return g

def is_strongly_connected(self):
r"""
Returns whether the current ``DiGraph`` is strongly connected.