# (0, 1), (1, 2)의 edge를 가진 기본적인 Graph 생성
G = nx.Graph()
G.add_edges_from([(0, 1), (1, 2)])
####################################
# first method: `Gneighbors`
# type: <class 'dict_keyiterator'>
print(f"type: {type(G.neighbors(1))}")
print(0 in G.neighbors(1))
print(0 in G.neighbors(2))
print("--")
####################################
# second method: `G[node_name]`
# type: <class 'networkx.classes.coreviews.AtlasView'>
print(f"type: {type(G[1])}")
print(0 in G[1])
print(0 in G[2])
####################################
def CASE1_neighbors(input_G):
# G.neighbors를 사용하여 연결성을 체크
start_time = time.time()
for _ in range(0, loop_n):
n1, n2 = np.random.randint(0, len(input_G), 2)
# G.neighbors(n2): <class 'dict_keyiterator'>
# iterator로 매번 하나씩 쭉 읽으면서 처리함.
n1_is_neighbor_of_n2 = n1 in input_G.neighbors(n2)
return time.time() - start_time
def CASE2_Atlasview(input_G):
# G[]를 사용하여 연결성을 체크
start_time = time.time()
for _ in range(0, loop_n):
n1, n2 = np.random.randint(0, len(input_G), 2)
# <class 'networkx.classes.coreviews.AtlasView'>
n1_is_neighbor_of_n2 = n1 in input_G[n2]
return time.time() - start_time
def CASE3_dict_of_dict(input_G):
# Case2에서 사용하는 `AtlasView`가 dictionary of dictionary 구조라면,
# 동일한 형태로, 실제 dictionary로 node들간의 관계를 변형한다음, 접근해도 동일한 결과가 나와야함.
G_dict_of_dict = {n1: {n2: {} for n2 in input_G[n1]} for n1 in input_G}
start_time = time.time()
for _ in range(0, loop_n):
n1, n2 = np.random.randint(0, len(input_G), 2)
# node to node 구조로 만든 딕셔너리에 접근하여 연결성을 확인함.
n1_is_neighbor_of_n2 = n1 in G_dict_of_dict[n2]
#print(n1_is_neighbor_of_n2)
return time.time() - start_time
import networkx as nx
import numpy as np
import time
G = nx.Graph()
np.random.seed(0)
p = 0.6 # complete graph 대비 edge의 비율
loop_n = 10**4 # 이 값을 높일수록 더 무작위로 넓은 범위의 값들에 접근하므로, 신뢰도가올라감.
print("==" * 30)
result_dict = {}
for N in range(400, 5000, 400):
G = nx.fast_gnp_random_graph(N, p, seed=0)
#print(n1, n2)
# Case 1의 시간 저장.
CASE1_time = CASE1_neighbors(G)
# Case 2의 시간 저장.
CASE2_time = CASE2_Atlasview(G)
# Case 3의 시간 저장.
CASE3_time = CASE3_dict_of_dict(G)
# 결과를 배수로 표현하여 출력.
print(
f"N: {N:4d} complete, CASE2 is {CASE1_time/CASE2_time:7.2f} times faster than CASE1"
)
print(
f"N: {N:4d} complete, CASE2 is {CASE3_time/CASE2_time:7.2f} times faster than CASE3"
)
print("--"*20)
print("=="*30)
============================================================
N: 400 complete, CASE2 is 2.36 times faster than CASE1
N: 400 complete, CASE2 is 0.85 times faster than CASE3
----------------------------------------
N: 800 complete, CASE2 is 3.91 times faster than CASE1
N: 800 complete, CASE2 is 0.88 times faster than CASE3
----------------------------------------
N: 1200 complete, CASE2 is 5.49 times faster than CASE1
N: 1200 complete, CASE2 is 0.88 times faster than CASE3
----------------------------------------
N: 1600 complete, CASE2 is 7.07 times faster than CASE1
N: 1600 complete, CASE2 is 0.85 times faster than CASE3
----------------------------------------
N: 2000 complete, CASE2 is 8.72 times faster than CASE1
N: 2000 complete, CASE2 is 0.94 times faster than CASE3
----------------------------------------
N: 2400 complete, CASE2 is 10.55 times faster than CASE1
N: 2400 complete, CASE2 is 1.00 times faster than CASE3
----------------------------------------
N: 2800 complete, CASE2 is 11.94 times faster than CASE1
N: 2800 complete, CASE2 is 1.00 times faster than CASE3
----------------------------------------
N: 3200 complete, CASE2 is 11.89 times faster than CASE1
N: 3200 complete, CASE2 is 1.16 times faster than CASE3
----------------------------------------
N: 3600 complete, CASE2 is 12.65 times faster than CASE1
N: 3600 complete, CASE2 is 1.84 times faster than CASE3
----------------------------------------
N: 4000 complete, CASE2 is 16.58 times faster than CASE1
N: 4000 complete, CASE2 is 7.80 times faster than CASE3
----------------------------------------
N: 4400 complete, CASE2 is 18.90 times faster than CASE1
N: 4400 complete, CASE2 is 6.03 times faster than CASE3
----------------------------------------
N: 4800 complete, CASE2 is 18.76 times faster than CASE1
N: 4800 complete, CASE2 is 10.55 times faster than CASE3
----------------------------------------
============================================================
댓글남기기