import networkx as nx
G = nx.Graph()
# 현재 상태에서의 closeness_centrality를 저장하고
prev_cc = nx.closeness_centrality(G)
new_edge = (0, 1)
post_cc = nx.incremental_closeness_centrality(
G=G,
edge=e,# edge => 추가되는 edge 한 개
prev_cc=prev_cc, # prev_cc => edge가 추가되기 전의 closeness_centrality
insertion=True # False이면, edge가 삭제되는 경우를 의미함.
)
import networkx as nx
import numpy as np
import time
import itertools
NODE_SIZE = 50
for EDGE_SIZE in range(100, 600, 50):
G = nx.Graph()
G.add_nodes_from([i for i in range(0, NODE_SIZE)])
# 추가한 EDGE들을 만들고, 섞음.
EDGEs = list(itertools.combinations(G, 2))[:EDGE_SIZE]
np.random.shuffle(EDGEs)
inc_cc_time, cc_time = 0.0, 0.0
start_time = time.time()
prev_cc = nx.closeness_centrality(G)
inc_cc_time += time.time() - start_time
for e in EDGEs:
# CASE 1: nx.incremental_closeness_centrality
start_time = time.time()
prev_cc = nx.incremental_closeness_centrality(G, edge=e,prev_cc=prev_cc, insertion=True)
inc_cc_time += time.time() - start_time
G.add_edge(*e)
# CASE 2: nx.closeness_centrality(G)
start_time = time.time()
cc = nx.closeness_centrality(G)
cc_time += time.time() - start_time
print(
f"EDGE_SIZE: {EDGE_SIZE:3d} - incremental_cc_time: {inc_cc_time:.5f} ::: cc_time: {cc_time:.5f}")
print("--"*50)
EDGE_SIZE: 100 - incremental_cc_time: 0.48296 ::: cc_time: 0.97368
----------------------------------------------------------------------------------------------------
EDGE_SIZE: 150 - incremental_cc_time: 0.62988 ::: cc_time: 1.63518
----------------------------------------------------------------------------------------------------
EDGE_SIZE: 200 - incremental_cc_time: 0.49914 ::: cc_time: 1.78936
----------------------------------------------------------------------------------------------------
EDGE_SIZE: 250 - incremental_cc_time: 0.62656 ::: cc_time: 2.65078
----------------------------------------------------------------------------------------------------
EDGE_SIZE: 300 - incremental_cc_time: 0.71788 ::: cc_time: 3.56669
----------------------------------------------------------------------------------------------------
EDGE_SIZE: 350 - incremental_cc_time: 1.30331 ::: cc_time: 6.24117
----------------------------------------------------------------------------------------------------
EDGE_SIZE: 400 - incremental_cc_time: 1.07049 ::: cc_time: 5.80284
----------------------------------------------------------------------------------------------------
EDGE_SIZE: 450 - incremental_cc_time: 1.14740 ::: cc_time: 7.97550
----------------------------------------------------------------------------------------------------
EDGE_SIZE: 500 - incremental_cc_time: 1.60740 ::: cc_time: 11.66063
----------------------------------------------------------------------------------------------------
EDGE_SIZE: 550 - incremental_cc_time: 1.95668 ::: cc_time: 13.81745
----------------------------------------------------------------------------------------------------
댓글남기기