label_components

label_components#

graph_tool.structure.label_components(g, vprop=None, directed=None, attractors=False)[git][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 a 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 is returned only if attractors == True, and the graph is directed.

See also

label_largest_component

Label the largest component

extract_largest_component

Extract the largest component

label_biconnected_components

Biconnected components

label_out_component

Vertices reachable from a given vertex

count_reachable

Number of vertices reachable from every vertex

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)
[12 12  4 12 12 13 16 12 12 12 12 17 12 12 12 12 12 12 12  7 14 12 12 18
 19 12 20 12 12 21 12 11 12 22 12 12 12  1 23 12 10 12 24 12  3 12  9  6
 12 12 12 12 12  0  5 12 12 25  8 12 12  2 26 15 12 12 12 12 27 12 12 12
 12 12 12 12 28 29 12 12 12 12 12 12 12 30 12 31 12 12 12 32 33 12 12 12
 12 12 12 12]
>>> print(hist)
[ 1  1  1  1  1  1  1  1  1  1  1  1 67  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  True  True False  True  True  True  True  True  True  True  True
 False  True  True False False False False False False False False False
 False False  True False False False False False False False]

The largest component and the attractors can be shown in a drawing of the graph:

>>> pos = gt.sfdp_layout(g)
>>> vcolor = g.new_vp("string",
...                   vals=np.where(comp.a == hist.argmax(), "#f28c8e",
...                                 np.where(is_attractor[comp.a],
...                                          "#9bbedc", "#cccccc")))
>>> gt.graph_draw(g, pos, vertex_fill_color=vcolor, edge_color="#999999",
...               output="label_components-random.pdf")
<...>
../_images/label_components-random.png

Strongly connected components of a directed random graph. The largest component is shown in red, the other attractors in blue, and the remaining vertices in gray.#