Does isomorphism.GraphMatcher consider attributes?

80 views
Skip to first unread message

Herman Bergwerf

unread,
Mar 20, 2017, 7:35:53 AM3/20/17
to networkx-discuss
I just want to make sure. I need to build a function that compares two molecules (simple small molecules, in an exercise for secondary school students). I wanted to use networkx, build two graphs and then use  isomorphism.GraphMatcher (this way I can also detect when the student to forgot to add a molecule somewhere or added one too much). Is it sufficient to build two graphs using add_edge (with element indices) and add use set_node_attributes to add an element attribute to each node? Would that correctly compare the two molecular graphs?

Thanks!
Herman

Edouard KLEIN

unread,
Mar 21, 2017, 11:37:10 AM3/21/17
to networkx-discuss
Hi !

You need to specify the node_match function:

import networkx
h2o = networkx.Graph()
h2o.add_edge(1, 2)
h2o.add_edge(0, 1)
networkx.set_node_attributes(h2o, 'name', {0: 'H', 1: 'O', 2: 'H'})
co2 = networkx.Graph()
co2.add_edge(1, 2)
co2.add_edge(0, 1)
networkx.set_node_attributes(co2, 'name', {0: 'O', 1: 'C', 2: 'O'})
networkx.isomorphism.is_isomorphic(h2o, co2)  # Returns True, not what you want
networkx.isomorphism.is_isomorphic(h2o, co2, node_match=lambda n1, n2: n1['name'] == n2['name'])  # Returns False, what you want



The arguments to networkx.isomorphism.is_isomorphic are the same as those to the constructor of GraphMatcher.


Cheers,

Edouard.
Reply all
Reply to author
Forward
0 new messages