import networkx as nx
import numpy as np
import time
def Derive_CORR_from_TWO_CENT_DICT(dict_A, dict_B):
# nx.centrality의 결과인 dictionary(nodename: cent_value) 2개를 input으로
# correlation을 리턴함
A_np = np.array([*dict_A.values()])
A_np /= np.linalg.norm(A_np)
B_np = np.array([*dict_B.values()])
B_np /= np.linalg.norm(B_np)
return np.correlate(A_np, B_np)[0]
np.random.seed(0)
N = 300 # node size
p = 0.4
G = nx.fast_gnp_random_graph(N, p, seed=0)
print("==" * 30)
# 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} sec")
# current flow betweenness centrality
cur_bet_time = time.time()
nx_current_bet_dict = nx.current_flow_betweenness_centrality(G)
cur_bet_time = time.time() - cur_bet_time
print(f"current flow-between cent calc time : {cur_bet_time: .4f} sec")
# approximation current flow betweenness centrality
aprox_cur_bet_time = time.time()
aprox_nx_current_bet_dict = nx.approximate_current_flow_betweenness_centrality(G)
aprox_cur_bet_time = time.time() - aprox_cur_bet_time
print(f"approx current flow-between cent calc time: {aprox_cur_bet_time: .4f} sec")
print("==" * 30)
# np.corr
bt_curbt_corr = Derive_CORR_from_TWO_CENT_DICT(nx_bet_dict,nx_current_bet_dict)
print(f"CORRELATION of BET and CURRENT_BET : {bt_curbt_corr:7.5f}")
bt_aprcurbt_corr = Derive_CORR_from_TWO_CENT_DICT(nx_bet_dict, aprox_nx_current_bet_dict)
print(f"CORRELATION of BET and APPROX CURRENT_BET: {bt_aprcurbt_corr:7.5f}")
print("==" * 30)
============================================================
exact betweenness cent calc time : 2.5393 sec
current flow-between cent calc time : 19.6670 sec
approx current flow-between cent calc time: 5.1184 sec
============================================================
CORRELATION of BET and CURRENT_BET : 0.99629
CORRELATION of BET and APPROX CURRENT_BET: 0.99044
============================================================
댓글남기기