Adjacency matrix network x
Solution 1:
Somewhat surprisingly, the answer to your question is no. The rows/columns of the adjacency matrix are ordered, by default, according to their order in G.nodes()
, which is not necessarily the order of the points.
In the nx documentation this is stated:
The rows and columns are ordered according to the nodes in nodelist. If nodelist is None, then the ordering is produced by G.nodes().
And since the order in G.nodes()
is arbitrary (see this SO post for example), the order is not necessarily the order of the original point.
You can easily set it to the order of the points by setting the nodelist
parameter (e.g., nodelist=sorted(G.nodes())
).
The following code demonstrates this on a simple example:
points = np.array([[0,0.], [0, 1], [1, 1], [2, 1], [3, 0]])
The resulting Delaunay triangulation is:
Using your code with the default, the G.nodes()
are (at least on my machine): [(2, 1), (2, 3), (2, 4), (2, 0), (1, 0)]
, and the resulting adjacency matrix is:
print(nx.adjacency_matrix(G).todense())
[[0, 1, 1, 1, 1],
[1, 0, 1, 0, 0],
[1, 1, 0, 0, 0],
[1, 0, 0, 0, 0],
[1, 0, 0, 0, 0]]
However, when using the following code with the nodelist
parameter we get the requested result:
print(nx.adjacency_matrix(G, nodelist=sorted(G.nodes())).todense())
[[0 1 1 0 0]
[1 0 1 0 0]
[1 1 0 1 1]
[0 0 1 0 0]
[0 0 1 0 0]]