如何获取 igraph 中两个图之间差异的顶点列表
How to get a vertex list of differences between two graphs in igraph
我正在尝试获取图 1 与另一个图 2 中存在或缺失的顶点列表。我想知道 igraph 中是否有任何辅助方法可以执行此操作,或者是否有必要构建一个自己的。谢谢。
我认为 graph 中没有内置方法可以满足您的需求。但是,自己制作很简单。
我将在这里做出合理的假设,即顶点的唯一标识符是 igraph 鼓励用户使用的 'name' 属性。本质上,您制作了两组 graph1 中的顶点名称和 graph2 中的顶点名称并计算它们的差异。然后您可以在 graph1 中搜索与该名称对应的顶点并将其添加到列表中 return:
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']]
我正在尝试获取图 1 与另一个图 2 中存在或缺失的顶点列表。我想知道 igraph 中是否有任何辅助方法可以执行此操作,或者是否有必要构建一个自己的。谢谢。
我认为 graph 中没有内置方法可以满足您的需求。但是,自己制作很简单。
我将在这里做出合理的假设,即顶点的唯一标识符是 igraph 鼓励用户使用的 'name' 属性。本质上,您制作了两组 graph1 中的顶点名称和 graph2 中的顶点名称并计算它们的差异。然后您可以在 graph1 中搜索与该名称对应的顶点并将其添加到列表中 return:
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']]