【发布时间】:2016-12-22 13:09:21
【问题描述】:
我对 Python 和 igraph 还是很陌生。对于我的学士论文,我必须比较图表,从而确定图表的交集和并集。我尝试了以下方法:
from igraph import *
import json
with open('test_graphs.json') as data_file:
data = json.load(data_file)
test1 = data['test1']
test2 = data['test2']
t1 = Graph(directed=True)
for v in test1:
t1.add_vertex(v)
for v in test1:
for o in test1[v]:
t1.add_edge(v, o)
print(t1)
t2 = Graph(directed=True)
for v in test2:
t2.add_vertex(v)
for v in test2:
for o in test2[v]:
t2.add_edge(v, o)
print(t2)
gr = t1.intersection(t2)
print(gr)
我的json文件在哪里:
{
"test1" : {
"A": ["B","C"],
"B": [],
"C": []
},
"test2" : {
"A": ["B","D"],
"B": [],
"D": []
}
}
我预计交叉点的输出是 A->B。但是出现了以下输出:
IGRAPH DN-- 3 2 --
+ attr: name (v)
+ edges (vertex names):
A->B, A->C
IGRAPH DN-- 3 2 --
+ attr: name (v)
+ edges (vertex names):
A->B, A->D
IGRAPH D--- 3 2 --
+ edges:
2->0 2->1
第一个打印的图表显示,两个输入图表都按预期工作(即使很难,我也不明白“attr”是从哪里来的?)。 但是输出图并不认为我的两个图中的顶点 A 和 B 是相同的,而 C 和 D 是相同的。所以我的问题是:考虑到顶点的标签,我如何确定图形的交集(和模拟并集)。
【问题讨论】:
-
查看这篇文章:*.com/questions/35182255/…
标签: python graph igraph intersection