请看EDIT下的部分!
我认为其他选项更通用,从程序化的角度来看可能更好。我只是快速了解了如何使用 numpy 以非常简单的方式获取列表。
首先创建邻接矩阵,你的节点列表是一个数组:
import numpy as np
node_list= np.random.randint(10 , size=(10, 2))
A = np.zeros((np.max(node_list) + 1, np.max(node_list) + 1)) # + 1 to account for zero indexing
A[node_list[:, 0], node_list[:, 1]] = 1 # set connected nodes to 1
x, y = np.where(A == 0) # Find disconnected nodes
disconnected_list = np.vstack([x, y]).T # The final list of disconnected nodes
不过,我不知道这将如何与真正的大规模网络一起工作。
编辑:上面的解决方案是我想得太快了。截至目前,上述解决方案提供了节点之间的缺失边,而不是断开的节点(在有向图的情况下)。此外,disconnected_list 包含每个节点两次。这是解决方案的第二个想法:
import numpy as np
node_list= np.random.randint(10 , size=(10, 2))
A = np.zeros((np.max(node_list) + 1, np.max(node_list) + 1)) # + 1 to account for zero indexing
A[node_list[:, 0], node_list[:, 1]] = 1 # set connected nodes to 1
A[node_list[:, 1], node_list[:, 0]] = 1 # Make the graph symmetric
A = A + np.triu(np.ones(A.shape)) # Add ones to the upper triangular
# matrix, so they are not considered in np.where (set k if you want to consider the diagonal)
x, y = np.where(A == 0) # Find disconnected nodes
disconnected_list = np.vstack([x, y]).T # The final list of disconnected nodes