用于子图同构的 NetworkX 匹配器
NetworkX matcher for subgraph isomorphism
有没有办法在通过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}
,但事实并非如此。
我找到了解决方案:
GM = isomorphism.MultiGraphMatcher(G1, G2, edge_match=lambda x, y: x[0]['weight'] == y[0]['weight'])
根据源代码文档,应该有 edge_match 为多图指定的函数。
有没有办法在通过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}
,但事实并非如此。
我找到了解决方案:
GM = isomorphism.MultiGraphMatcher(G1, G2, edge_match=lambda x, y: x[0]['weight'] == y[0]['weight'])
根据源代码文档,应该有 edge_match 为多图指定的函数。