Ticket #7671: trac_7671.patch

File trac_7671.patch, 6.0 KB (added by ncohen, 11 years ago)
  • sage/graphs/base/c_graph.pyx

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1266873679 -3600
    # Node ID 96b75706cba395e03bdcf8c97229f9c3b36af290
    # Parent  efdf887eeb279c193a08fd5252953cc691f385cf
    trac 7671 : strongly_connected_components in c_graphs
    
    diff -r efdf887eeb27 -r 96b75706cba3 sage/graphs/base/c_graph.pyx
    a b  
    14971497        return (<CGraph>self._cg).num_verts == len(list(self.depth_first_search(v))) and \
    14981498            (<CGraph>self._cg).num_verts == len(list(self.depth_first_search(v, reverse=True)))
    14991499
     1500    def strongly_connected_component_containing_vertex(self, v):
     1501        r"""
     1502        Returns the strongly connected component containing the given vertex
     1503
     1504        INPUT:
     1505
     1506        - ``v`` -- a vertex
     1507
     1508        EXAMPLE:
     1509
     1510        The digraph obtained from the PetersenGraph has an unique
     1511        strongly connected component ::
     1512
     1513            sage: g = DiGraph(graphs.PetersenGraph())
     1514            sage: g.strongly_connected_component_containing_vertex(0)
     1515            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
     1516
     1517        In the Butterfly DiGraph, each vertex is a strongly connected
     1518        component::
     1519
     1520            sage: g = digraphs.ButterflyGraph(3)
     1521            sage: all([[v] == g.strongly_connected_component_containing_vertex(v) for v in g])
     1522            True
     1523        """
     1524        cdef int v_int = get_vertex(v, self.vertex_ints, self.vertex_labels, self._cg)
     1525        cdef set a = set(self.depth_first_search(v))
     1526        cdef set b = set(self.depth_first_search(v, reverse=True))
     1527        return list(a & b)
     1528
     1529
    15001530
    15011531cdef class Search_iterator:
    15021532    cdef graph
  • sage/graphs/digraph.py

    diff -r efdf887eeb27 -r 96b75706cba3 sage/graphs/digraph.py
    a b  
    14381438            [[0, 1, 2, 3], [4, 5, 6]]
    14391439            sage: D = DiGraph( { 0 : [1, 3], 1 : [2], 2 : [3], 4 : [5, 6], 5 : [6] } )
    14401440            sage: D.strongly_connected_components()
    1441             [[3], [2], [1], [0], [6], [5], [4]]
     1441            [[0], [1], [2], [3], [4], [5], [6]]
    14421442            sage: D.add_edge([2,0])
    14431443            sage: D.strongly_connected_components()
    1444             [[0, 1, 2], [3], [6], [5], [4]]
     1444            [[0, 1, 2], [3], [4], [5], [6]]
     1445        """
     1446
     1447        try:
     1448            vertices = set(self.vertices())
     1449            scc = []
     1450            while vertices:
     1451                tmp = self.strongly_connected_component_containing_vertex(vertices.__iter__().next())
     1452                vertices.difference_update(set(tmp))
     1453                scc.append(tmp)
     1454            return scc
     1455
     1456        except ValueError:
     1457            import networkx
     1458            return networkx.strongly_connected_components(self.networkx_graph(copy=False))
     1459
     1460
     1461    def strongly_connected_component_containing_vertex(self, v):
     1462        """
     1463        Returns the set of vertices whose strongly connected component is the same as v's.
     1464
     1465        INPUT:
     1466
     1467        - ``v`` -- a vertex
     1468
     1469        EXAMPLE:
     1470
     1471        In the symmetric digraph of a graph, the strongly connected components are the connected
     1472        components::
     1473
     1474            sage: g = graphs.PetersenGraph()
     1475            sage: d = DiGraph(g)
     1476            sage: d.strongly_connected_component_containing_vertex(0)
     1477            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
     1478        """
     1479
     1480        if self.order()==1:
     1481            return [v]
     1482       
     1483        try:
     1484            return self._backend.strongly_connected_component_containing_vertex(v)
     1485
     1486        except AttributeError:
     1487            raise ValueError("This function is only defined for C graphs.")
     1488
     1489    def strongly_connected_components_subgraphs(self):
     1490        r"""
     1491        Returns the strongly connected components as a list of subgraphs.
     1492
     1493        EXAMPLE:
     1494
     1495        In the symmetric digraph of a graph, the strongly connected components are the connected
     1496        components::
     1497
     1498            sage: g = graphs.PetersenGraph()
     1499            sage: d = DiGraph(g)
     1500            sage: d.strongly_connected_components_subgraphs()
     1501            [Subgraph of (Petersen graph): Digraph on 10 vertices]
    14451502
    14461503        """
    1447         import networkx
    1448         return networkx.strongly_connected_components(self.networkx_graph(copy=False))
     1504        return map(self.subgraph, self.strongly_connected_components())
     1505
     1506
     1507    def strongly_connected_components_digraph(self):
     1508        r"""
     1509        Returns the digraph of the strongly connected components
     1510
     1511        The digraph of the strongly connected components of a graph `G` has
     1512        a vertex per strongly connected component included in `G`. There
     1513        is an edge from a component `C_1` to a component `C_2` if there is
     1514        an edge from one to the other in `G`.
     1515
     1516        EXAMPLE:
     1517
     1518        Such a digraph is always acyclic ::
     1519
     1520            sage: g = digraphs.RandomDirectedGNP(15,.1)
     1521            sage: scc_digraph = g.strongly_connected_components_digraph()
     1522            sage: scc_digraph.is_directed_acyclic()
     1523            True
     1524
     1525        The vertices of the digraph of strongly connected components are
     1526        exactly the strongly connected components::
     1527
     1528            sage: g = digraphs.ButterflyGraph(2)
     1529            sage: scc_digraph = g.strongly_connected_components_digraph()
     1530            sage: g.is_directed_acyclic()
     1531            True
     1532            sage: all([ Set(scc) in scc_digraph.vertices() for scc in g.strongly_connected_components()])
     1533            True
     1534        """
     1535
     1536        from sage.sets.set import Set
     1537
     1538        scc = self.strongly_connected_components()
     1539        scc_set = map(Set,scc)
     1540
     1541        d = {}
     1542        for i,c in enumerate(scc):
     1543            for v in c:
     1544                d[v] = i
     1545
     1546        g = DiGraph()
     1547        g.add_vertices(scc_set)
     1548        g.allow_multiple_edges(False)
     1549       
     1550        g.add_edges( (scc_set[d[u]], scc_set[d[u]]) for u,v in self.edges(labels=False) )
     1551
     1552        return g
     1553           
     1554
    14491555
    14501556    def is_strongly_connected(self):
    14511557        r"""