def _generate_communities(degree_sequence, community_sizes, mu, max_iters):
 result = [set() for _ in community_sizes]
 n = len(degree_sequence)
 free = list(range(n))
 for i in range(max_iters):
  v = free.pop()
  c = random.choice(range(len(community_sizes)))
  s = round((degree_sequence[v] * (1 - mu)))
  if (s < community_sizes[c]):
   result[c].add(v)
  else:
   free.append(v)
  if (len(result[c]) > community_sizes[c]):
   free.append(result[c].pop())
  if (not free):
   return result
 msg = 'Could not assign communities; try increasing min_community'
 raise nx.ExceededMaxIterations(msg)

def _generate_communities ( degree_sequence , community_sizes , mu , max_iters ) : result = [ set ( ) for _ in community_sizes ] n = len ( degree_sequence ) free = list ( range ( n ) ) for i in range ( max_iters ) : v = free . pop ( ) c = random . choice ( range ( len ( community_sizes ) ) ) s = round ( ( degree_sequence [ v ] * ( 1 - mu ) ) ) if ( s < community_sizes [ c ] ) : result [ c ] . add ( v ) else : free . append ( v ) if ( len ( result [ c ] ) > community_sizes [ c ] ) : free . append ( result [ c ] . pop ( ) ) if ( not free ) : return result msg = 'Could not assign communities; try increasing min_community' raise nx . ExceededMaxIterations ( msg )
returns a list of sets .