1

I recently used networkx.adjacency_matrix(G) to generate an adjacency matrix for a graph. I was surprised it showed these distinct clusters when I plotted the adjacency matrix (1). These clusters disappear when the node order is plotted in the order I added them to the graph (2) [equivalent to networkx.adjacency_matrix(G, sorted(list(G.nodes())].

What is the default ordering protocol of networkx when it makes an adjacency matrix? This does not seem to be documented.

Default "order":

enter image description here

Order in which nodes/edges added to graph:

enter image description here

batlike
  • 668
  • 1
  • 7
  • 19

1 Answers1

0

The result will may depend on your python version as raised by Arya, the topic is about default ordering of dicts. Take a look here: Are dictionaries ordered in Python 3.6+?

From the code of adjacency_matrix, which will take you to to_scipy_sparse_matrix, the used default node order is list(G).

Sparky05
  • 4,692
  • 1
  • 10
  • 27
  • Thank you for this info, I also saw that the default node order is `list(G)`. What I'm trying to disambiguate is, are the order of nodes utilized by networkX exactly as how dictionaries are ordered in Python? Additionally, even if it relies on Python dictionary ordering, how does this in any way create the stochastic block-model like pattern? – batlike Mar 19 '21 at 20:28
  • The answer you linked specifies that the dictionaries are "insertion ordered". Yet, when I plot the adjacency matrix in the insertion order it loses the block structure. – batlike Mar 19 '21 at 21:52
  • I guess `list(G)` is in your case not the ascending node (as you would get by sorting). Further guessing, I assume you have created the graph by adding edges, e.g. `(0,1), (0,3), (1, 3), (2,3)` would result in the node order `0,1,3,2`. You could `fix` this by adding the nodes manually first, i.e. `G.add_nodes_from`. – Sparky05 Mar 20 '21 at 22:54