Perform union of graphs based on vertex names Python igraph

前端 未结 1 559
庸人自扰
庸人自扰 2020-12-19 21:28

This issue has been filed on github something like 6 months ago, but since it has not yet been fixed I\'m wondering whether there is a quick fix that I am missing.

I

相关标签:
1条回答
  • 2020-12-19 21:35

    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
    
    0 讨论(0)
提交回复
热议问题