kcore_subgraph

Contents

kcore_subgraph#

graph_tool.structure.kcore_subgraph(g, k=None, kind='core', core=None)[git][source]#

Return the \(k\)-core, \(k\)-shell, \(k\)-crust, or \(k\)-corona of the graph.

Parameters:
gGraph

Graph to be used.

kint (optional, default: None)

The value of \(k\). If not given, the largest core number is used.

kindstr (optional, default: "core")

"core" (vertices with core number at least \(k\)), "shell" (exactly \(k\)), "crust" (at most \(k\)), or "corona" (vertices of the \(k\)-core with exactly \(k\) neighbors in it).

coreVertexPropertyMap (optional, default: None)

Core numbers, as given by kcore_decomposition(). If not given, they are computed.

Returns:
uGraphView

The subgraph induced by the selected vertices.

See also

kcore_decomposition

Core number of every vertex

onion_decomposition

Refinement of the k-core decomposition into layers

Notes

With \(c_v\) the core number of vertex \(v\), the subgraph is induced by the vertices with \(c_v \ge k\) (core), \(c_v = k\) (shell) or \(c_v \le k\) (crust), or by the vertices with \(c_v \ge k\) whose total degree in the \(k\)-core is exactly \(k\) (corona). The core, shell, and crust are those defined in [carmi-model-2007], where the \(k\)-crust is the union of the shells with indices smaller than or equal to \(k\) (the remainder of the graph after the \((k+1)\)-core is removed). The algorithm runs in time \(O(V + E)\).

References

[carmi-model-2007]

Shai Carmi, Shlomo Havlin, Scott Kirkpatrick, Yuval Shavitt and Eran Shir, “A model of Internet topology using k-shell decomposition”, Proceedings of the National Academy of Sciences 104, 11150-11154 (2007), DOI: 10.1073/pnas.0701175104 [sci-hub, @tor]

Examples

>>> g = gt.collection.data["karate"]
>>> print(gt.kcore_subgraph(g).num_vertices(),
...       gt.kcore_subgraph(g, 2, "shell").num_vertices())
10 11

The largest core can be highlighted in a drawing of the network:

>>> u = gt.kcore_subgraph(g)
>>> inc = g.new_vp("bool")
>>> inc.a[u.get_vertices()] = True
>>> vcolor = g.new_vp("string", vals=np.where(inc.a, "#f28c8e",
...                                           "#cccccc"))
>>> s = gt.edge_endpoint_property(g, inc, "source")
>>> t = gt.edge_endpoint_property(g, inc, "target")
>>> ecolor = g.new_ep("string", vals=np.where(s.a & t.a, "#e41a1c",
...                                           "#999999"))
>>> gt.graph_draw(g, g.vp.pos, vertex_fill_color=vcolor,
...               edge_color=ecolor, output="kcore_subgraph-karate.pdf")
<...>
../_images/kcore_subgraph-karate.png

Largest core (the 4-core) of Zachary’s karate club network, with its vertices and edges shown in red.#