girth#
- graph_tool.structure.girth(g, cycle=False)[git][source]#
Return the girth of the graph, i.e. the length of its shortest cycle.
- Parameters:
- g
Graph Graph to be used.
- cycle
bool(optional, default:False) If
True, a shortest cycle is also returned.
- g
- Returns:
- girth
intorfloat The length of the shortest cycle (directed, if the graph is directed), or
numpy.infif there are no cycles.- cycle
numpy.ndarray The vertices of a shortest cycle, in order (or
Noneif there are no cycles). Only returned ifcycle == True.
- girth
See also
find_cycleA single cycle of the graph
cycle_basisBasis of the cycle space
chordless_cyclesAll chordless cycles
Notes
The girth is the length of a shortest cycle, i.e. its number of edges. Self-loops are cycles of length one, and in undirected graphs parallel edges form cycles of length two.
A breadth-first search is made from every vertex, in parallel, and is stopped once it can no longer find a cycle shorter than the shortest one found so far. In the worst case, the time is \(O(V(V + E))\).
Parallel implementation.
If enabled during compilation, this algorithm will run in parallel using OpenMP. See the parallel algorithms section for information about how to control several aspects of parallelization.
Examples
>>> g = gt.collection.petersen_graph() >>> print(gt.girth(g)) 5 >>> g = gt.collection.data["karate"] >>> print(gt.girth(g, cycle=True)) (3, array([0, 1, 2], dtype=uint64))