Ticket #10874: trac_10874graphstrongly_connected_componnentsnt.patch
File trac_10874graphstrongly_connected_componnentsnt.patch, 3.5 KB (added by , 10 years ago) 


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): 2601 2601 return map(self.subgraph, self.strongly_connected_components()) 2602 2602 2603 2603 2604 def strongly_connected_components_digraph(self ):2604 def strongly_connected_components_digraph(self, keep_labels = False): 2605 2605 r""" 2606 2606 Returns the digraph of the strongly connected components 2607 2607 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 2609 2613 a vertex per strongly connected component included in `G`. There 2610 2614 is an edge from a component `C_1` to a component `C_2` if there is 2611 2615 an edge from one to the other in `G`. … … class DiGraph(GenericGraph): 2632 2636 The following digraph has three strongly connected components, 2633 2637 and the digraph of those is a chain:: 2634 2638 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"}}) 2636 2640 sage: scc_digraph = g.strongly_connected_components_digraph() 2637 2641 sage: scc_digraph.vertices() 2638 2642 [{0}, {3}, {1, 2}] 2639 2643 sage: scc_digraph.edges() 2640 2644 [({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:"012", 2: "012", 3: "03"}, 1: {2: "12", 3: "13"}, 2:{1: "21", 3: "23"}}) 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}, '03'), ({0}, {1, 2}, '012'), 2660 ({1, 2}, {3}, '13'), ({1, 2}, {3}, '23'), 2661 ({1, 2}, {1, 2}, '12'), ({1, 2}, {1, 2}, '21')] 2662 2641 2663 """ 2642 2664 2643 2665 from sage.sets.set import Set … … class DiGraph(GenericGraph): 2650 2672 for v in c: 2651 2673 d[v] = i 2652 2674 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 2658 2684 return g 2659 2685 2660 2661 2686 def is_strongly_connected(self): 2662 2687 r""" 2663 2688 Returns whether the current ``DiGraph`` is strongly connected.