日常小工具,本人代码水平一般,只是存在这里当笔记。若正好有小伙伴用到,希望能帮助到你,如若有错误之处,欢迎批评指正。
输入:图以及图中一个节点
输出:该节点在图中的一阶,二阶,三阶邻居
import networkx as nx
def find123Nei(G, node):
nodes = list(nx.nodes(G))
nei1_li = []
nei2_li = []
nei3_li = []
for FNs in list(nx.neighbors(G, node)): # find 1_th neighbors
nei1_li .append(FNs)
for n1 in nei1_li:
for SNs in list(nx.neighbors(G, n1)): # find 2_th neighbors
nei2_li.append(SNs)
nei2_li = list(set(nei2_li) - set(nei1_li))
if node in nei2_li:
nei2_li.remove(node)
for n2 in nei2_li:
for TNs in nx.neighbors(G, n2):
nei3_li.append(TNs)
nei3_li = list(set(nei3_li) - set(nei2_li) - set(nei1_li))
if node in nei3_li:
nei3_li.remove(node)
return nei1_li, nei2_li, nei3_li
h = nx.Graph()
h.add_nodes_from(list(range(1, 8)))
h.add_edges_from([(1, 2), (1, 3), (1, 5), (1, 4), (2, 8), (2, 6), (3, 6), (4, 7)])
neighbors = find123Nei(G, 1)
print(neighbors[0]) # 输出节点的一阶邻居
print(neighbors[1]) # 输出节点的二阶邻居
print(neighbors[2]) # 输出节点的三阶邻居
输出结果如下: [2, 3, 5, 4], [6, 7, 8, 9, 10], [11, 12, 13]