Test graph equality in NetworkX

2019-06-17 08:19发布

What's the most efficient way to test if two NetworkX graphs are identical (i.e. same set of nodes, same node attributes on each node, same set of edges and same edge attributes on each edge)? Suppose that we know the two graphs are of the same class.

Thank you for your kind answer.

1条回答
手持菜刀,她持情操
2楼-- · 2019-06-17 09:05

There's a function in NetworkX called is_isomorphic()

https://networkx.github.io/documentation/stable/reference/algorithms/generated/networkx.algorithms.isomorphism.is_isomorphic.html#networkx.algorithms.isomorphism.is_isomorphic

Here is an example from that page:

>>> import networkx.algorithms.isomorphism as iso
>>> G1 = nx.DiGraph()
>>> G2 = nx.DiGraph()
>>> G1.add_path([1,2,3,4],weight=1)
>>> G2.add_path([10,20,30,40],weight=2)
>>> em = iso.numerical_edge_match('weight', 1)
>>> nx.is_isomorphic(G1, G2)  # no weights considered
True
>>> nx.is_isomorphic(G1, G2, edge_match=em) # match weights
False
查看更多
登录 后发表回答