Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Networkx to_edgelist() and from_edgelist() give me different graphs for an edgelist generated from one graph

The following code

import networkx as nx
import numpy as np

# Create a graph

random_graph = nx.erdos_renyi_graph(10, 0.5, seed=2)

G = random_graph

# Get the edge list representation
edge_list = nx.to_edgelist(G)

# Create a new graph from the edge list
new_G = nx.from_edgelist(edge_list)

# Check if the graphs are the same
print("Original Graph Adjacency Matrix:")
print(np.array(nx.adjacency_matrix(G).todense()))


print("New Graph Adjacency Matrix:")
print(np.array(nx.adjacency_matrix(new_G).todense()))


# Print adjacency matrices of the original and reconstructed graphs
original_adj_matrix = np.array(nx.adjacency_matrix(G).todense())
new_adj_matrix = np.array(nx.adjacency_matrix(new_G).todense())

# Compare adjacency matrices with a tolerance for floating-point differences
if np.allclose(original_adj_matrix, new_adj_matrix, atol=1e-8):
    print("The original and reconstructed graphs are the same.")
else:
    print("The original and reconstructed graphs are different.")

Produces the following output:

Original Graph Adjacency Matrix:
[[0 0 0 1 1 0 0 0 1 0]
 [0 0 0 0 1 1 1 0 0 0]
 [0 0 0 0 1 1 1 1 1 1]
 [1 0 0 0 1 0 0 0 1 1]
 [1 1 1 1 0 1 1 0 0 0]
 [0 1 1 0 1 0 1 0 0 0]
 [0 1 1 0 1 1 0 0 0 0]
 [0 0 1 0 0 0 0 0 1 0]
 [1 0 1 1 0 0 0 1 0 0]
 [0 0 1 1 0 0 0 0 0 0]]
New Graph Adjacency Matrix:
[[0 1 1 1 0 0 0 0 0 0]
 [1 0 1 1 0 0 0 0 0 1]
 [1 1 0 0 1 1 1 1 0 0]
 [1 1 0 0 0 0 0 1 1 0]
 [0 0 1 0 0 1 1 0 0 0]
 [0 0 1 0 1 0 1 1 0 0]
 [0 0 1 0 1 1 0 1 0 0]
 [0 0 1 1 0 1 1 0 1 1]
 [0 0 0 1 0 0 0 1 0 0]
 [0 1 0 0 0 0 0 1 0 0]]
The original and reconstructed graphs are different.

Am I doing something wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I was expecting the methods to_edgelist() and from_edgelist() to produce the same graphs, however they are both very different.

On different runs, the original and the new adjacency matrix remain the same, but they don’t match.

>Solution :

Your issue is due to the non consistent order of the nodes.

As noted in the documentation of adjacency_matrix, the nodelist parameter controls the order of the rows/columns:

nodelist
list, optional

The rows and columns are ordered according to the nodes in nodelist. If nodelist is None, then the ordering is produced by
G.nodes().

Thus you should use a defined order:

nodes = sorted(G)

edge_list = nx.to_edgelist(G, nodelist=nodes)
new_G = nx.from_edgelist(edge_list)

np.array_equal(nx.adjacency_matrix(G, nodelist=nodes).todense(),
               nx.adjacency_matrix(new_G, nodelist=nodes).todense())
# True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading