如何使用网络属性图过滤具有特定属性的节点并保留路径 [英] How to filter nodes with specifc attribute and preserve the path using networkx graph

查看:90
本文介绍了如何使用网络属性图过滤具有特定属性的节点并保留路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些天,我正在研究networkx图. 让我们说我有这张图.具有蓝色节点的优先级1,具有黄色节点的优先级2,具有紫色节点的优先级3和具有绿色节点的优先级4.

These days I am working on networkx graph. Let us say I have this graph. Priority 1 with the blue nodes, priority 2 with the yellow nodes, priority 3 with purple nodes, and priority 4 with green nodes.

我想按优先级过滤.例如,第一个优先级1,它应该过滤带有斑点颜色的节点.我想保留蓝色(优先级1)的节点,而忽略其余的节点,它应该看起来像这样.

I would like to filter by priority level. For example the first priority 1, it should filter nodes with blule color. I would like to keep nodes with blue color (priority 1) and ignore the rest, And it should look like this.

对于优先级2,包括优先级节点,它应该显示黄色的节点.保留优先级1(蓝色)和优先级2(黄色).在这里,我应该保留从10到190的路径,因为存在从被忽略的节点开始的路径.它应该看起来像这样.

And for priority 2, including nodes for priority, it should show nodes with the yellow color. Keeping priority 1 (with blue color) and priority 2 (yellow color). Here I should keep the path from 10 to 190 since there is a path from the ignored nodes. And it should look like this.

与优先级3和4相同.例如,对于优先级3,我希望将优先级1、2和3保持在一起,就像我在优先级2上显示的那样.

And the same for priority 3 and 4. For example for priority 3, I would like to keep priority 1, 2, and 3 together as I showed at priority level 2.

这就是我的开始方式.

This how I started.

collector = nx.DiGraph()
for n1, atrr1 in g1.nodes(data ='True'):
    for n2, atrr2 in g1.nodes(data ='True'):
        if ((g1.node[n1]['priority'] ==1) & (g1.node[n2]['priority'] ==2)):

            if (has_path(g1,n1, n2)):
                collector.add_edge(n1,n2)

        if ((g1.node[n2]['priority'] ==2) & (g1.node[n1]['priority'] ==2)):

            if (has_path(g1,n1, n2)):
                collector.add_edge(n1,n2) 


nx.draw(collector, with_labels = True, pos = nx.spring_layout(collector))
plt.rcParams["figure.figsize"] = [6,6]
plt.axis('off')
plt.show()

有人可以帮我用python做到这一点吗?

Can anyone help me to do this in python?

推荐答案

一种方法是计算图形可及性矩阵(如果行a的列b中具有1的方阵,如果可通过图形边缘从节点a到达节点b.您可以在此处中找到实现方法. 然后,您所要做的就是滤除所需的节点,并使用计算出的矩阵(仅遍历与新图形中出现的节点相对应的行和列,并绘制一个1的边缘.

One way to do it would be to compute the graph reachability matrix (square matrix that has a 1 in row a, column b if node b is reachable from node a via the graphs edges). You can find how to implement this here. Then, all you have to do is filter out the nodes you want and re-draw the edges just for the filtered nodes using the computed matrix (by going over only the rows and columns corresponding to nodes that appear in the new graph and drawing an edge where there is a 1).

这篇关于如何使用网络属性图过滤具有特定属性的节点并保留路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆