NetworkX: current flow betwenness centrality.
๐ ๋ชฉ์ฐจ (Contents)
current flow betweenness centrality.
- ์ด์ ์, closeness centrality๋ฅผ ์ด์ผ๊ธฐํ ๋๋,
information centrality๋ผ๋ ์ด๋ฆ์ผ๋ก, ์ธก์ ํ ๊ฒ์ด ์์์ฃ . - betwenness centrality๋ ๋ง์ฐฌ๊ฐ์ง๋ก, ์ ๋ฅ ๋ชจํ์ ์ ์ฉํ์ฌ, graph์์ ์ ๋ณด๊ฐ ํ๋ฅผ ๋, ๊ฐ node๋ฅผ ์ผ๋ง๋ ์ง๋๊ฐ๋์ง๋ฅผ ์ธก์ ํ, current flow betweenness centrality๋ผ๋ ๊ฒ์ด ์์ต๋๋ค. ์ด๋ ๋ค๋ฅธ ๋ง๋ก random-walk betweenness centrality๋ผ๊ณ ํ๊ธฐ๋ ํ์ฃ .
Do it.
- ๋ ๋งํ์ง๋ง,
betweenness centrality๋,current flow betwenness centrality๊ฐ ์์ฒด๋ ํฌ๊ฒ ๋ค๋ฅด์ง ์์ต๋๋ค. - ๊ทธ๋ฐ๋ฐ, current flow์ ๊ฒฝ์ฐ ์๊ฐ์ด ํจ์ฌ ๋ง์ด ๊ฑธ๋ฆฌ๋ค์ ํ . ์ด๊ฑด, ์ด์ ์
current closeness centrality๊ฐcloseness centrality์ ๋นํด์ ํจ์ฌ ์งง์ ์๊ฐ์ด ๊ฑธ๋ ธ๋ ๊ฒ์ ๋นํ๋ฉด, ์กฐ๊ธ ๋ค๋ฅธ ๊ฒฐ๊ณผ์ ๋๋ค. - ํนํ, Node์ ๊ฐ์๋ฅผ 1000๊ฐ๊น์ง ์ฌ๋ฆฌ๋ฉด, ๋ ๊ธฐํ๊ธ์์ ์ธ ์ฐจ์ด๊ฐ ๋์ฃ .
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)
- ์ฆ, ์๋์์ ๋ณด์๋ ๊ฒ์ฒ๋ผ, approximation์ด ๊ทธ๋ฅ
betweeness centrality๋ณด๋ค ๋ ์ค๋ ๊ฑธ๋ฆฝ๋๋ค.
============================================================
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
============================================================
wrap-up
- current flow ์๋ฆฌ์ฆ๋ ๋์์ โ์ ๋ฅ๋ชจํโ์ผ๋ก ๋ชจ๋ธ๋งํ์ฌ ์ฒ๋ฆฌํฉ๋๋ค. ๋ณดํต ์ด๊ฒฝ์ฐ๋ ๋ผํ๋ผ์ค ๋ณํ์ ์ด์ฉํด์, ๊ณ์ฐํ๋ ๊ฒ์ผ๋ก ๊ธฐ์ตํ๋๋ฐ, ํ๋ํ๋ shortest_path๋ฅผ ์ฐพ์ผ๋ฉด์ ์ฒ๋ฆฌํด์ผ ํ๋ ๊ธฐ๋ณธ์ ์ธ betweenness centrality๋ณด๋ค ์ค๋ ๊ฑธ๋ฆฐ๋ค๋ ๊ฒ์ด ์กฐ๊ธ ์ด์ํ๊ฒ ๋๊ปด์ง๋ค์.
reference
GitHub์์ ์ด ๊ธ์ ๋งํฌ๋ค์ด ์๋ณธ ๋ณด๊ธฐ & ์์ ์ ์src/content/posts/2020-02-03-current_flow_betweenness_centrality
๐ ์ฐ๊ด ์ง์ ํ๋ธ:๐ python-libs ์นดํ
๊ณ ๋ฆฌ๐ธ NetworkX ์ปค๋ฆฌํ๋ผ#python#python-libs#networkx