Python中的NetworkX:仅连接值而不连接键 [英] NetworkX in Python: Connect only values not keys

查看:116
本文介绍了Python中的NetworkX:仅连接值而不连接键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处面临着类似的问题,数据几乎与所述相同:

I face a similar problem to the SO question here, with almost the same data as described:

graph = {
    '1': ['2', '3', '4'],
    '2': ['5','11','12','13','14','15'],
    '3' : ['6','7','66','77'],
    '5': ['6', '8','66','77'],
    '4': ['7','66','77'],
    '7': ['9', '10']
}

但是,如果我将此图形数据输入到链接脚本中,它将把数据的键与值连接起来(例如,"1"(第一行的键)与"2"(第二行的键)连接数据集中的下一行).
举例说明:我想将第一行的"1"与第一行的2,3,4连接,而不是第二行的2.此外,我想将所有值相互连接,例如2与3与4与4与5与11等等.

However, if I feed this graph data into the linked script, it connects the keys of the data with values (e.g. "1" (key of the first row) is connected with "2" (key of the second row) of the next line in the data set).
Clearing this up, at an example: I want to connect "1" of the first row with 2,3,4 of the first row, but not with 2 of the second row. Furthermore I want to connect all the values with each other, like 2 with 3 with 4 with 5 with 11 and so on.

这清楚吗?

应保留链接脚本的所有其余功能(例如,通过节点的编号边,节点标签等来调整节点的圆圈大小).

All the remaining features of the linked script should be kept (such as adjusting the circle size of the nodes by their number edges, node labelling, etc).

编辑
感谢韦恩·沃纳(Wayne Werner),我在解释中发现了一个错误:考虑上述数据集中的以下数据:

EDIT
Thanks to Wayne Werner, I noticed an error in my explanation: Consider the following data of the data set stated above:

'3' : ['6','7','66','77'],
'5': ['6', '8','66','77'],

在这种情况下,我想将3与6,7,66,77连接起来,而不要将3与5连接起来.
此外,我想将5与6,8,66,77连接起来.

In this case I want to connect 3 with 6,7,66,77 but not 3 with 5.
Furthermore I want to connect 5 with 6,8,66,77.

推荐答案

您要问的是可能的,我想您只是对NetworkX图的工作方式感到困惑.将节点添加到图形时,您传入一个ID来表示该节点.在内部,NetworkX使用此值来跟踪单个节点.将两个节点连接在一起时,您传递两个ID(代表节点)进行连接.

What you're asking is possible, I think you're just confused about how NetworkX graphs work. When you add a node to the graph, you pass in an ID to represent that node. Internally NetworkX uses this value to track individual nodes. When connect two nodes together, you pass two ids (representing nodes) to connect.

在对问题的解释中,您希望NetworkX两次接受相同的ID,但是忘记"该ID已经代表一个节点.那不可能.

In your explanation of your problem you are wanting NetworkX to accept the same ID twice, but 'forget' that ID already represents a node. That's not possible.

您有两个选择:

  1. 分别绘制单个图形(这使您可以随意重复使用ID)
  2. 为每个子图创建唯一的ID,但更改节点上的标签.

以下是第二种方法的示例.此处graph词典中的键用于为每个子图创建唯一的ID集,例如1_21_31_4等.我们将唯一ID与标签一起存储,并在创建绘图时应用它们.这样,看起来就像多个节点具有相同的ID,即使它们没有相同.

The following is an example of the second approach. Here the key in the graph dictionary is used to create a unique ID set for each subgraph, e.g. 1_2, 1_3, 1_4, etc. We store the unique IDs with the label, and apply these when creating the plot. That way it looks like multiple nodes have the same ID, even though they do not.

import networkx
from itertools import combinations

graph = {
    '1': ['2', '3', '4'],
    '2': ['5','11','12','13','14','15'],
    '3' : ['6','7','66','77'],
    '5': ['6', '8','66','77'],
    '4': ['7','66','77'],
    '7': ['9', '10']
}

g = networkx.Graph()
labels = {}

for k, vs in graph.items():
    vs.append(k)
    for a, b in combinations(vs, 2):
        # We're creating multiple 'subnetworks', so prefix the ids
        node_a = '%s_%s' % (k, a)
        node_b = '%s_%s' % (k, b)

        g.add_edge(node_a,node_b)

        # Store labels
        labels[node_a] = a
        labels[node_b] = b

# Draw the graph, replacing the labels to hide the unique IDs            
networkx.draw_spring(g, labels=labels)

这将产生以下图像,因为您可以看到多个节点具有相同的标签,即使它们在后台具有不同的ID.

This produces the following image, as you can see multiple nodes have the same label, even though behind the scenes they have distinct IDs.

这篇关于Python中的NetworkX:仅连接值而不连接键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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