code: def density(G):
      r"""Returns the density of a graph.
  
      The density for undirected graphs is
  
      .. math::
  
         d = \frac{2m}{n(n-1)},
  
      and for directed graphs is
  
      .. math::
  
         d = \frac{m}{n(n-1)},
  
      where `n` is the number of nodes and `m`  is the number of edges in `G`.
  
      Notes
      -----
      The density is 0 for a graph without edges and 1 for a complete graph.
      The density of multigraphs can be higher than 1.
  
      Self loops are counted in the total number of edges so graphs with self
      loops can have density higher than 1.
      """
      n = number_of_nodes(G)
      m = number_of_edges(G)
      if m == 0 or n <= 1:
          return 0
      d = m / (n * (n - 1))
      if not G.is_directed():
          d *= 2
      return d
  
imports: from collections import Counter
  from itertools import chain
  import networkx
  from networkx.utils import not_implemented_for, pairwise
label_desc: Use image node_id 7 for calling a global function with example usage: density(G) and returns: d, int