Ticket #10874: trac_10874-graph-strongly_connected_componnents-nt.patch

File trac_10874-graph-strongly_connected_componnents-nt.patch, 3.5 KB (added by nthiery, 10 years ago)

Apply only this patch

  • sage/graphs/digraph.py

    # HG changeset patch
    # User Nicolas M. Thiery <nthiery@users.sf.net>
    # 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 b class DiGraph(GenericGraph): 
    26012601        return map(self.subgraph, self.strongly_connected_components())
    26022602
    26032603
    2604     def strongly_connected_components_digraph(self):
     2604    def strongly_connected_components_digraph(self, keep_labels = False):
    26052605        r"""
    26062606        Returns the digraph of the strongly connected components
    26072607
    2608         The digraph of the strongly connected components of a graph `G` has
     2608        INPUT:
     2609
     2610         - ``keep_labels`` -- boolean (default: False)
     2611
     2612        The digraph of the strongly connected components of a graph `G` has
    26092613        a vertex per strongly connected component included in `G`. There
    26102614        is an edge from a component `C_1` to a component `C_2` if there is
    26112615        an edge from one to the other in `G`.
    class DiGraph(GenericGraph): 
    26322636        The following digraph has three strongly connected components,
    26332637        and the digraph of those is a chain::
    26342638
    2635             sage: g = DiGraph({0:[1,2,3],1:[2],2:[1,3]})
     2639            sage: g = DiGraph({0:{1:"01", 2: "02", 3: 03}, 1: {2: "12"}, 2:{1: "21", 3: "23"}})
    26362640            sage: scc_digraph = g.strongly_connected_components_digraph()
    26372641            sage: scc_digraph.vertices()
    26382642            [{0}, {3}, {1, 2}]
    26392643            sage: scc_digraph.edges()
    26402644            [({0}, {3}, None), ({0}, {1, 2}, None), ({1, 2}, {3}, None)]
     2645
     2646        By default, the labels are discarded, and the result has no
     2647        loops nor multiple edges. If ``keep_labels`` is ``True``, then
     2648        the labels are kept, and the result is a multi digraph,
     2649        possibly with multiple edges and loops. However, edges in the
     2650        result with same source, target, and label are not duplicated
     2651        (see the edges from 0 to the strongly connected component
     2652        `\{1,2\}` below)::
     2653
     2654            sage: g = DiGraph({0:{1:"0-12", 2: "0-12", 3: "0-3"}, 1: {2: "1-2", 3: "1-3"}, 2:{1: "2-1", 3: "2-3"}})
     2655            sage: scc_digraph = g.strongly_connected_components_digraph(keep_labels = True)
     2656            sage: scc_digraph.vertices()
     2657            [{0}, {3}, {1, 2}]
     2658            sage: scc_digraph.edges()
     2659            [({0}, {3}, '0-3'), ({0}, {1, 2}, '0-12'),
     2660             ({1, 2}, {3}, '1-3'), ({1, 2}, {3}, '2-3'),
     2661             ({1, 2}, {1, 2}, '1-2'), ({1, 2}, {1, 2}, '2-1')]
     2662
    26412663        """
    26422664
    26432665        from sage.sets.set import Set
    class DiGraph(GenericGraph): 
    26502672            for v in c:
    26512673                d[v] = i
    26522674
    2653         g = DiGraph()
    2654         g.add_vertices(scc_set)
    2655         g.allow_multiple_edges(False)
    2656 
    2657         g.add_edges( (scc_set[d[u]], scc_set[d[v]]) for u,v in self.edges(labels=False) )
     2675        if keep_labels:
     2676            g = DiGraph(multiedges=True, loops=True)
     2677            g.add_vertices(scc_set)
     2678            g.add_edges( set((scc_set[d[u]], scc_set[d[v]], label) for (u,v,label) in self.edges() ) )
     2679        else:
     2680            g = DiGraph(multiple_edges=False, loops=False)
     2681            g.add_vertices(scc_set)
     2682            g.add_edges( (scc_set[d[u]], scc_set[d[v]]) for u,v in self.edges(labels=False) )
     2683
    26582684        return g
    26592685
    2660 
    26612686    def is_strongly_connected(self):
    26622687        r"""
    26632688        Returns whether the current ``DiGraph`` is strongly connected.