Warning

This documents an unmaintained version of NetworkX. Please upgrade to a maintained version and see the current NetworkX documentation.

networkx.algorithms.cluster.triangles

triangles(G, nodes=None)[source]

Compute the number of triangles.

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

Parameters:
  • G (graph) – A networkx graph
  • nodes (container of nodes, optional (default= all nodes in G)) – Compute triangles for nodes in this container.
Returns:

out – Number of triangles keyed by node label.

Return type:

dictionary

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]

Notes

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