我特别喜欢Python和igraph。对于我的学士论文,我必须比较图表并确定图表交点和联合。我试过如下: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是相同的。所以我的问题:我怎样才能确定图的交集(并模拟联合),考虑我的顶点标签。
看看这个帖子:http://stackoverflow.com/questions/35182255/perform-union-of-graphs-based-on-vertex-names-python-igraph – paqmo