Discrepancy in Adjacency matrix

I have a protein-protein interaction graph of ~100 nodes and ~1000 edges as a networkx graph. The nodes of the graph have string labels. When I use the function dgl.from_networkx(nx_g) I understand the dgl uses node names starting from 0. However, I see that the adjacency matrix generated by the dgl graph is different from the adjacency matrix generated from the networkx graph.

dgl_graph = dgl.from_networkx(nx_g) # nx_g is a networkx graph

dgl_adj = dgl_graph.adj().to_dense().numpy().astype(np.int32)
nx_adj = nx.to_numpy_array(nx_g).astype(np.int32)

print(nx_adj)

>>> 
[[0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 [0 0 0 ... 0 0 0]
 ...
 [0 0 0 ... 0 1 0]
 [0 0 0 ... 1 0 0]
 [0 0 0 ... 0 0 0]]

print(dgl_adj)
>>>
[[0 0 0 ... 1 1 0]
 [0 0 0 ... 1 0 0]
 [0 0 0 ... 0 0 1]
 ...
 [1 1 0 ... 0 0 0]
 [1 0 0 ... 0 0 0]
 [0 0 1 ... 0 0 0]]

Why is this issue seen here? Though the graph is same, the ordering is totally different.

Also, is there a way to name the nodes of the dgl graph to strings?

Were your networkx node labels consecutive integers starting from 0? The adjacency matrix should be the same if so:

In [15]: g = nx.Graph([(0, 1), (1, 2), (2, 3), (2, 0), (2, 1)])

In [16]: gg = dgl.from_networkx(g)

In [17]: gg.adj().to_dense()
Out[17]:
tensor([[0., 1., 1., 0.],
        [1., 0., 1., 0.],
        [1., 1., 0., 1.],
        [0., 0., 1., 0.]])

In [18]: nx.to_numpy_array(g)
Out[18]:
array([[0., 1., 1., 0.],
       [1., 0., 1., 0.],
       [1., 1., 0., 1.],
       [0., 0., 1., 0.]])

Thanks for the reply. No, the node labels are strings (string of integers) and they are not numbered consecutively. Some node labels are as follows: ‘1200876’, ‘12’, ‘45679’. There is no ordering whatsoever.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.