code: def naive_greedy_modularity_communities(G, resolution=1, weight=None):
      r"""Find communities in G using greedy modularity maximization.
  
      This implementation is O(n^4), much slower than alternatives, but it is
      provided as an easy-to-understand reference implementation.
  
      Greedy modularity maximization begins with each node in its own community
      and joins the pair of communities that most increases modularity until no
      such pair exists.
  
      This function maximizes the generalized modularity, where `resolution`
      is the resolution parameter, often expressed as $\gamma$.
      See :func:`~networkx.algorithms.community.quality.modularity`.
  
      Parameters
      ----------
      G : NetworkX graph
          Graph must be simple and undirected.
  
      resolution : float (default=1)
          If resolution is less than 1, modularity favors larger communities.
          Greater than 1 favors smaller communities.
  
      weight : string or None, optional (default=None)
          The name of an edge attribute that holds the numerical value used
          as a weight.  If None, then each edge has weight 1.
          The degree is the sum of the edge weights adjacent to the node.
  
      Returns
      -------
      list
          A list of sets of nodes, one for each community.
          Sorted by length with largest communities first.
  
      Examples
      --------
      >>> G = nx.karate_club_graph()
      >>> c = nx.community.naive_greedy_modularity_communities(G)
      >>> sorted(c[0])
      [8, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]
  
      See Also
      --------
      greedy_modularity_communities
      modularity
      """
      # First create one community for each node
      communities = [frozenset([u]) for u in G.nodes()]
      # Track merges
      merges = []
      # Greedily merge communities until no improvement is possible
      old_modularity = None
      new_modularity = modularity(
          G, communities, resolution=resolution, weight=weight
      )
      while old_modularity is None or new_modularity > old_modularity:
          # Save modularity for comparison
          old_modularity = new_modularity
          # Find best pair to merge
          trial_communities = list(communities)
          to_merge = None
          for i, u in enumerate(communities):
              for j, v in enumerate(communities):
                  # Skip i==j and empty communities
                  if j <= i or len(u) == 0 or len(v) == 0:
                      continue
                  # Merge communities u and v
                  trial_communities[j] = u | v
                  trial_communities[i] = frozenset([])
                  trial_modularity = modularity(
                      G,
                      trial_communities,
                      resolution=resolution,
                      weight=weight,
                  )
                  if trial_modularity >= new_modularity:
                      # Check if strictly better or tie
                      if trial_modularity > new_modularity:
                          # Found new best, save modularity and group indexes
                          new_modularity = trial_modularity
                          to_merge = (
                              i,
                              j,
                              new_modularity - old_modularity,
                          )
                      elif to_merge and min(i, j) < min(
                          to_merge[0], to_merge[1]
                      ):
                          # Break ties by choosing pair with lowest min id
                          new_modularity = trial_modularity
                          to_merge = (
                              i,
                              j,
                              new_modularity - old_modularity,
                          )
                  # Un-merge
                  trial_communities[i] = u
                  trial_communities[j] = v
          if to_merge is not None:
              # If the best merge improves modularity, use it
              merges.append(to_merge)
              i, j, dq = to_merge
              u, v = communities[i], communities[j]
              communities[j] = u | v
              communities[i] = frozenset([])
      # Remove empty communities and sort
      return sorted(
          (c for c in communities if len(c) > 0), key=len, reverse=True
      )
  
file_path: networkx/networkx/algorithms/community/modularity_max.py
imports: from collections import defaultdict
  import networkx
  from networkx.algorithms.community.quality import modularity
  from networkx.utils import not_implemented_for
  from networkx.utils.mapped_queue import MappedQueue
label_desc: Use image node_id 3 for calling a global function with example usage: naive_greedy_modularity_communities(G, resolution, weight) and returns: sorted