Reputation: 31
Out of a set of data points I am following the routine below:
tri = Delaunay(points)
G = nx.Graph()
for path in tri.simplices:
#print(path)
nx.add_path(G, path)
Adj = nx.adjacency_matrix(G).todense()
my question is now... Do are the rows/column of the adjacency matrix sorted in the same order of the points? i.e.
[0,0][0, 0] = Adj_{1,1} [0,0][0,1] = Adj_{1,2} etc....
I have to classify the neighbours within my graph and determine their order.
Upvotes: 1
Views: 844
Reputation: 2056
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]]
Upvotes: 1