f_
frhyme.code
•python-libs•โฑ ์•ฝ 7๋ถ„ ์†Œ์š”

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๋ฒˆ ์ˆ˜ํ–‰ํ–ˆ๋‹ค๋ฉด ๊ทธ ๊ฒฐ๊ณผ๋Š” ์–ด๋А ์ •๋„์˜ ์‹ ๋ขฐ ๊ตฌ๊ฐ„์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”๊ฐ€? ์™€ ๊ฐ™์€ ์งˆ๋ฌธ๋“ค์— ๋Œ€๋‹ต์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋ผ๋„, ํ†ต๊ฒŒ์  ์ง€์‹์ด ํ•„์š”ํ•˜์ฃ .

๐Ÿ’กRELATED ARTICLES ยท ํ•จ๊ป˜ ์ฝ์œผ๋ฉด ์ข‹์€ ์—ฐ๊ด€ ํฌ์ŠคํŠธ

๊ธ€ ์ฃผ์†Œ๊ฐ€ ํด๋ฆฝ๋ณด๋“œ์— ๋ณต์‚ฌ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.