code: def 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).
  
      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
      -----
      Self loops are ignored.
  
      """
      if nodes is not None:
          # If `nodes` represents a single node, return only its number of triangles
          if nodes in G:
              return next(_triangles_and_degree_iter(G, nodes))[2] // 2
  
          # if `nodes` is a container of nodes, then return a
          # dictionary mapping node to number of triangles.
          return {
              v: t // 2
              for v, d, t, _ in _triangles_and_degree_iter(G, nodes)
          }
  
      # if nodes is None, then compute triangles for the complete graph
  
      # dict used to avoid visiting the same nodes twice
      # this allows calculating/counting each triangle only once
      later_nbrs = {}
  
      # iterate over the nodes in a graph
      for node, neighbors in G.adjacency():
          later_nbrs[node] = {
              n for n in neighbors if n not in later_nbrs and n != node
          }
  
      # instantiate Counter for each node to include isolated nodes
      # add 1 to the count if a nodes neighbor's neighbor is also a neighbor
      triangle_counts = Counter(dict.fromkeys(G, 0))
      for node1, neighbors in later_nbrs.items():
          for node2 in neighbors:
              third_nodes = neighbors & later_nbrs[node2]
              m = len(third_nodes)
              triangle_counts[node1] += m
              triangle_counts[node2] += m
              triangle_counts.update(third_nodes)
  
      return dict(triangle_counts)
  
file_path: networkx/networkx/algorithms/cluster.py
imports: from collections import Counter
  from itertools import chain, combinations
  import networkx
  from networkx.utils import not_implemented_for
label_desc: Use image node_id 1 for calling a global function with example usage: triangles(G, nodes) and returns: dict, unknown, unknown