import networkx as nx
import numpy as np
#G = np.random.seed(0)
N = 10
G = nx.scale_free_graph(N, seed=0)
G = nx.Graph(G)
print("==" * 20)
def custom_CN_with_comm_info(G, u, v):
# Common neighbor와 comm infor를 업데이트.
common_nbrs = list(nx.common_neighbors(G, u, v))
comm_info = 0
for w in common_nbrs:
# u, v, w 의 community가 동일하면 1을 더함.
u_comm = G.nodes[u]['community']
v_comm = G.nodes[v]['community']
w_comm = G.nodes[w]['community']
if u_comm==v_comm==w_comm:
comm_info+=1
return len(common_nbrs) + comm_info
# community 정보를 node_attr에 업데이트해야 함.
nx.set_node_attributes(G, {u:np.random.randint(0, 3, 1)[0] for u in G}, 'community')
print("== nx.cn_soundarajan_hopcroft(G, community='community')")
for u, v, nx_cn_soun_hop in nx.cn_soundarajan_hopcroft(G, community='community'):
assert nx_cn_soun_hop == custom_CN_with_comm_info(G, u, v)
print("Assertion complete")
print("=="*20)
#============================================================================
#============================================================================
#============================================================================
def custom_RA_with_comm_info(G, u, v):
# Resource allocation index with comm info.
xs = []
for w in nx.common_neighbors(G, u, v):
# u, v, w 의 community가 동일하면 1을 더함.
u_comm = G.nodes[u]['community']
v_comm = G.nodes[v]['community']
w_comm = G.nodes[w]['community']
if u_comm == v_comm == w_comm:
xs.append(
1.0/nx.degree(G, w)
)
return sum(xs)
nx.ra_index_soundarajan_hopcroft
print("== nx.ra_index_soundarajan_hopcroft(G, community='community')")
for u, v, nx_ra_soun_hop in nx.ra_index_soundarajan_hopcroft(G, community='community'):
assert nx_ra_soun_hop == custom_RA_with_comm_info(G, u, v)
print("Assertion complete")
print("==" * 20)
댓글남기기