python-3.xnetworkxmatchingisomorphism

Networkx isomorphism return one-to-one correspondence


With python library networkx it is possible to check for isomorphism using function is_isomorphic(G1, G2) where G1 and G2 are two graphs (https://networkx.github.io/documentation/stable/reference/algorithms/isomorphism.html).

But how to get the one-to-one node correspondence of the isomorphism after having checked that there exists one?

Let's say we perform node matching exclusively.


Solution

  • This is the way to go, that was actually here: https://networkx.github.io/documentation/stable/reference/algorithms/isomorphism.vf2.html

    import networkx as nx
    from networkx.algorithms import isomorphism
    G1 = nx.path_graph(4) # create super simple graphs
    G2 = nx.path_graph(4)
    GM = isomorphism.GraphMatcher(G1,G2)
    GM.is_isomorphic()
    GM.mapping # prints the matching/mapping