我们使用Python查找两个图G1和G2的匹配:
from networkx.algorithms import isomorphism
GM = isomorphism.GraphMatcher(G1, G2)
matchings_list = list(GM.subgraph_isomorphisms_iter())例如,matchings_list是{'4':'0','11':'1','8':'2',‘7’},{'4':'0','11':'1','15':'2','6':'3'}。
matchings_list中的每个匹配都是一个字典,其键是G1的顶点,值是G2的顶点。
如果我们想获得G1的顶点,可以很容易地访问matching.keys()。
但是,我们如何能够简单地访问G1的边缘?
访问它们的方法很长,就是用G2边遍历G2的边缘,并从匹配中获得相应的G1顶点(创建匹配的反向字典,因为键是G1的顶点),并创建一个元组来生成边缘。
发布于 2020-11-15 17:50:40
但是,使用G1.subgraph(matching.keys()).edges的速度要比漫长的路慢:
in_time = time.perf_counter()
g1_edge_list = []
for m in matchings:
m2 = {y: x for x, y in m.items()}
for e1, e2 in G2.edges:
g1_edge_list.append((m2[e1], m2[e2]))
out_time = time.perf_counter() - in_time
print(out_time)
in_time2 = time.perf_counter()
g1_edge_list = []
for m in matchings:
g1_edge_list += graph.subgraph(m.keys()).edges
out_time_2 = time.perf_counter() - in_time2
print(out_time_2)它大约慢了12倍。@Timus
https://stackoverflow.com/questions/64843968
复制相似问题