NetworkX: Node Connectivity.
๐ ๋ชฉ์ฐจ (Contents)
networkx - approximation for NODE connectivity
- Graph๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๋น ๋ฅด๊ฒ ์ฒ๋ฆฌํ๋ ๊ฒ์ด ์ด๋ ต์ต๋๋ค. ํ ์ด๋ธ๊ณผ ๊ฐ์ ํํ๋ผ๋ฉด, ๋น๊ต์ ์ด๋ ์ ๋ ๋ณ๋ ฌ์ ์ผ๋ก ์ฒ๋ฆฌํ ์ ์๋๋ฐ(์๋ก ๋ฐ์ดํฐ๊ฐ ๋ ๋ฆฝ์ ์ด๊ธฐ ๋๋ฌธ), Graph๋ ๋ชจ๋ ์ฐ๊ฒฐ๋์ด ์์ผ๋ฏ๋ก ๊ทธ๋ ์ง ์์ฃ . ๋ฐ๋ผ์, ํ์์ ๋ฐ๋ผ์, approximation์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๋ค์ด ์์ต๋๋ค. ๊ทธ์ธ๋ก, graph2vec, node2vec ๋ฑ์ ๋ฐฉ๋ฒ๋ก ๋ํ approximation์ ๋ฐฉ๋ฒ ์ค ํ๋์ฃ (๋ฌผ๋ก , ์ด๋ค ์ฌ๋๋ค์ ์ด ๋ฐฉ๋ฒ์ approximation์ด ์๋๋ผ๊ณ ํ ์ ์์ต๋๋ค๋ง, ์ ๋ ๊ทธ๋ ๊ฒ ์๊ฐํด์.)
- ๋ณธ ํฌ์คํธ์์๋
local node connectivity๋ฅผ ๋๋ต์ ์ผ๋ก ๊ณ์ฐํ๊ธฐ ์ํดnetworkx์ ๋ด์ฌ๋ approximation method๋ฅผ ์ฌ์ฉํฉ๋๋ค.
local node connectivity
- pairwise node connectivity ๋ผ๊ณ ๋งํ๊ธฐ๋ ํ๋๋ฐ์. ์๋ฅผ ๋ค์ด์ ์ค๋ช
ํ๋ ๊ฒ ๋ ์ฌ์์.
- ๊ฐ๋ น
node_A,node_B๊ฐ ์๋ค๊ณ ํฉ์๋ค(๊ธฐ๋ณธ์ ์ผ๋ก๋, ๋ ๋ ธ๋๊ฐ ์ธ์ ํ์ง ์๊ณ (non-adjacent), ์๋ก ๋ค๋ฅด๋ค๋ ๊ฒ์ ์ ์ ํฉ๋๋ค). - ์ด ๋, ๋ ๋
ธ๋ ๊ฐ์ ๊ด๊ณ๋ฅผ disconnected๋ก ๋ง๋ค๊ธฐ ์ํด์๋ ์ต์ํ
n๊ฐ์ ๋ ธ๋๋ฅผ ์ ๊ฑฐํด์ผ, ํ๋ค๊ณ ํฉ์๋ค. - ์ด ๋,
n์ด ๋ฐ๋ก ์ด ๋ ๋ ธ๋๊ฐ์ connectivity๊ฐ ๋ฉ๋๋ค.minimum separating cutset์ด๋ผ๊ณ ํ๊ธฐ๋ ํ์ฃ .
- ๊ฐ๋ น
- ๋ํ, ์ด ๊ฐ์ ๋ฉ์ฐ ์ ํํ๊ฒ, Mengerโs theorem์ ์ํด์, โpath ๋ค๊ฐ์ ๊ณตํต์ธ ๋
ธ๋๊ฐ ์์ source์ target๊ฐ์ ๋ชจ๋ pathโ, ์ฆ
node_disjoint_path์ ์์ ๋์ผํฉ๋๋ค. ์ด๊ฑด ์กฐ๊ธ ์๊ฐํด๋ณด๋ฉด ๋งค์ฐ ๋น์ฐํด์, ํท๊ฐ๋ฆด ๊ฒฝ์ฐ ํ๋ฒ ๊ทธ๋ฆผ์ ๊ทธ๋ ค๋ณด์๋ฉด ๋ช ํํด์ง ๊ฑฐ์์. - ๊ทธ๋ฆฌ๊ณ , ๋ชจ๋ node pair ๋ค๊ฐ์ connectivity๋ค ์ค์์ ๊ฐ์ฅ ์์ ๊ฐ์, ๋น์ฐํ์ง๋ง, (global) node connectivity๊ฐ ๋ฉ๋๋ค. ๊ทธ๋ฅ node connectivity๋ โํด๋น Graph๊ฐ ๋ถ๋ฆฌ๋๊ธฐ ์ํด ํ์ํ ๊ฐ์ฅ ์ ์ ์์ ๋ ธ๋๋ฅผ ๋งํ๋๊น์โ.
do it.
- ๋ ์๋ก ์ด ๊ธธ์์ต๋๋ค. ๊ทธ๋ฅ ํด๋ณด๋ฉด ์ฝ์ฃ ๋ ๋งํ์ง๋ง ์ฌ์์.
import networkx as nx
from networkx.algorithms.approximation import connectivity
import time
N = 40 # node size
p = 0.6 # edge proportion of Complete graph
G = nx.fast_gnp_random_graph(N, p, seed=0)
print("=="*30)
print(f"Node size: {N}, Edge size: {len(G.edges())}")
print("--" * 30)
print("== Start")
start_time = time.time()
MIN_global_node_connectivity = len(G)
WRONG_connectivity_count = 0
# G:nx.Graph ์ ๋ชจ๋ node pair๋ค์ local connnectivity๋ฅผ approximationํ๋ค.
for n1, d1 in connectivity.all_pairs_node_connectivity(G).items():
for n2, approx_c in d1.items():
if MIN_global_node_connectivity>=approx_c:
MIN_global_node_connectivity = approx_c
# `local_node_connectivity`๋ ํน์ source, target์ ๋ํด์๋ง ๊ณ์ฐํด์ฃผ๋ ํจ์
# ์ฅ๋ ์ผ์ assert๋ฅผ ๋ฃ๊ธฐ๋ ํ์ง๋ง, ํ๋ฆด ๋ฆฌ๊ฐ ์๋ค.
assert approx_c == connectivity.local_node_connectivity(G, n1, n2)
# ์์ ๋งํ ๊ฒ๊ณผ ๊ฐ์ด, local_node_connectivity๋ ๋ ๋
ธ๋๊ฐ์ disjoint_path์ ์์ ๋์ผํด์ผ ํ๋ค.
disjoint_paths = nx.algorithms.connectivity.node_disjoint_paths(G, n1, n2)
disjoint_path_count = 0
for _ in disjoint_paths:
disjoint_path_count+=1
# `disjoint_path_count`๊ฐ ์ ํํ node connectivity์ด๋ฏ๋ก ์ถ๋ ฅํจ.
if approx_c!=disjoint_path_count:# Wrong
print(
f"WRONG - {n1:3d} => {n2:3d} - approx: {approx_c:3d} - disjoint_path_count: {disjoint_path_count:3d}"
)
WRONG_connectivity_count+=1
# graph์ node connectivity์ all_pairs_node_connectivity์ ์ต์๊ฐ์ ๋น์ฐํ ๊ฐ์์ผ ํจ.
assert connectivity.node_connectivity(G) == MIN_global_node_connectivity
print("--" * 30)
print(f"Accuracy: {1-WRONG_connectivity_count/(N*(N-1)):%}")
print(f"== complete with comp. time: {time.time()-start_time:6.2f} sec")
print("==" * 30)
- ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค. ์ฆ, approximation์ด๊ธฐ๋ ํ๋ฐ, ์กฐ๊ธ ์๋ฆ ๋์ ์ ๋๋ก ์ ํํฉ๋๋ค.
============================================================
Node size: 40, Edge size: 456
------------------------------------------------------------
== Start
WRONG - 7 => 14 - approx: 21 - disjoint_path_count: 22
WRONG - 14 => 7 - approx: 21 - disjoint_path_count: 22
------------------------------------------------------------
Accuracy: 99.871795%
== complete with comp. time: 20.81 sec
============================================================
wrap-up
- local connectivity๊ฐ ๋ค๋ฅธ ๋ ธ๋๋ค์ ๋นํด์ ๋๋ค๋ฉด, ๊ทธ ๋ ๋ ธ๋๋ ์๋ง๋ ์ข ๋ ๋ฐ์ง๋ ๊ด๊ณ๋ผ๊ณ , ์ค๋ช ํ ์ ์๊ฒ ์ฃ . ๊ทธ ๋ ๊ด๊ณ๋ฅผ ๋จ์ด๋จ๋ฆฌ๋ ค๋ฉด ์ ๊ฑฐํด์ผ ํ๋ ๋ ธ๋์ ์๊ฐ ๋ง์ผ๋๊น์. ๊ทธ๋ฆฌ๊ณ , ์ด๋ betweenness centrality์๋ ์ ์ฌํ ๊ฐ๋ ์ผ๋ก, ๋๊ปด์ง๊ธฐ๋ ํด์.
GitHub์์ ์ด ๊ธ์ ๋งํฌ๋ค์ด ์๋ณธ ๋ณด๊ธฐ & ์์ ์ ์src/content/posts/2020-01-30-nx_connectivity_approximation
๐ ์ฐ๊ด ์ง์ ํ๋ธ:๐ python-libs ์นดํ
๊ณ ๋ฆฌ๐ธ NetworkX ์ปค๋ฆฌํ๋ผ#python#python-libs#networkx