NetworkX Centrality: local, global reaching centrality.
๐ ๋ชฉ์ฐจ (Contents)
centrality - local reaching centrality.
- โlocal reaching centralityโ๋ ๋ง ๊ทธ๋๋ก, โ์ ๊ทผ๊ฐ๋ฅ์ฑโ์ ํ์ฉํ์ฌ, node์ ์ค์ฌ์ฑ์ ํ๊ฐํฉ๋๋ค. node
u์ local reaching centrality๋u์์ ๊ทธ๋ํ๋ด ๋ชจ๋ ๋ค๋ฅธ ๋ ธ๋๊น์ง์ ๊ฑฐ๋ฆฌ์ ์ญ์๋ฅผ ์ทจํด์ ๊ทธ ํฉ์ ๊ณ์ฐํ๋ ๊ฒ์ด๋๊น์. - ์ด๋ ๊ฒ ์ฐ๊ณ ๋ณด๋ฉด harmonic centrality์ ๋น์ทํด ๋ณด์ด๋๋ฐ, ์์ฃผ ์ฝ๊ฐ์ ์ฐจ์ด๊ฐ ์์ต๋๋ค.
harmonic centrality: ๋ค๋ฅธ ๋ชจ๋ ๋ ธ๋v๋ค๋ก๋ถํฐ target nodeu๊น์ง ๊ฐ๋๋ฐ ํ์ํ ์ต๋จ๊ฑฐ๋ฆฌ์ ์ญ์ ํฉlocal reaching centrality: target nodeu๋ถํฐ ๋ค๋ฅธ ๋ชจ๋ ๋ ธ๋v๊น์ง ๊ฐ๋๋ฐ ํ์ํ ์ต๋จ๊ฑฐ๋ฆฌ์ ์ญ์ ํฉ.
- ๋ฐ๋ผ์, undirected network์์๋ ํฐ ์ฐจ์ด๊ฐ ์์ง๋ง, ๋ฐฉํฅ์ฑ์ด ์๋ directed network์์๋ ๊ฐ์ด ๋ฐ๋๋ก ๋์ค๊ฒ ๋ฉ๋๋ค.
- ๋ค, ๋ฌผ๋ก , ๋ญ ๊ทธ๋ค์ง ํฐ ์๋ฏธ๊ฐ ์๋ ์ฐจ์ด๋ ์๋๋๋ค๋ง.
Compute local reaching centrality
import networkx as nx
import numpy as np
def np_normalize_dict(input_dict):
"""
input_dict: {node_name: centrality_float}์์
value๋ฅผ normalizeํ์ฌ ๋ฆฌํด.
"""
vs = np.array(list(input_dict.values()))
vs /= np.linalg.norm(vs)
return {k: v for k, v in zip(input_dict.keys(), vs)}
def custom_local_reaching_centraliyt(G, target_node):
"""
- local reaching centrality
- target_node์์ ๋ค๋ฅธ ๋ชจ๋ node๊น์ง์ ๊ฑฐ๋ฆฌ์ ๋ํด์ ์ญ์๋ฅผ ์ทจํด์ ํฉํจ.
harmonic centrality: ๋ค๋ฅธ ๋ชจ๋ node v๋ถํฐ target_node u ๊น์ง์ ๊ฑฐ๋ฆฌ์ ์ญ์ ํฉ.
"""
path_ls = [1/nx.shortest_path_length(G, target_node, n) for n in G if target_node!=n]
return sum(path_ls)/(len(G)-1)
# Graph generation
G = nx.DiGraph()
G.add_edges_from([(0, 1), (1, 2)])
# harmonic centrality and normalization
nx_harmonic_cent = nx.harmonic_centrality(G)
nx_harmonic_cent = np_normalize_dict(nx_harmonic_cent)
# global reaching centrality and normalization
nx_node_reaching_centraliy = {n: nx.local_reaching_centrality(G, n) for n in G}
nx_node_reaching_centraliy = np_normalize_dict(nx_node_reaching_centraliy)
for n in G:
print(
f"{n:3d} ::: ",
round(nx_node_reaching_centraliy[n], 8),
round(nx_harmonic_cent[n], 8))
print("=="*20)
- ์๋์์ ๋ณด๋ ๊ฒ์ฒ๋ผ,
local_reaching_centrality์harmonic centrality๋ ์์๊ฐ ๋ฐ๋๋ก ๋์ค๊ฒ ๋จ.
0 ::: 0.89442719 0.0
1 ::: 0.4472136 0.5547002
2 ::: 0.0 0.83205029
wrap-up
nx.global_reaching_centrality๋ ์์ง๋ง, ๊ทธ๋ฅ ํ๊ท ๊ฐ์ ๊ณ์ฐํ ๊ฒ ๊ฐ์์, ์ ์ธํ์์ต๋๋ค.
reference
GitHub์์ ์ด ๊ธ์ ๋งํฌ๋ค์ด ์๋ณธ ๋ณด๊ธฐ & ์์ ์ ์src/content/posts/2020-02-06-local_global_reaching_centrality
๐ ์ฐ๊ด ์ง์ ํ๋ธ:๐ python-libs ์นดํ
๊ณ ๋ฆฌ๐ธ NetworkX ์ปค๋ฆฌํ๋ผ#python#python-libs#networkx
๐กRELATED ARTICLES ยท ํจ๊ป ์ฝ์ผ๋ฉด ์ข์ ์ฐ๊ด ํฌ์คํธ
python-libs
NetworkX: second order centrality
์ฝ์ด๋ณด๊ธฐ→
python-libs
ํ๊ตญ ํํฉ์ฌ ํผ์ณ๋ง ๋คํธ์ํฌ ๋ถ์ - 2ํธ - Data Collection Wisely
์ฝ์ด๋ณด๊ธฐ→
python-libs
ํ๊ตญ ํํฉ์ฌ ํผ์ณ๋ง ๋คํธ์ํฌ ๋ถ์ - 3ํธ - Data Analysis
์ฝ์ด๋ณด๊ธฐ→
python-libs
ํ๊ตญ ํํฉ์ฌ ํผ์ณ๋ง ๋คํธ์ํฌ ๋ถ์ - 1 ~ 3ํธ.
์ฝ์ด๋ณด๊ธฐ→