【问题标题】:NetworkX matcher for subgraph isomorphism用于子图同构的 NetworkX 匹配器
【发布时间】:2017-10-11 19:45:49
【问题描述】:

在通过 NetworkX 搜索子图同构时,有没有办法找到节点的映射?例如,

import numpy as np
from networkx.algorithms import isomorphism
import networkx as nx

B = [[0, 2, 1, 0, 0],
     [2, 0, 1, 0, 1],
     [1, 1, 0, 1, 0],
     [0, 0, 1, 0, 0],
     [0, 1, 0, 0, 0]]

A = [[0, 1, 1],
     [1, 0, 2],
     [1, 2, 0]]

G1 = nx.from_numpy_matrix(np.array(B), create_using=nx.MultiGraph())
G2 = nx.from_numpy_matrix(np.array(A), create_using=nx.MultiGraph())
GM = isomorphism.MultiGraphMatcher(G1,G2)
print(GM.subgraph_is_isomorphic())
print(GM.mapping)

打印{0: 0, 1: 1, 2: 2},但不是真的。

【问题讨论】:

  • 对我来说,这会打印 {} 。你用的是什么版本的nx?检查使用nx.__version__
  • @Joel 版本是 2.0
  • 对我来说也一样。你真的运行过这段代码吗?
  • @Joel 发现打印映射前有print(GM.subgraph_is_isomorphic())时,结果不一样..

标签: python match networkx subgraph isomorphism


【解决方案1】:

我找到了解决办法:

GM = isomorphism.MultiGraphMatcher(G1, G2, edge_match=lambda x, y: x[0]['weight'] == y[0]['weight'])

根据源代码文档,应该为多图指定了edge_match函数。

【讨论】:

    猜你喜欢
    • 2015-06-24
    • 2022-08-03
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 2017-07-24
    相关资源
    最近更新 更多