NetworkX

Previous topic

networkx.algorithms.cluster.square_clustering

Next topic

networkx.algorithms.cluster.transitivity

networkx.algorithms.cluster.triangles

networkx.algorithms.cluster.triangles(G, nodes=None)

Compute the number of triangles.

Finds the number of triangles that include a node as one of the vertices.

Parameters :

G : graph

A networkx graph

nodes : container of nodes, optional

Compute triangles for nodes. The default is all nodes in G.

Returns :

out : dictionary

Number of trianges keyed by node label.

Notes

When computing triangles for the entire graph each triangle is counted three times, once at each node. Self loops are ignored.

Examples

>>> G=nx.complete_graph(5)
>>> print(nx.triangles(G,0))
6
>>> print(nx.triangles(G))
{0: 6, 1: 6, 2: 6, 3: 6, 4: 6}
>>> print(list(nx.triangles(G,(0,1)).values()))
[6, 6]