pythongraphnetworkxisomorphism

Check equality of isomorphic graphs with various vertex labels in NetworkX


I know to check the equality of 2 graphs regardless of the vertex labels, one can use the is_isomorphic() function in NetworkX. However, I have a set of graphs that has fixed number of nodes, edges and connectivity, that is, are all isomorphic to each other, but the labels of the vertices keep changing. In this case, how can I check equality of graphs where all vertex labels also need to be identical?

Each graph looks like below. Each vertex is labeled by a long string. enter image description here


Solution

  • As stated in the documentation of is_isomorphic you can use for this the node_match parameter. It also has two already implemented methods to compare equality based on node attributes: numerical_node_match and categorical_node_match. In both cases you need to supply the name of the node attribute(s) and the default value(s).

    If you directly want to use the node label, i.e. the node id, e.g. 020000010000 in G == True. Then you can either use convert_node_labels_to_integers with label_attribute="label" and afterwards use categorical_node_match("label", None) or create your own node_match function.