| 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 | |
| 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] |