Ticket #12244: trac_12244.patch

File trac_12244.patch, 3.9 KB (added by ncohen, 9 years ago)
  • sage/graphs/distances_all_pairs.pyx

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1325413839 -3600
    # Node ID 93613ef6006868094781f093184cf18ae2440712
    # Parent  482fd1fcc962ac3de720e652d1da42c137c2a066
    trac #12244 -- Empty graphs and new distance computations
    
    diff -r 482fd1fcc962 -r 93613ef60068 sage/graphs/distances_all_pairs.pyx
    a b  
    276276# Predecessors #
    277277################
    278278
    279 cdef unsigned short * c_shortest_path_all_pairs(G):
     279cdef unsigned short * c_shortest_path_all_pairs(G) except NULL:
    280280    r"""
    281281    Returns the matrix of predecessors in G.
    282282
     
    327327    """
    328328
    329329    cdef int n = G.order()
     330   
     331    if n == 0:
     332        return {}
    330333
    331334    cdef unsigned short * predecessors = c_shortest_path_all_pairs(G)
    332335    cdef unsigned short * c_predecessors = predecessors
     
    404407    from sage.rings.infinity import Infinity
    405408
    406409    cdef int n = G.order()
     410
     411    if n == 0:
     412        return {}
     413
    407414    cdef unsigned short * distances = c_distances_all_pairs(G)
    408415    cdef unsigned short * c_distances = distances
    409416
     
    481488
    482489    from sage.rings.infinity import Infinity
    483490    cdef int n = G.order()
     491   
     492    if n == 0:
     493        return {}, {}
     494
    484495    cdef unsigned short * distances = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short *))
    485496    cdef unsigned short * c_distances = distances
    486497    cdef unsigned short * predecessor = <unsigned short *> sage_malloc(n*n*sizeof(unsigned short *))
     
    527538# Eccentricity #
    528539################
    529540
    530 cdef unsigned short * c_eccentricity(G):
     541cdef unsigned short * c_eccentricity(G) except NULL:
    531542    r"""
    532543    Returns the vector of eccentricities in G.
    533544
     
    559570    """
    560571    from sage.rings.infinity import Infinity
    561572    cdef int n = G.order()
     573
     574    if n == 0:
     575        return []
     576
    562577    cdef unsigned short * ecc = c_eccentricity(G)
    563578
    564579    cdef list l_ecc = []
     
    691706
    692707    cdef list gverts = g.verts()
    693708
     709    if gverts == []:
     710        if distances and paths:
     711            return {}, {}
     712        else:
     713            return {}
     714
    694715    cdef int n = max(gverts) + 1
    695716
    696717    if n >= <unsigned short> -1:
  • sage/graphs/generic_graph.py

    diff -r 482fd1fcc962 -r 93613ef60068 sage/graphs/generic_graph.py
    a b  
    1054310543            2
    1054410544            sage: G.diameter()
    1054510545            2
    10546         """
     10546
     10547        TEST::
     10548
     10549            sage: g = Graph()
     10550            sage: g.radius()
     10551            Traceback (most recent call last):
     10552            ...
     10553            ValueError: This method has no meaning on empty graphs.
     10554        """
     10555        if self.order() == 0:
     10556            raise ValueError("This method has no meaning on empty graphs.")
     10557
    1054710558        return min(self.eccentricity())
    1054810559   
    1054910560    def center(self):
     
    1146711478        the theory of the Wiener number. INDIAN JOURNAL OF CHEMISTRY SECTION A
    1146811479        PUBLICATIONS & INFORMATION DIRECTORATE, CSIR
    1146911480        """
    11470 
    1147111481        return sum([sum(v.itervalues()) for v in self.distance_all_pairs().itervalues()])/2
    1147211482
    1147311483    def average_distance(self):
     
    1149311503        .. [GYLL93] I. Gutman, Y.-N. Yeh, S.-L. Lee, and Y.-L. Luo. Some recent
    1149411504          results in the theory of the Wiener number. *Indian Journal of
    1149511505          Chemistry*, 32A:651--661, 1993.
    11496         """
     11506
     11507        TEST::
     11508
     11509            sage: g = Graph()
     11510            sage: g.average_distance()
     11511            Traceback (most recent call last):
     11512            ...
     11513            ValueError: The graph must have at least two vertices for this value to be defined
     11514        """
     11515        if self.order() < 2:
     11516            raise ValueError("The graph must have at least two vertices for this value to be defined")
    1149711517
    1149811518        return Integer(self.wiener_index())/Integer((self.order()*(self.order()-1))/2)
    1149911519