如何获得igraph中两个图之间差异的顶点列表

时间:2016-09-14 22:05:25

标签: python igraph

我试图获取图表1中存在或缺失的顶点列表与另一个图表2.我想知道igraph中是否有任何辅助方法来执行此操作或是否需要建立自己的。谢谢。

1 个答案:

答案 0 :(得分:0)

我不认为图中的内置方法能够满足您的需求。但是,制作自己的内容很简单。

我在这里做出合理的假设,即顶点的唯一标识符是' name' igraph鼓励用户使用的属性。基本上,您在graph1中创建两组顶点名称,在graph2中创建顶点名称并计算它们之间的差异。然后,您可以在graph1中搜索与该名称对应的顶点,并将其添加到列表中以返回:

def find_diff(graph1, graph2):
    """
    Returns list of vertices that are present in graph1 but missing in graph2
    """
    set_v1 = set(graph1.vs['name'])  # set of names of vertices in graph1
    set_v2 = set(graph2.vs['name'])  # set of names of vertices in graph2
    diff = set_v1 - set_v2 
    result = []
    for vname in diff:
        result.append(graph1.vs.find(name=vname))     # find vertex that corresponds to name in graph1 and append it to list

    return result

以上代码是为了清楚起见。实际上,您可以更快,更短的方式来实现。这是一个班轮:

def find_diff(graph1, graph2):
    """
    Returns list of vertices that are present in graph1 but missing in graph2
    """
    return [vx for vx in graph1.vs if vx['name'] not in graph2.vs['name']]