[英]How to add edges that are conditional to object attribute using igraph and python
The nodes of my graphs are instances that have some methods. 我的图表的节点是具有一些方法的实例。 I have correctly added the nodes, but now I want to check, for those nodes if they have the same group_id, add_edges.
我已正确添加节点,但现在我想检查,如果这些节点具有相同的group_id,则为add_edges。 I am using igraph
我正在使用igraph
import igraph as ig
def making_graph(researchers):
g = ig.Graph()
for each in researchers:
for i in range(len(each)):
g.add_vertex(each[i])
g.vs[i]['researcher_id'] = each[i].get_researcher_id()
g.vs[i]['name'] = each[i].get_name()
g.vs[i]['sex'] = each[i].get_sex()
g.add_edges() # if researchers have the same group_id, add edge
return g
The class for researcher is 研究员的课程是
class Researcher:
def __init__(self, group_id, research_id, name, tit, sex,
tot_nac_2011, tot_nac_2014, tot_int_2011, tot_int_2014, tot_bbl_2011, tot_bbl_2014):
self.group_id = group_id
self.research_id = research_id
self.name = name
self.tit = tit
self.sex = sex
self.producao = [tot_nac_2011, tot_nac_2014, tot_int_2011, tot_int_2014, tot_bbl_2011, tot_bbl_2014]
def get_group_id(self):
return self.group_id
def get_researcher_id(self):
return self.research_id
def get_sex(self):
return self.sex
and when I call making_graph
I pass a list with a list of researchers 当我打电话给
making_graph
我会传递一份包含研究人员名单的清单
EDITED. 编辑。 This also does not work.
这也行不通。 Why?
为什么?
def making_graph(researchers):
g = ig.Graph()
for each in researchers:
for i in range(len(each)):
g.add_vertex(each[i])
g.vs[i]['researcher_id'] = each[i].get_researcher_id()
g.vs[i]['name'] = each[i].get_name()
g.vs[i]['sex'] = each[i].get_sex()
for other in researchers:
for j in range(len(other)):
if each[i].get_group_id() == other[j].get_group_id():
g.add_edge([(g.vs[i], g.vs[j])])
return g
Now, it seems this does work: 现在,似乎这确实有效:
def making_graph(researchers):
g = ig.Graph()
for each in researchers:
for i in range(len(each)):
g.add_vertex(each[i])
g.vs[i]['group_id'] = each[i].get_group_id()
g.vs[i]['researcher_id'] = each[i].get_researcher_id()
g.vs[i]['name'] = each[i].get_name()
g.vs[i]['sex'] = each[i].get_sex()
for v in g.vs:
for w in g.vs:
if v['group_id'] == w['group_id'] and v != w and g.are_connected(v, w) is False:
g.add_edge(v, w)
return g
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.