0

I am trying to compare if two graphs are isomorphic using networkx. However, I found a very weird problem that got me confused for days. I have a function get_graph that takes the nodes, edge-starts and edge-ends as inputs. When I was debugging, I added a line print(list(edges)) to print out the zipped edges in the function. Then I was shocked to find that this somehow changes the result of isomorphism. If you try my code below, you will find out that the two graphs are isomorphic if you add that line, but is not isomorphic when commenting out.

I don't think this has anything to do with networkx or graphs in general. This should be considered as weird behavior of the print function. As far as I am concerned, the print function should never change the result of a function. Anyone has any idea?

import numpy as np
import networkx as nx
import networkx.algorithms.isomorphism as iso

def get_graph(nodes, edge_starts, edge_ends):
    G = nx.Graph()
    G.add_nodes_from(nodes)
    edges = zip(edge_starts, edge_ends)

#    print(list(edges))

    G.add_edges_from(edges)
    return G

nodes1 = [(0, {'symbol': 'Ni'}), (1, {'symbol': 'Pt'}), (2, {'symbol': 'Pt'}), (3, {'symbol': 'Pt'}), (4, {'symbol': 'Ni'}), (5, {'symbol': 'Pt'}), (6, {'symbol': 'Pt'}), (7, {'symbol': 'Pt'}), (8, {'symbol': 'Ni'}), (9, {'symbol': 'Pt'}), (10, {'symbol': 'Pt'}), (11, {'symbol': 'Pt'}), (12, {'symbol': 'Ni'}), (13, {'symbol': 'Pt'}), (14, {'symbol': 'Pt'}), (15, {'symbol': 'Pt'}), (16, {'symbol': 'CH3'}), (17, {'symbol': 'OH'}), (18, {'symbol': 'CO'})]
starts1 = [1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 16, 17, 17, 18, 18, 18]
ends1 = [0, 0, 1, 1, 2, 1, 3, 0, 4, 3, 4, 5, 0, 2, 5, 6, 2, 3, 3, 6, 8, 0, 5, 8, 9, 0, 1, 9, 10, 6, 7, 9, 11, 2, 7, 8, 12, 1, 4, 11, 12, 13, 4, 5, 8, 10, 13, 14, 5, 13, 14, 3, 6, 9]

G1 = get_graph(nodes1, starts1, ends1)

nodes2 = [(0, {'symbol': 'Ni'}), (1, {'symbol': 'Pt'}), (2, {'symbol': 'Pt'}), (3, {'symbol': 'Pt'}), (4, {'symbol': 'Ni'}), (5, {'symbol': 'Pt'}), (6, {'symbol': 'Pt'}), (7, {'symbol': 'Pt'}), (8, {'symbol': 'Ni'}), (9, {'symbol': 'Pt'}), (10, {'symbol': 'Pt'}), (11, {'symbol': 'Pt'}), (12, {'symbol': 'Ni'}), (13, {'symbol': 'Pt'}), (14, {'symbol': 'Pt'}), (15, {'symbol': 'Pt'}), (16, {'symbol': 'CH3'}), (17, {'symbol': 'OH'}), (18, {'symbol': 'CO'})]
starts2 = [1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 16, 17, 17, 18, 18, 18]
ends2 = [0, 0, 1, 1, 2, 1, 3, 0, 4, 3, 4, 5, 0, 2, 5, 6, 2, 3, 3, 6, 8, 0, 5, 8, 9, 0, 1, 9, 10, 6, 7, 9, 11, 2, 7, 8, 12, 1, 4, 11, 12, 13, 4, 5, 8, 10, 13, 14, 1, 10, 11, 2, 7, 13]

G2 = get_graph(nodes2, starts2, ends2)

print(nx.isomorphism.is_isomorphic(G1, G2))
Shaun Han
  • 2,676
  • 2
  • 9
  • 29
  • 3
    `print(list(edges))` is consuming all the data in the `edges` iterator. So it's empty when you call `G.add_edges_from()`. – Barmar Jan 25 '21 at 22:28
  • 1
    I think that zip returns an iterable, so list(edges) has the side-effect of consuming it. print() has no real part here. – quamrana Jan 25 '21 at 22:29
  • 1
    Ninja'd by @Barmar, you are consuming your generator – crissal Jan 25 '21 at 22:29

0 Answers0