# HG changeset patch
# User Nathann Cohen <nathann.cohen@gmail.com>
# Date 1327491226 -3600
# Node ID 319a2a04c664c9883b7e38bd22c7d55dd65791a2
# Parent b4d18004fc71f2804ab58a8c505531e312c303df
trac 12355 -- Bug in Graph.girth
diff --git a/sage/graphs/generic_graph.py b/sage/graphs/generic_graph.py
|
a
|
b
|
|
| 11264 | 11264 | """ |
| 11265 | 11265 | Computes the girth of the graph. For directed graphs, computes the |
| 11266 | 11266 | girth of the undirected graph. |
| 11267 | | |
| | 11267 | |
| 11268 | 11268 | The girth is the length of the shortest cycle in the graph. Graphs |
| 11269 | 11269 | without cycles have infinite girth. |
| 11270 | | |
| 11271 | | EXAMPLES:: |
| 11272 | | |
| | 11270 | |
| | 11271 | EXAMPLES:: |
| | 11272 | |
| 11273 | 11273 | sage: graphs.TetrahedralGraph().girth() |
| 11274 | 11274 | 3 |
| 11275 | 11275 | sage: graphs.CubeGraph(3).girth() |
| … |
… |
|
| 11280 | 11280 | 6 |
| 11281 | 11281 | sage: graphs.trees(9).next().girth() |
| 11282 | 11282 | +Infinity |
| 11283 | | |
| | 11283 | |
| 11284 | 11284 | TESTS: |
| 11285 | | |
| | 11285 | |
| 11286 | 11286 | Prior to Trac #12243, the girth computation assumed |
| 11287 | 11287 | vertices were integers (and failed). The example below |
| 11288 | 11288 | tests the computation for graphs with vertices that are |
| 11289 | 11289 | not integers. In this example the vertices are sets. :: |
| 11290 | | |
| | 11290 | |
| 11291 | 11291 | sage: G = graphs.OddGraph(3) |
| 11292 | 11292 | sage: type(G.vertices()[0]) |
| 11293 | 11293 | <class 'sage.sets.set.Set_object_enumerated_with_category'> |
| 11294 | 11294 | sage: G.girth() |
| 11295 | | 5 |
| 11296 | | """ |
| | 11295 | 5 |
| | 11296 | |
| | 11297 | Ticket :trac:`12355`:: |
| | 11298 | |
| | 11299 | sage: H=Graph([(0, 1), (0, 3), (0, 4), (0, 5), (1, 2), (1, 3), (1, 4), (1, 6), (2, 5), (3, 4), (5, 6)]) |
| | 11300 | sage: H.girth() |
| | 11301 | 3 |
| | 11302 | |
| | 11303 | Girth < 3 (see :trac:`12355`):: |
| | 11304 | |
| | 11305 | sage: g = graphs.PetersenGraph() |
| | 11306 | sage: g.allow_multiple_edges(True) |
| | 11307 | sage: g.allow_loops(True) |
| | 11308 | sage: g.girth() |
| | 11309 | 5 |
| | 11310 | sage: g.add_edge(0,0) |
| | 11311 | sage: g.girth() |
| | 11312 | 1 |
| | 11313 | sage: g.delete_edge(0,0) |
| | 11314 | sage: g.add_edge(0,1) |
| | 11315 | sage: g.girth() |
| | 11316 | 2 |
| | 11317 | sage: g.delete_edge(0,1) |
| | 11318 | sage: g.girth() |
| | 11319 | 5 |
| | 11320 | sage: g = DiGraph(g) |
| | 11321 | sage: g.girth() |
| | 11322 | 2 |
| | 11323 | """ |
| | 11324 | |
| | 11325 | # Cases where girth <= 2 |
| | 11326 | if self.has_loops(): |
| | 11327 | return 1 |
| | 11328 | if self.is_directed(): |
| | 11329 | if any(self.has_edge(v,u) for u,v in self.edges(labels = False)): |
| | 11330 | return 2 |
| | 11331 | else: |
| | 11332 | if self.has_multiple_edges(): |
| | 11333 | return 2 |
| | 11334 | |
| 11297 | 11335 | n = self.num_verts() |
| 11298 | 11336 | best = n+1 |
| 11299 | 11337 | seen = {} |
| … |
… |
|
| 11313 | 11351 | else: |
| 11314 | 11352 | if u in thisList: |
| 11315 | 11353 | best = depth*2-1 |
| | 11354 | thislList = set() |
| 11316 | 11355 | break |
| 11317 | 11356 | if u in nextList: |
| 11318 | 11357 | best = depth*2 |
| | 11358 | if best == 2*depth-1: |
| | 11359 | break |
| 11319 | 11360 | thisList = nextList |
| 11320 | 11361 | depth += 1 |
| 11321 | 11362 | if best == n+1: |