Ticket #7569: trac_7569.patch

File trac_7569.patch, 2.1 KB (added by ncohen, 11 years ago)
  • sage/graphs/generic_graph.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1266920661 -3600
    # Node ID 3e4ea9e6bb4ddec6ee656c8ecc85b4ccc1014053
    # Parent  27095e45c83130db3fe4703f4d7829dfd0165051
    trac 7569 : random_vertex and random_edge functions
    
    diff -r 27095e45c831 -r 3e4ea9e6bb4d sage/graphs/generic_graph.py
    a b  
    45794579
    45804580    __contains__ = has_vertex
    45814581
     4582    def random_vertex(self,**kwds):
     4583        r"""
     4584        Returns a random vertex.
     4585
     4586        INPUT:
     4587
     4588        - ``**kwds`` -- arguments to be passed down to the
     4589          ``vertex_iterator`` method.
     4590
     4591        EXAMPLE:
     4592
     4593        The returned value belong to the set of vertices::
     4594
     4595            sage: g = graphs.PetersenGraph()
     4596            sage: v = g.random_vertex()
     4597            sage: v in g
     4598            True
     4599        """
     4600
     4601        from sage.misc.prandom import randint
     4602        it = self.vertex_iterator(**kwds)
     4603        for i in xrange(0, randint(0,self.order()-1)):
     4604            it.next()
     4605
     4606        return it.next()
     4607
     4608    def random_edge(self,**kwds):
     4609        r"""
     4610        Returns a random edge.
     4611
     4612        INPUT:
     4613
     4614        - ``**kwds`` -- arguments to be passed down to the
     4615          ``edge_iterator`` method.
     4616
     4617        EXAMPLE:
     4618
     4619        The returned value belong to the set of edges::
     4620
     4621            sage: g = graphs.PetersenGraph()
     4622            sage: u,v = g.random_edge(labels=False)
     4623            sage: g.has_edge(u,v)
     4624            True
     4625
     4626        As the ``edges()`` method would, this function returns
     4627        by default a triple ``(u,v,l)`` of values, in which
     4628        ``l`` is the label of edge `u,v` ::
     4629
     4630            sage: g.random_edge()
     4631            (...,...,...)
     4632        """
     4633
     4634        from sage.misc.prandom import randint
     4635        it = self.edge_iterator(**kwds)
     4636        for i in xrange(0, randint(0,self.size()-1)):
     4637            it.next()
     4638
     4639        return it.next()                           
     4640
    45824641    def vertex_boundary(self, vertices1, vertices2=None):
    45834642        """
    45844643        Returns a list of all vertices in the external boundary of