import networkx as nx
import time
# Generate graph
N = 300
G = nx.scale_free_graph(N, seed=0)
G = nx.Graph(G)
assert nx.is_connected(G) == True
print("==" * 30)
print("== community detection by edge betweenness centrality")
girvan_start_time = time.time()
Max_modularity = 0
Max_module_communities = None
community_generator = nx.algorithms.community.girvan_newman(G=G)
for i, comms in enumerate(community_generator):
comm_modularity = nx.algorithms.community.modularity(G, comms)
if Max_modularity<=comm_modularity:
Max_modularity = comm_modularity
Max_module_communities = comms
gen_time = time.time() - girvan_start_time
#print(f"{i:2d} Modularity: {comm_modularity:.5f} == times: {gen_time}")
if comm_modularity<0.001:
break
print(
f"Max_modularity: {Max_modularity:.5f}, time: {time.time() - girvan_start_time:.5f}"
)
for i, comm in enumerate(Max_module_communities):
#print(f"community {i:2d} ::: {comm}")
continue
print("=="*30)
print("== community detection by greedy modularity community")
#================================================
# by greedy modularity community
greedy_start_time = time.time()
greedy_module_comms = nx.algorithms.community.greedy_modularity_communities(G)
print(
f"Modularity: {nx.algorithms.community.modularity(G, greedy_module_comms):.5f} == times: {time.time() - greedy_start_time:.5f}"
)
for i, comm in enumerate(greedy_module_comms):
#print(f"community {i:2d} ::: {comm}")
continue
print("== complete")
print("==" * 30)
============================================================
== community detection by edge betweenness centrality
Max_modularity: 0.47044, time: 64.24782
============================================================
== community detection by greedy modularity community
Modularity: 0.50138 == times: 0.25804
== complete
============================================================
댓글남기기