girth

Contents

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:
gGraph

Graph to be used.

cyclebool (optional, default: False)

If True, a shortest cycle is also returned.

Returns:
girthint or float

The length of the shortest cycle (directed, if the graph is directed), or numpy.inf if there are no cycles.

cyclenumpy.ndarray

The vertices of a shortest cycle, in order (or None if there are no cycles). Only returned if cycle == True.

See also

find_cycle

A single cycle of the graph

cycle_basis

Basis of the cycle space

chordless_cycles

All 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))