Skip to main content
`Ctrl`+`K`

# triangles

triangles(G, nodes=None)

  Compute the number of triangles.

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

  Parameters:
    G graph

      A networkx graph

    nodes node, iterable of nodes, or None (default=None)

      If a singleton node, return the number of triangles for that node. If an iterable, compute the number of triangles for each of those nodes. If `None` (the default) compute the number of triangles for all nodes in `G`.

  Returns:
    out dict or int

      If `nodes` is a container of nodes, returns number of triangles keyed by node (dict). If `nodes` is a specific node, returns number of triangles for the node (int).

  Notes

  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]