import networkx as nx
import numpy as np
import time
np.random.seed(0)
N = 500 # node size
p = 0.4
G = nx.fast_gnp_random_graph(N, p, seed=0)
# exact betweenness centrality
bet_time = time.time()
nx_bet_dict = nx.betweenness_centrality(G)
bet_time = time.time() - bet_time
print(f"exact betweenness cent calc time: {bet_time: .4f}")
## check its accuracy with varying K
print("== betwenness centrality with K node sampling and its estimation")
VARYING_K=True
if VARYING_K == True:
# exact betwenness centrality
nx_bet_np = np.array([*nx_bet_dict.values()])
nx_bet_np /= np.linalg.norm(nx_bet_np)
for K in range(N//10, N+1, N//10):
# betwenness centrality with K node sampling and its estimation
k_bet_time = time.time()
nx_bet_dict_K = nx.betweenness_centrality(G, k=K)
nx_bet_K_np = np.array([*nx_bet_dict_K.values()])
nx_bet_K_np /= np.linalg.norm(nx_bet_K_np)
k_bet_time = time.time() - k_bet_time
bet_corr = np.correlate(nx_bet_np, nx_bet_K_np)[0]
print(f"k: {K:4d} - corr: {bet_corr:.4f} - time: {k_bet_time:6.3f}")
print("==")
exact betweenness cent calc time: 12.1926
== betwenness centrality with K node sampling and its estimation
k: 50 - corr: 0.9886 - time: 1.240
k: 100 - corr: 0.9937 - time: 2.427
k: 150 - corr: 0.9967 - time: 3.651
k: 200 - corr: 0.9977 - time: 4.860
k: 250 - corr: 0.9984 - time: 6.262
k: 300 - corr: 0.9989 - time: 7.392
k: 350 - corr: 0.9994 - time: 8.444
k: 400 - corr: 0.9996 - time: 9.780
k: 450 - corr: 0.9998 - time: 10.827
k: 500 - corr: 1.0000 - time: 13.183
==
댓글남기기