首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NetworkX同构--如何找到边映射

NetworkX同构--如何找到边映射
EN

Stack Overflow用户
提问于 2022-08-19 18:28:11
回答 2查看 60关注 0票数 2

我有两个具有不同边缘标签的同构图:

代码语言:javascript
复制
G1 = nx.Graph()
G1.add_edges_from([(1,2), (2,3), (3,4), (4,2)])
attrs1 = {(1, 2): {'label': 'Albert'}, (2, 3): {'label': 'Bob'}, (3, 4): {'label': 'Cole'}, (4, 2): {'label': 'Dan'}}
nx.set_edge_attributes(G1, attrs1)

G2 = nx.Graph()
G2.add_edges_from([(13,14), (12,13), (11,12), (14,12)])
attrs2 = {(11, 12): {'label': 'Alice'}, (12, 13): {'label': 'Barbara'}, (13, 14): {'label': 'Cathrine'}, (14, 12): {'label': 'Delia'}}
nx.set_edge_attributes(G2, attrs2)

我使用nx.isomorphism查找节点之间的正确映射()。

代码语言:javascript
复制
GM = isomorphism.GraphMatcher(G1, G2)
GM.is_isomorphic()
print(GM.mapping)
>>> {1: 11, 2: 12, 3: 13, 4: 14}

没有内置的方法可以直接获得边缘之间的映射。在边缘标签之间获取字典最有效的方法是什么?

代码语言:javascript
复制
{'Albert': 'Alice', 'Bob': 'Barbara', 'Cole': 'Cathrine', 'Dan': 'Delia'}

非常感谢大家的建议!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-08-19 19:53:44

以下是@J_H的评论所启发的答案。

让我们重新命名G1的节点,以匹配G2的名称:

代码语言:javascript
复制
node_map = GM.mapping
G1b = nx.relabel_nodes(G1, node_map)

为这两个图创建EdgeView对象。通过这些,可以访问边缘标签。

代码语言:javascript
复制
e1 = G1b.edges()
e2 = G2.edges()

现在,通过对列表的理解,我们获得了期望的输出:

代码语言:javascript
复制
[(e2[j]['label'], e1[j]['label']) for j in list(e1)]

<<< [('Alice', 'Albert'),
<<< ('Barbara', 'Bob'),
<<< ('Delia', 'Dan'),
<<< ('Cathrine', 'Cole')]
票数 0
EN

Stack Overflow用户

发布于 2022-08-19 19:08:04

代码语言:javascript
复制
LOOP E over edges in G1
   GET v1,v2 the nodes connected by E
   GET v1g2,v2g2 the isomorphic vertices in G2 if v1,v2
   GET Eg2 the edge between v1g2,v2g2 in G2
   LABEL Eg2 with E's label
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73420908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档