k_components

Contents

k_components#

graph_tool.flow.k_components(g, k=None)[git][source]#

Return the \(k\)-components of the graph.

Parameters:
gGraph

Graph to be used. If it is directed, it is treated as undirected.

kint (optional, default: None)

If given, only the \(k\)-components for this value are returned.

Returns:
compsdict or list

If k is None, a dictionary mapping each value of \(k\) to the list of \(k\)-components; otherwise, the list of \(k\)-components. Each component is given by a sorted numpy.ndarray of vertex indices.

See also

vertex_connectivity

Vertex connectivity

label_k_edge_components

K-edge-connected components

graph_tool.structure.label_biconnected_components

Biconnected components

graph_tool.structure.kcore_decomposition

K-core decomposition

Notes

A \(k\)-component is a maximal subgraph with vertex connectivity at least \(k\) (see vertex_connectivity()). The \(k\)-components are nested: each \((k+1)\)-component lies within a \(k\)-component, and those of the same \(k\) may overlap in fewer than \(k\) vertices. The 1-components are the connected components and the 2-components the biconnected components, with more than two vertices. This hierarchy is the structural cohesion of [moody-structural-2003].

The components are found by recursively splitting the subgraphs with minimum vertex separators: if a subgraph has connectivity below \(k\), the subgraphs given by each component of the remainder together with the separator are examined in turn, since a \(k\)-connected subgraph cannot be split by fewer than \(k\) vertices; before each step, the subgraph is reduced to its \(k\)-core (see kcore_decomposition()). This approach follows [wen-enumerating-2019] and requires polynomial time, but each step involves a computation of the vertex connectivity, which makes it expensive for large graphs.

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.

References

[moody-structural-2003]

James Moody and Douglas R. White, “Structural cohesion and embeddedness: a hierarchical concept of social groups”, American Sociological Review 68, 103-127 (2003), DOI: 10.2307/3088904 [sci-hub, @tor]

[wen-enumerating-2019]

Dong Wen, Lu Qin, Ying Zhang, Lijun Chang and Lu Chen, “Enumerating k-vertex connected components in large graphs”, IEEE 35th International Conference on Data Engineering (ICDE), 52-63 (2019), DOI: 10.1109/ICDE.2019.00014 [sci-hub, @tor]

Examples

>>> g = gt.collection.data["karate"]
>>> comps = gt.k_components(g)
>>> print({k: len(c) for k, c in comps.items()})
{1: 1, 2: 2, 3: 2, 4: 1}

The vertices can be colored according to the largest \(k\) of the \(k\)-components to which they belong:

>>> kmax = g.new_vp("int")
>>> for k, cs in comps.items():
...     for c in cs:
...         kmax.a[c] = np.maximum(kmax.a[c], k)
>>> colors = ["#cccccc", "#9bbedc", "#a6d7a4", "#f28c8e"]
>>> vcolor = g.new_vp("string", vals=[colors[k - 1] for k in kmax.a])
>>> gt.graph_draw(g, g.vp.pos, vertex_fill_color=vcolor, vertex_text=kmax,
...               output="k_components-karate.pdf")
<...>
../_images/k_components-karate.png

Largest \(k\) of the \(k\)-components that contain the vertices of Zachary’s karate club network, given by the labels and the colors (gray: 1, blue: 2, green: 3, red: 4).#