我有一个M边缘定义的numpy数组,它是Mx2并包含节点索引。对于每个节点,我想在节点的n环(在我的例子中为3环)内找到边缘。对于商业模型,我有一个中等密度计算网格的N = 250K节点,所以我正在寻找适当有效和稀疏的东西。
答案 0 :(得分:0)
import numpy as np
import networkx as nx
G = nx.dorogovtsev_goltsev_mendes_graph(4)
edges_list = G.edges()
edges_list = np.array(edges_list)
one_ring = []
for n in G.nodes():
first = edges_list[edges_list[:, 0] == n, 1]
second = edges_list[edges_list[:, 1] == n, 0]
one_ring.append(np.hstack([first, second]))
N = 3
n_ring = []
def find_n_ring(root, node, i):
if i >= N:
return node
for node2 in one_ring[node]:
n_ring_node = find_n_ring(root, node2, i + 1)
if n_ring_node is not None and n_ring_node not in n_ring[root]:
n_ring[root].append(n_ring_node)
for root in G.nodes():
n_ring.append([])
find_n_ring(root, root, 0)
print(n_ring)