def find_partition(graph):
# code and lib from http://perso.crans.org/aynaud/communities/
# must be an undirected graph
g = graph
partition = community.best_partition(g)
print "Partitions found: ", len(set(partition.values()))
# to show members of each partition:
for i in set(partition.values()):
members = [nodes for nodes in partition.keys() if partition[nodes] == i]
print i, len(members)
# if i==0:
# # write out the subgraph
# community_graph = graph.subgraph(members)
# #draw_graph(community_graph)
# #nx.write_edgelist(community_graph, "community.edgelist", data=False)
# #for member in members:
# # print member, i
# print "Partition for node johncoogan: ", partition[node_id]
nx.set_node_attributes(g, 'partition', partition)
return g, partition
评论列表
文章目录