使用以下可工作的代码:
我该如何得到n个新的图形(或子图),每个社区都有一个对象(描述为“具有n个节点和w条边的图形”)?
import netowkx as nx
import networkx.algorithms.community as nx_comm
G = nx.karate_club_graph()
# Find the communities
communities = sorted(nx_comm.greedy_modularity_communities(G), key=len, reverse=True)
# Count the communities
print(f"The club has {len(communities)} communities.")
'''Add community to node attributes'''
for c, v_c in enumerate(communities):
for v in v_c:
# Add 1 to save 0 for external edges
G.nodes[v]['community'] = c + 1
'''Find internal edges and add their community to their attributes'''
for v, w, in G.edges:
if G.nodes[v]['community'] == G.nodes[w]['community']:
# Internal edge, mark with community
G.edges[v, w]['community'] = G.nodes[v]['community']
else:
# External edge, mark as 0
G.edges[v, w]['community'] = 0
我该如何得到n个新的图形(或子图),每个社区都有一个对象(描述为“具有n个节点和w条边的图形”)?