k_core#

k_core(G, k=None, core_number=None)[source]#

Returns the k-core of G.

A k-core is a maximal subgraph that contains nodes of degree k or more.

Deprecated since version 3.3: k_core will not accept MultiGraph objects in version 3.5.

Parameters:
GNetworkX graph

A graph or directed graph

kint, optional

The order of the core. If not specified return the main core.

core_numberdictionary, optional

Precomputed core numbers for the graph G.

Returns:
GNetworkX graph

The k-core subgraph

Raises:
NetworkXNotImplemented

The k-core is not defined for multigraphs or graphs with self loops.

See also

core_number

Notes

The main core is the core with k as the largest core_number.

For directed graphs the node degree is defined to be the in-degree + out-degree.

Graph, node, and edge attributes are copied to the subgraph.

References

[1]

An O(m) Algorithm for Cores Decomposition of Networks Vladimir Batagelj and Matjaz Zaversnik, 2003. https://arxiv.org/abs/cs.DS/0310049

Examples

>>> degrees = [0, 1, 2, 2, 2, 2, 3]
>>> H = nx.havel_hakimi_graph(degrees)
>>> H.degree
DegreeView({0: 1, 1: 2, 2: 2, 3: 2, 4: 2, 5: 3, 6: 0})
>>> nx.k_core(H).nodes
NodeView((1, 2, 3, 5))