Perform union of graphs based on vertex names Python igraph

走远了吗. 提交于 2019-11-29 11:39:53

Simply make a new graph, and add vertices by name. Of course, this would eliminate other node properties, which you would also have to add manually.

g1 = igraph.Graph()
g2 = igraph.Graph()

# add vertices
g1.add_vertices(["A","B"])
g2.add_vertices(["B","C","D"])

g3 = igraph.Graph()
verts_to_add = []
for v in g1.vs:
    if v['name'] not in verts_to_add:
        verts_to_add.append(v['name'])
for v in g2.vs:
    if v['name'] not in verts_to_add:
        verts_to_add.append(v['name'])

g3.add_vertices(verts_to_add)

for v in g3.vs:
    print(v['name'])

#A
#B
#C
#D
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!