def girvan_newman(G, most_valuable_edge=None):
 if (G.number_of_edges() == 0):
  (yield tuple(nx.connected_components(G)))
  return
 if (most_valuable_edge is None):
  def most_valuable_edge(G):
   'Returns the edge with the highest betweenness centrality\n            in the graph `G`.\n\n            '
   betweenness = nx.edge_betweenness_centrality(G)
   return max(betweenness, key=betweenness.get)
 g = G.copy().to_undirected()
 g.remove_edges_from(g.selfloop_edges())
 while (g.number_of_edges() > 0):
  (yield _without_most_central_edges(g, most_valuable_edge))

def girvan_newman ( G , most_valuable_edge = None ) : if ( G . number_of_edges ( ) == 0 ) : ( yield tuple ( nx . connected_components ( G ) ) ) return if ( most_valuable_edge is None ) : def most_valuable_edge ( G ) : betweenness = nx . edge_betweenness_centrality ( G ) return max ( betweenness , key = betweenness . get ) g = G . copy ( ) . to_undirected ( ) g . remove_edges_from ( g . selfloop_edges ( ) ) while ( g . number_of_edges ( ) > 0 ) : ( yield _without_most_central_edges ( g , most_valuable_edge ) )
finds communities in a graph using the girvan–newman method .