NetworkX: clique, max independent set
Average clustering coefficient of Graph.
background
- local clustering of each node ๋ ๊ฐ ๋ ธ๋์ ๋ํ clustering(๋ฐ์ง๋)๋ฅผ ๋งํ๋ฉฐ, ํด๋น ๋ ธ๋ ์ด์๋ค๊ณผ ๊ตฌ์ฑํ ์ ์๋ ๋ชจ๋ triangle ์ ๋๋น ์ค์ ์กด์ฌํ๋ triangle์ ๋น์จ์ ๋งํฉ๋๋ค.
- average clustering coefficient๋ ๋ชจ๋ ๋ ธ๋๋ค์ clustering coefficient์ ํ๊ท ์ ๋งํ์ฃ .
approximation of avg. clustering coef
- ์ ํํ ๊ฐ์ ๊ณ์ฐํ๊ธฐ ์ํด์๋ ์กด์ฌํ๋ ๋ชจ๋ ์ผ๊ฐํ์ ์ผ์ผ์ด ํ์ธํ๋ฉด์ ํด๋ ๋์ง๋ง, ๊ทธ๋ฅ ๋ชฌํ ์นด๋ฅผ๋ก ์๋ฎฌ๋ ์ด์ ์ฒ๋ผ ๊ฐ ๋ ธ๋๋ณ๋ก ๊ทธ ์ด์์ ๋๋คํ๊ฒ ๊ณจ๋ผ์, ์ผ๊ฐํ์ด ์กด์ฌํ๋์ง๋ฅผ ์ฒดํฌํ๋ฉด์ ๊ณ์ฐํด๋ ๋ฉ๋๋ค.
- ์ด๋ ์ ๋ trial์ด ๋ณด์ฅ๋์์ ๋, ์ถฉ๋ถํ node์ ์๊ฐ ๋ง๋ค๋ฉด, ํ๊ท ๊ฐ์์ ๊ทธ ์ค์ฐจ๊ฐ ์ค์ด๋ค๊ฒ ๋๊ฑฐ๋ ์. ๊ฒฐ๊ตญ, n์ด ์ฆ๊ฐํ๋ฉด ํ๊ท ์ผ๋ก ํ๊ทํ๊ฒ ๋๋, ๊ธฐ๋ณธ์ ์ธ ํต๊ณ์์ ์ ๋ขฐ๊ตฌ๊ฐ ๋ฌธ์ ์ ๋์ผํฉ๋๋ค.
Do it.
- ์๋ฌดํผ, ํด๋ด
์๋ค. Node๊ฐ 2000๊ฐ ์๋ ๊ทธ๋ํ๋ฅผ ๋์์ผ๋ก
- exact method: ์กด์ฌํ๋ ์ผ๊ฐํ์ ์ผ์ผ์ด ์ธ์ด๊ฐ๋ฉด์, ๊ฐ ๋ ธ๋์ clustering coef๋ฅผ ์ฐพ๊ณ , ๊ทธ ํ๊ท ์ ๊ตฌํจ.
- approx method: ๋ชฌํ ์นด๋ฅผ๋ก ์๋ฎฌ๋ ์ด์ ์ฒ๋ผ ๋๋คํ๊ฒ ์ ํํ๋ฉด์ ๊ฐ์ ๊ณ์ฐ.
- ์ฝ๋์ ์ฝ๋ ์คํ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
import networkx as nx
from networkx.algorithms.approximation import clustering_coefficient
import matplotlib.pyplot as plt
import numpy as np
import time
####################################
# local clustering coefficient:
# ํด๋น ๋
ธ๋๊ฐ ์ด์๋
ธ๋๋ค๊ณผ ์ฐ๊ณํ์ฌ ๋ง๋ค ์ ์๋ ๋ชจ๋ ์ผ๊ฐํ ๋๋น ์ค์ ๋ก ์กด์ฌํ๋ ์ผ๊ฐํ ์
# ------------------------
# average clustering coefficient:
# ๋ชจ๋ node clustering coefficient์ ํ๊ท
####################################
# ์ฌ๊ธฐ์๋ approxmiation์ ํ์ฉํ ๋ฐฉ๋ฒ๊ณผ exact method๋ฅผ ํ์ฉํ ๋ฐฉ๋ฒ์ ๋น๊ต.
# ๋น์ฐํ์ง๋ง, node์ ์๊ฐ ์ปค์ง์๋ก approximation์ ์ ํ๋๊ฐ ์ฆ๊ฐํ๊ณ , ์๋ ์์ ์ด์ ๋ ์ปค์ง.
N = 2000
p = 0.4
G = nx.fast_gnp_random_graph(N, p, seed=0)
#clustering_coefficient.average_clustering(G, trials=10, seed=0)
local_node_coef_lst = []
start_time = time.time()
for n in G:
l = len(G[n])
actual_tri_count = nx.triangles(G, n)
possible_tri_count = l*(l-1)//2
local_node_coef = actual_tri_count / possible_tri_count if possible_tri_count!=0 else 0
local_node_coef_lst.append(local_node_coef)
#print(f"{n} :: {actual_tri_count} :: {possible_tri_count} :: {local_node_coef}")
print(f"local node coef mean : {np.mean(local_node_coef_lst):6.4f}")
exact_method_time = time.time() - start_time
start_time = time.time()
####################################
# clustering_coefficient.average_clustering(G, trials=1000, seed=0)
# -----------------------------------
# trials๋ฅผ ๋์ผ์๋ก ๊ฐ์ด ๋น์ทํด์ง.
# node size๊ฐ 100๊ฐ์ผ ๋, 5000์ผ๋ก ์ธํ
ํ๋๊น ๋น์ทํ๊ฒ ๋์ด.
# ๋ฌผ๋ก , ๋น์ฐํ์ง๋ง, ์ด ๊ฐ์ ์ฌ๋ฆด์๋ก ์๊ฐ ์์์๋ ์ํด๋ฅผ ๋ณด๊ฒ ๋จ.
approx_clustering_coef = clustering_coefficient.average_clustering(G, trials=200, seed=0)
print(f"approx_clustering_coef: {approx_clustering_coef:6.4f}")
approx_method_time = time.time() - start_time
print("--"*20)
print(f"exact_method_time : {exact_method_time:6.2f}")
print(f"approx_method_time: {approx_method_time:6.2f}")
print(
f"approximation is {exact_method_time/approx_method_time:7.2f} times faster than exact method"
)
print("=="*20)
- ์ฝ๋ ์คํ ๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค. Node๊ฐ 2000๊ฐ์ผ ๋์ ๋ํด์ ์ํํ๋๋ฐ, exact method๋, approximation์ด๋ ํฐ ์ฐจ์ด๊ฐ ์์ฃ . ๋ค๋ง ์๋ ์ธก๋ฉด์์๋ ์ง๋์น ์ ๋๋ก approximation์ด ๋น ๋ฆ ๋๋ค. ์ฌ์ค, ์๊ฐํด๋ณด๋ฉด ๋น์ฐํ ๊ฑฐ์ฃ .
local node coef mean : 0.4002
approx_clustering_coef: 0.3950
----------------------------------------
exact_method_time : 164.08
approx_method_time: 0.01
approximation is 14255.15 times faster than exact method
========================================
wrap-up
- ๊ทธ๋ฌ๋๊น, ์ฌ๋งํ๋ฉด ์ ํํ ๊ฐ์ ๊ณ์ฐํด์ผ ํ๋ค๋ ๊ฐ๋ฐ๋๋ฌธ์, ๋๋ฌด ํฐ ๋ฐ์ดํฐ์ ๋ํด์ ๋ฌด๋ฆฌํ์ง ๋ง๊ณ , โ์ด๋ ์ ๋ ๋ณด์ฅ๋๋โ approximation์ ์ฐ๋๋ก ํฉ์๋ค. ์ถฉ๋ถํ ํฐ ๋ฐ์ดํฐ์ ๋ํด์๋, ์ด๋์ ๋ random์ ๊ฐ์ง๊ณ ๊ฒ์ฐํ๋ฉด ์๊ฐ ์์์ ์๋์ ์ธ ์ด์ ์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.
- ๋ฌผ๋ก , ์ฌ๊ธฐ์ ๋งํ๋ โ์ด๋ ์ ๋โ๋ผ๋ ๊ฒ์ ํ๋จํ๋ ค๋ฉด ํต๊ณ์ ์ธ ๋ฐฑ๊ทธ๋ผ์ด๋๊ฐ ํํํด์ผ๊ฒ ์ฃ . ๊ฐ๋ น trial์ 100๋ฒ์ผ๋ก ์ธํ ํ experiment๋ฅผ 50๋ฒ ์ํํ๋ค๋ฉด ๊ทธ ๊ฒฐ๊ณผ๋ ์ด๋ ์ ๋์ ์ ๋ขฐ ๊ตฌ๊ฐ์ ๊ฐ์ง๊ณ ์๋๊ฐ? ์ ๊ฐ์ ์ง๋ฌธ๋ค์ ๋๋ต์ ํ๊ธฐ ์ํด์๋ผ๋, ํต๊ฒ์ ์ง์์ด ํ์ํ์ฃ .
GitHub์์ ์ด ๊ธ์ ๋งํฌ๋ค์ด ์๋ณธ ๋ณด๊ธฐ & ์์ ์ ์src/content/posts/2020-01-31-nx_average_clustering_coefficient
๐ ์ฐ๊ด ์ง์ ํ๋ธ:๐ python-libs ์นดํ
๊ณ ๋ฆฌ๐ธ NetworkX ์ปค๋ฆฌํ๋ผ#python#python-libs#networkx