Search code examples
pythonnetworkxbrute-forceisomorphism

brute-force graph isomorphism with networkx


I'm trying to write a brute-force approach to check if two graphs are isomorphic. I am using the class networkx but I don't want to use the built in functions for isomorphism.
I understand that I have to check all node-permutations of a graph but I don't know how to do that. So how would I permutate nodes in a networkx graph?


Solution

  • The following gives a list of all permutations of the nodes of a graph H.

    from itertools import permutations
    
    list(permutations(H.nodes(), len(H.nodes()))
    

    After that, you could compare their adjacency matrices. See the following code: https://github.com/jgloves/graphTheory/blob/master/are_isomorphic.py