code: def core_number(G):
      """Returns the core number for each node.
  
      A k-core is a maximal subgraph that contains nodes of degree k or more.
  
      The core number of a node is the largest value k of a k-core containing
      that node.
  
      Parameters
      ----------
      G : NetworkX graph
         An undirected or directed graph
  
      Returns
      -------
      core_number : dictionary
         A dictionary keyed by node to the core number.
  
      Raises
      ------
      NetworkXNotImplemented
          If `G` is a multigraph or contains self loops.
  
      Notes
      -----
      For directed graphs the node degree is defined to be the
      in-degree + out-degree.
  
      Examples
      --------
      >>> degrees = [0, 1, 2, 2, 2, 2, 3]
      >>> H = nx.havel_hakimi_graph(degrees)
      >>> nx.core_number(H)
      {0: 1, 1: 2, 2: 2, 3: 2, 4: 1, 5: 2, 6: 0}
      >>> G = nx.DiGraph()
      >>> G.add_edges_from([(1, 2), (2, 1), (2, 3), (2, 4), (3, 4), (4, 3)])
      >>> nx.core_number(G)
      {1: 2, 2: 2, 3: 2, 4: 2}
  
      References
      ----------
      .. [1] An O(m) Algorithm for Cores Decomposition of Networks
         Vladimir Batagelj and Matjaz Zaversnik, 2003.
         https://arxiv.org/abs/cs.DS/0310049
      """
      if nx.number_of_selfloops(G) > 0:
          msg = (
              "Input graph has self loops which is not permitted; "
              "Consider using G.remove_edges_from(nx.selfloop_edges(G))."
          )
          raise nx.NetworkXNotImplemented(msg)
      degrees = dict(G.degree())
      # Sort nodes by degree.
      nodes = sorted(degrees, key=degrees.get)
      bin_boundaries = [0]
      curr_degree = 0
      for i, v in enumerate(nodes):
          if degrees[v] > curr_degree:
              bin_boundaries.extend([i] * (degrees[v] - curr_degree))
              curr_degree = degrees[v]
      node_pos = {v: pos for pos, v in enumerate(nodes)}
      # The initial guess for the core number of a node is its degree.
      core = degrees
      nbrs = {v: list(nx.all_neighbors(G, v)) for v in G}
      for v in nodes:
          for u in nbrs[v]:
              if core[u] > core[v]:
                  nbrs[u].remove(v)
                  pos = node_pos[u]
                  bin_start = bin_boundaries[core[u]]
                  node_pos[u] = bin_start
                  node_pos[nodes[bin_start]] = pos
                  nodes[bin_start], nodes[pos] = (
                      nodes[pos],
                      nodes[bin_start],
                  )
                  bin_boundaries[core[u]] += 1
                  core[u] -= 1
      return core
  
file_path: networkx/networkx/algorithms/core.py
imports: import networkx
label_desc: Use image node_id 1 for calling a global function with example usage: core_number(G) and returns: core