import networkx as nx
import matplotlib.pyplot as plt
print("--" * 20)
# m: number of row
# n: number of column
(m, n) = 3, 2
# with_positions:
# if True => node_attr에 (x, y)를 추가(default)
# nx에 첨부된 다른 layout들보다, node_attr로부터 position을 가져오는 것이 훨씬 깔끔함.
G = nx.triangular_lattice_graph(
m=m, n=n,
with_positions=True
)
#############################################
# lattice network의 경우
# adjancemey matrix가 diagonal entry로부터
# 크게 벗어나지 않는다.
# lattice adjancecy matrix example:
print("== lattice adjacency matrix")
A = nx.to_numpy_array(G)
print(A)
print("--" * 20)
"""
[[0. 1. 1. 0. 0. 0. 0. 0.]
[1. 0. 1. 1. 0. 0. 0. 0.]
[1. 1. 0. 1. 1. 1. 0. 0.]
[0. 1. 1. 0. 0. 1. 0. 0.]
[0. 0. 1. 0. 0. 1. 1. 0.]
[0. 0. 1. 1. 1. 0. 1. 1.]
[0. 0. 0. 0. 1. 1. 0. 1.]
[0. 0. 0. 0. 0. 1. 1. 0.]]
"""
#############################################
lattice_clustering_coef = nx.average_clustering(G)
print(f"== clustering: {lattice_clustering_coef}")
print("--" * 20)
#############################################
# draw triangular lattice graph.
# triangular lattice graph를 생성할 때,
# `with_positions`를 True(default)로 설정하였다면,
# node_attr에 position이 작성되어 있으므로 가져와서
# layout을 잡아주는 것이 훨씬 깔끔하다.
print("== draw network")
plt.figure()
pos = {n: G.nodes[n]['pos'] for n in G}
nx.draw_networkx(G, pos=pos)
plt.savefig('!tri_lattice_net.png')
print("--" * 20)
----------------------------------------
== lattice adjacency matrix
[[0. 1. 1. 0. 0. 0. 0. 0.]
[1. 0. 1. 1. 0. 0. 0. 0.]
[1. 1. 0. 1. 1. 1. 0. 0.]
[0. 1. 1. 0. 0. 1. 0. 0.]
[0. 0. 1. 0. 0. 1. 1. 0.]
[0. 0. 1. 1. 1. 0. 1. 1.]
[0. 0. 0. 0. 1. 1. 0. 1.]
[0. 0. 0. 0. 0. 1. 1. 0.]]
----------------------------------------
== clustering: 0.6833333333333332
----------------------------------------
== draw network
----------------------------------------
댓글남기기