code: def barycenter(G, weight=None, attr=None, sp=None):
      r"""Calculate barycenter of a connected graph, optionally with edge weights.
  
      The :dfn:`barycenter` a
      :func:`connected <networkx.algorithms.components.is_connected>` graph
      :math:`G` is the subgraph induced by the set of its nodes :math:`v`
      minimizing the objective function
  
      .. math::
  
          \sum_{u \in V(G)} d_G(u, v),
  
      where :math:`d_G` is the (possibly weighted) :func:`path length
      <networkx.algorithms.shortest_paths.generic.shortest_path_length>`.
      The barycenter is also called the :dfn:`median`. See [West01]_, p. 78.
  
      Parameters
      ----------
      G : :class:`networkx.Graph`
          The connected graph :math:`G`.
      weight : :class:`str`, optional
          Passed through to
          :func:`~networkx.algorithms.shortest_paths.generic.shortest_path_length`.
      attr : :class:`str`, optional
          If given, write the value of the objective function to each node's
          `attr` attribute. Otherwise do not store the value.
      sp : dict of dicts, optional
         All pairs shortest path lengths as a dictionary of dictionaries
  
      Returns
      -------
      list
          Nodes of `G` that induce the barycenter of `G`.
  
      Raises
      ------
      NetworkXNoPath
          If `G` is disconnected. `G` may appear disconnected to
          :func:`barycenter` if `sp` is given but is missing shortest path
          lengths for any pairs.
      ValueError
          If `sp` and `weight` are both given.
  
      Examples
      --------
      >>> G = nx.Graph([(1, 2), (1, 3), (1, 4), (3, 4), (3, 5), (4, 5)])
      >>> nx.barycenter(G)
      [1, 3, 4]
  
      See Also
      --------
      center
      periphery
      """
      if sp is None:
          sp = nx.shortest_path_length(G, weight=weight)
      else:
          sp = sp.items()
          if weight is not None:
              raise ValueError(
                  "Cannot use both sp, weight arguments together"
              )
      smallest, barycenter_vertices, n = float("inf"), [], len(G)
      for v, dists in sp:
          if len(dists) < n:
              raise nx.NetworkXNoPath(
                  f"Input graph {G} is disconnected, so every induced subgraph "
                  "has infinite barycentricity."
              )
          barycentricity = sum(dists.values())
          if attr is not None:
              G.nodes[v][attr] = barycentricity
          if barycentricity < smallest:
              smallest = barycentricity
              barycenter_vertices = [v]
          elif barycentricity == smallest:
              barycenter_vertices.append(v)
      return barycenter_vertices
  
file_path: networkx/networkx/algorithms/distance_measures.py
imports: import networkx
  from networkx.utils import not_implemented_for
label_desc: Use image node_id 7 for calling a global function with example usage: barycenter(G, weight, attr, sp) and returns: barycenter_vertices