简体   繁体   English

当一个属性相同时连接图中的节点(NetworkX)

[英]Connect nodes in a graph when one attribute is the same (NetworkX)

I want to create a graph which automatically adds edges between nodes if one particular attribute is the same. 我想创建一个图,如果一个特定的属性相同,该图将自动在节点之间添加边。 Nodes in my graph represent students. 我图中的节点代表学生。 I am adding two attributes to my nodes: university_id and full_name . 我向节点添加了两个属性: university_idfull_name I want to add an edge between two people only if they go to the same university. 我只想在两个人上同一所大学时在他们之间增加优势。

I've been looking at this solution: NetworkX: add edges to graph from node attributes 我一直在研究此解决方案: NetworkX:从节点属性向图形添加边

From testing it, it seems that this solution connects all edges of a graph, whether or not any of the attributes are the same. 通过测试,似乎此解决方案将图形的所有边连接起来,而无论任何属性是否相同。 Is there a simple solution I can use to connect students only based on their university_id ? 有没有一种简单的解决方案可以仅根据学生的university_id来联系学生?

Here is my code: 这是我的代码:

import matplotlib.pyplot as plt
import networkx as nx
import MySQLdb

# #############################################################################
# Retrieve the data from remote server.
myDB = MySQLdb.connect(host="*,port=3306,user="mysql",passwd="***")
cHandler = myDB.cursor()
cHandler.execute("USE research_project")
cHandler.execute("SELECT * FROM students")
results = cHandler.fetchall()

G = nx.Graph()
for items in results:
        # items[0] is a unique ID, items[1] = university_id, items[2] = full name
        G.add_node(items[0], attr_dict={'university_id': items[1], 'full_name': items[2]})

for node_r, attributes in G.nodes(data=True):
    key_set = set(attributes.keys())
    G.add_edges_from([(node_r, node) for node, attributes in G.nodes(data=True)
                      if key_set.intersection(set(attributes.keys()))
                      and node != node_r])

nx.draw(G)
plt.show()
from __future__ import print_function
import matplotlib.pyplot as plt
import networkx as nx
import MySQLdb


# #############################################################################
# Retrieve the data from remote server.
myDB = MySQLdb.connect(host="*,port=3306,user="mysql",passwd="***")
cHandler = myDB.cursor()
cHandler.execute("USE research_project")
cHandler.execute("SELECT * FROM students")
results = cHandler.fetchall()

G = nx.Graph()
for items in results:
    G.add_node(items[0], attr_dict={'university_id': items[1], 'full_name': items[2]})

for node_r in G.nodes(data=True):
    for node in G.nodes(data=True):
        if node != node_r and node[1]['attr_dict']['university_id'] == node_r[1]['attr_dict']['university_id']:
            G.add_edge(node[0], node_r[0], attr_dict=None)

nx.draw(G, with_labels=True)
plt.show()

I tested the above on small sets of data and it appears to work. 我在少量数据上测试了上面的方法,它似乎可以工作。 I have a hunch that what was happening had to do with the way I was adding attributes to the nodes. 我有一种预感,发生的事情与我向节点添加属性的方式有关。

The caveat with the above solution is that it's TREMENDOUSLY slow at runtime. 上述解决方案的警告是,运行时异常缓慢。 I will update my answer whenever I can come up with a faster solution. 只要有更快的解决方案,我都会更新答案。

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

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