butterflies

Contents

butterflies#

graph_tool.clustering.butterflies(g, total=False, prop=None)[git][source]#

Return the number of butterflies (cycles of length four) of the vertices, or of the whole graph.

Parameters:
gGraph

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

totalbool (optional, default: False)

If True, only the total number of butterflies in the graph is returned.

propVertexPropertyMap (optional, default: None)

Vertex property map of type int64_t where results will be stored.

Returns:
propVertexPropertyMap or int

Vertex property map with the number of butterflies that contain every vertex, or the total number of butterflies if total == True.

See also

robins_alexander_clustering

Bipartite clustering based on butterflies

square_clustering

Square clustering coefficient

Notes

A butterfly [sanei-mehri-butterfly-2018] is a complete bipartite subgraph \(K_{2,2}\), i.e. a cycle of length four, which is the smallest cycle of a bipartite graph. The cycles of length four are counted in the same way in graphs that are not bipartite. The number of butterflies that contain vertex \(v\) is

\[b_v = \sum_{w\ne v}\binom{|N_v\cap N_w|}{2},\]

and the total number is \(\sum_vb_v/4\). Parallel edges and self-loops are ignored.

The butterflies are enumerated with the vertex-priority algorithm of [wang-accelerated-2023], which visits every butterfly once, from its vertex of largest degree, in time \(O(\sum_{(u,v)\in E}\min(k_u,k_v))\).

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

[sanei-mehri-butterfly-2018]

Seyed-Vahid Sanei-Mehri, Ahmet Erdem Sariyuce and Srikanta Tirthapura, “Butterfly counting in bipartite networks”, Proceedings of the 24th ACM SIGKDD International Conference on Knowledge Discovery & Data Mining, 2150-2159 (2018), DOI: 10.1145/3219819.3220097 [sci-hub, @tor]

[wang-accelerated-2023]

Kai Wang, Xuemin Lin, Lu Qin, Wenjie Zhang and Ying Zhang, “Accelerated butterfly counting with vertex priority on bipartite graphs”, The VLDB Journal 32, 257-281 (2023), DOI: 10.1007/s00778-022-00746-0 [sci-hub, @tor]

Examples

>>> g = gt.collection.ns["davis_southern_women"]
>>> b = gt.butterflies(g)
>>> print(b.a[:10])
[75 68 91 71 21 30 33 17 32 30]
>>> print(gt.butterflies(g, total=True))
341

The values can be visualized by drawing the network with vertex colors and sizes given by them, and with the women as circles and the events as squares:

>>> pos = gt.sfdp_layout(g)
>>> x = b.copy("double")
>>> cmap = matplotlib.colors.LinearSegmentedColormap.from_list(
...     "gray-blue", ["#d9d9d9", "#7dabd1"])
>>> shape = g.new_vp("string", vals=np.where(g.vp.is_woman.a, "circle",
...                                            "square"))
>>> gt.graph_draw(g, pos, vertex_fill_color=x, vcmap=cmap,
...               vertex_size=gt.prop_to_size(x, mi=5, ma=15),
...               vertex_shape=shape,
...               output="butterflies-davis.pdf")
<...>
../_images/butterflies-davis.png

Numbers of butterflies that contain the vertices of the bipartite network of women and the social events they attended. The women are shown as circles and the events as squares. The vertex colors, from gray to blue, and the vertex sizes increase with the value.#