Ticket #9741: trac_9741-graph-vertices-sort.patch

File trac_9741-graph-vertices-sort.patch, 4.9 KB (added by rbeezer, 3 years ago)
  • sage/graphs/generic_graph.py

    # HG changeset patch
    # User Rob Beezer <beezer@ups.edu>
    # Date 1281720222 25200
    # Node ID 7202bd2d27e3da887746cb0b56b098ee263a486f
    # Parent  5b338f2e484f2065d3d30d47bc204d6e9ed13d12
    #9741: improve sorting of graph vertices
    
    diff -r 5b338f2e484f -r 7202bd2d27e3 sage/graphs/generic_graph.py
    a b  
    61276127        else: 
    61286128            return iter(set(self._backend.iterator_nbrs(vertex))) 
    61296129 
    6130     def vertices(self, boundary_first=False): 
    6131         """ 
    6132         Return a list of the vertices. 
    6133          
    6134         INPUT: 
    6135          
    6136          
    6137         -  ``boundary_first`` - Return the boundary vertices 
    6138            first. 
    6139          
    6140          
    6141         EXAMPLES:: 
    6142          
     6130    def vertices(self, key=None, boundary_first=False): 
     6131        r""" 
     6132        Return a list of the vertices, usually as a sorted list. 
     6133 
     6134        INPUT: 
     6135 
     6136        - ``key`` - default: ``None`` - a function that takes 
     6137          a vertex as its one argument and returns a value that 
     6138          can be used for comparisons in the sorting algorithm. 
     6139 
     6140        - ``boundary_first`` - default:  ``False`` - if ``True``, 
     6141          return the boundary vertices first. 
     6142 
     6143        OUTPUT: 
     6144 
     6145        The vertices of the list. 
     6146 
     6147        .. warning:: 
     6148 
     6149            There is always an attempt to sort the list before 
     6150            returning the result. However, since any object may 
     6151            be a vertex, there is no guarantee that any two 
     6152            vertices will be comparable.  With default objects 
     6153            for vertices (all integers), or when all the vertices 
     6154            are of the same simple type, then there should not be 
     6155            a problem with how the vertices will be sorted. However, 
     6156            if you need to guarantee a total order for the sort, 
     6157            use the ``key`` argument, as illustrated in the 
     6158            examples below. 
     6159 
     6160        EXAMPLES:: 
     6161 
    61436162            sage: P = graphs.PetersenGraph() 
    61446163            sage: P.vertices() 
    61456164            [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 
    6146          
    6147         Note that the output of the vertices() function is always sorted. 
    6148         This is sub-optimal, speed-wise, but note the following 
    6149         optimizations:: 
    6150          
     6165 
     6166        If you do not care about sorted output and you are 
     6167        concerned about the time taken to sort, consider the 
     6168        following alternatives.  The moral is: if you want a 
     6169        fast vertex iterator, call the dictionary directly. :: 
     6170 
    61516171            sage: timeit V = P.vertices()                     # not tested 
    61526172            100000 loops, best of 3: 8.85 [micro]s per loop 
    61536173            sage: timeit V = list(P.vertex_iterator())        # not tested 
    61546174            100000 loops, best of 3: 5.74 [micro]s per loop 
    61556175            sage: timeit V = list(P._nxg.adj.iterkeys())      # not tested 
    61566176            100000 loops, best of 3: 3.45 [micro]s per loop 
    6157          
    6158         In other words, if you want a fast vertex iterator, call the 
    6159         dictionary directly. 
     6177 
     6178        We illustrate various ways to use a ``key`` to sort the list:: 
     6179 
     6180            sage: H=graphs.HanoiTowerGraph(3,3,labels=False) 
     6181            sage: H.vertices() 
     6182            [0, 1, 2, 3, 4, ... 22, 23, 24, 25, 26] 
     6183            sage: H.vertices(key=lambda x: -x) 
     6184            [26, 25, 24, 23, 22, ... 4, 3, 2, 1, 0] 
     6185 
     6186        :: 
     6187 
     6188            sage: G=graphs.HanoiTowerGraph(3,3) 
     6189            sage: G.vertices() 
     6190            [(0, 0, 0), (0, 0, 1), (0, 0, 2), (0, 1, 0), ... (2, 2, 1), (2, 2, 2)] 
     6191            sage: G.vertices(key = lambda x: (x[1], x[2], x[0])) 
     6192            [(0, 0, 0), (1, 0, 0), (2, 0, 0), (0, 0, 1), ... (1, 2, 2), (2, 2, 2)] 
     6193 
     6194        :: 
     6195 
     6196            sage: t = polygen(QQ, 't') 
     6197            sage: K = Graph({5*t:[t^2], t^2:[t^2+2], t^2+2:[4*t^2-6], 4*t^2-6:[5*t]}) 
     6198            sage: dsc = sage.rings.polynomial.polynomial_element_generic.Polynomial_rational_dense.discriminant 
     6199            sage: K.vertices(key=dsc) 
     6200            [t^2 + 2, t^2, 5*t, 4*t^2 - 6] 
     6201 
     6202        If boundary vertices are requested first, then they are sorted 
     6203        separately from the remainder (which are also sorted). :: 
     6204 
     6205            sage: P = graphs.PetersenGraph() 
     6206            sage: P.set_boundary((5..9)) 
     6207            sage: P.vertices(boundary_first=True) 
     6208            [5, 6, 7, 8, 9, 0, 1, 2, 3, 4] 
     6209            sage: P.vertices(boundary_first=True, key=lambda x: -x) 
     6210            [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] 
    61606211        """ 
    61616212        if not boundary_first: 
    6162             return sorted(list(self.vertex_iterator())) 
    6163          
     6213            return sorted(list(self.vertex_iterator()), key=key) 
     6214 
    61646215        bdy_verts = [] 
    61656216        int_verts = [] 
    61666217        for v in self.vertex_iterator(): 
     
    61686219                bdy_verts.append(v) 
    61696220            else: 
    61706221                int_verts.append(v) 
    6171         return sorted(bdy_verts) + sorted(int_verts) 
     6222        return sorted(bdy_verts, key=key) + sorted(int_verts, key=key) 
    61726223 
    61736224    def neighbors(self, vertex): 
    61746225        """