graph_tool.topology.label_components#

graph_tool.topology.label_components(g, vprop=None, directed=None, attractors=False)[source]#

Label the components to which each vertex in the graph belongs. If the graph is directed, it finds the strongly connected components.

A property map with the component labels is returned, together with an histogram of component labels.

Parameters:
gGraph

Graph to be used.

vpropVertexPropertyMap (optional, default: None)

Vertex property to store the component labels. If none is supplied, one is created.

directedbool (optional, default: None)

Treat graph as directed or not, independently of its actual directionality.

attractorsbool (optional, default: False)

If True, and the graph is directed, an additional array with Boolean values is returned, specifying if the strongly connected components are attractors or not.

Returns:
compVertexPropertyMap

Vertex property map with component labels.

histnumpy.ndarray

Histogram of component labels.

is_attractornumpy.ndarray

A Boolean array specifying if the strongly connected components are attractors or not. This returned only if attractors == True, and the graph is directed.

Notes

The components are arbitrarily labeled from 0 to N-1, where N is the total number of components.

The algorithm runs in \(O(V + E)\) time.

Examples

>>> g = gt.random_graph(100, lambda: (poisson(2), poisson(2)))
>>> comp, hist, is_attractor = gt.label_components(g, attractors=True)
>>> print(comp.a)
[15 15 11 15 15 10 19 15 15 12 15 16 18  1 15 15 15 15 15 14  6 15 15 20
 22 15 23 15 15 25 15 26 15 29  9 15 15  4 31 15 30 15 33 15 15 15 21 13
 15 15 15 15 15  0  5  3 15 34  2 15 24  7 35 15 15 15 15 15 36 15 15 15
 27 15 32 15 37 38  8 15 15 15 15 17 15 39 15 40 15 15 15 41 42 15 15 15
 15 28 15 15]
>>> print(hist)
[ 1  1  1  1  1  1  1  1  1  1  1  1  1  1  1 58  1  1  1  1  1  1  1  1
  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1  1]
>>> print(is_attractor)
[ True False  True False  True  True  True  True False False  True  True
 False  True  True False False False False False False  True False False
 False False  True False False False  True False False False False  True
 False False False False False False False]