code: def constraint(G, nodes=None, weight=None):
      r"""Returns the constraint on all nodes in the graph ``G``.
  
      The *constraint* is a measure of the extent to which a node *v* is
      invested in those nodes that are themselves invested in the
      neighbors of *v*. Formally, the *constraint on v*, denoted `c(v)`,
      is defined by
  
      .. math::
  
         c(v) = \sum_{w \in N(v) \setminus \{v\}} \ell(v, w)
  
      where $N(v)$ is the subset of the neighbors of `v` that are either
      predecessors or successors of `v` and $\ell(v, w)$ is the local
      constraint on `v` with respect to `w` [1]_. For the definition of local
      constraint, see :func:`local_constraint`.
  
      Parameters
      ----------
      G : NetworkX graph
          The graph containing ``v``. This can be either directed or undirected.
  
      nodes : container, optional
          Container of nodes in the graph ``G`` to compute the constraint. If
          None, the constraint of every node is computed.
  
      weight : None or string, optional
        If None, all edge weights are considered equal.
        Otherwise holds the name of the edge attribute used as weight.
  
      Returns
      -------
      dict
          Dictionary with nodes as keys and the constraint on the node as values.
  
      See also
      --------
      local_constraint
  
      References
      ----------
      .. [1] Burt, Ronald S.
             "Structural holes and good ideas".
             American Journal of Sociology (110): 349–399.
  
      """
      if nodes is None:
          nodes = G
      constraint = {}
      for v in nodes:
          # Constraint is not defined for isolated nodes
          if len(G[v]) == 0:
              constraint[v] = float("nan")
              continue
          constraint[v] = sum(
              local_constraint(G, v, n, weight)
              for n in set(nx.all_neighbors(G, v))
          )
      return constraint
  
imports: import networkx
label_desc: Use image node_id 4 for calling a global function with example usage: constraint(G, nodes, weight) and returns: constraint