简体   繁体   English

计算图中的所有连接节点

[英]Counting all connected nodes in graph

I have a >10k list of (unordered) pairs of numbers. 我有一个> 10k的(无序)数字对列表。 I'd like to classify them into sets of connected pairs either directly or indirectly. 我想将它们直接或间接地分类为连接对的集合。 I think this corresponds to undirected graph. 我认为这对应于无向图。 I'm using python, and tried something like this to represent this structure. 我使用python,并试图像这样来表示这种结构。

In order to know all the numbers connected to i , I can examine whether there is a path from i to j for all j in the list except i . 为了知道与i所有数字,我可以检查列表中除i之外的所有j是否有从ij的路径。 However, with this implementation, the computation time gets too long for the size of list I'm dealing with. 但是,使用此实现,对于我正在处理的列表大小而言,计算时间变得太长。 Is there a more efficient way to do this? 有没有更有效的方法可以做到这一点? (Or is there an already established python libraries?) (或者是否已经建立了python库?)

It sounds as though you are interested in computing the connected components of a graph. 听起来好像您对计算图形的连接组件感兴趣。 I would suggest looking into the networkx package and its tools for computing components . 我建议研究一下networkx软件包及其用于计算组件的工具

For example, suppose our data is a list of pairs of numbers, each pair representing an edge in the graph: 例如,假设我们的数据是数字对的列表,每对数字代表图形中的一条边:

pairs = [
    (1, 2),
    (2, 4),
    (3, 5),
    (2, 5),
    (7, 9),
    (9, 10),
    (8, 7)
]

In the graph represented by these edges, there is a path between any pair of nodes in the set {1, 2, 3, 4, 5} , and there is also a path between any pair of nodes in {6, 7, 8, 9, 10} . 在这些边缘表示的图中,集合{1, 2, 3, 4, 5}任何一对节点之间都有一条路径, {6, 7, 8, 9, 10}任何一对节点之间也都有一条路径{6, 7, 8, 9, 10} But there is no path, say, from 5 to 7 . 但是,例如从57 ,是没有路径的。 This is to say that there are two connected components in the graph. 也就是说,图中有两个连接的组件。

To discover these components, we first import networkx and create a graph: 为了发现这些组件,我们首先导入networkx并创建一个图形:

>>> import networkx as nx
>>> graph = nx.from_edgelist(pairs)

Computing the components is as simple as 计算组件非常简单

>>> list(nx.connected_components(graph))
>>> [{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}]

nx.connected_components is a generator, and so here we converted the result into a list in order to show all of the connected components. nx.connected_components是一个生成器,因此在这里我们将结果转换为一个列表,以显示所有连接的组件。

We can also find the connected component containing a given node: 我们还可以找到包含给定节点的连接组件:

>>> nx.node_connected_component(graph, 3)
{1, 2, 3, 4, 5}

We can also quickly count the number of connected components: 我们还可以快速计算已连接组件的数量:

>>> nx.number_connected_components(graph)
2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM