Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test graph equality in NetworkX

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.

like image 912
Moses Xu Avatar asked Jul 02 '13 14:07

Moses Xu


1 Answers

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
like image 196
Alek Liskov Avatar answered Sep 19 '22 22:09

Alek Liskov