0

Initially, i have 2d array. By using this array i have created a graph with weight on its edges. Now i am trying to use this Graph to make Minimum Spanning Tree matrix but i cant make it as desire. I am using the following code to make graph.

 G = nx.from_numpy_matrix(ED_Matrix, create_using=nx.DiGraph)
 layout = nx.spring_layout(G)
 sizes = len(ED_Matrix)
 nx.draw(G, layout, with_labels=True, node_size=sizes)
 labels = nx.get_edge_attributes(G, "weight")
 output = nx.draw_networkx_edge_labels(G, pos=layout, edge_labels=labels)
 plt.show()

And its gives the output like this enter image description here

Now i am using MST code, to get the its MST matrix but its gives error like this.

 from scipy.sparse import csr_matrix
 from scipy.sparse.csgraph import minimum_spanning_tree
 Tcsr = minimum_spanning_tree(G)
 Tcsr.toarray().astype(int)

enter image description here

1 Answer 1

1

Taking into account example from docs of scipy, it should be constructed from adjacency matrix of G, not from G.

You might want to replace G with nx.adjacency_matrix(G) or csr_matrix(nx.adjacency_matrix(G)) or ED_Matrix itself in calculation (assignment) of Tcsr:

Tcsr = minimum_spanning_tree(nx.adjacency_matrix(G)) #or
Tcsr = minimum_spanning_tree(csr_matrix(nx.adjacency_matrix(G))) #or
Tcsr = minimum_spanning_tree(ED_Matrix)

Tcsr is a sparse matrix which is later converted to numpy array.

Sign up to request clarification or add additional context in comments.

3 Comments

ED_Matrix , is just a 2d array, its not a calculation of Tcsr
I think I was not clear mentioning calculation. I mean it's an assignment.
I have replaced the G with nx.adjacency_matrix(G) plus instead of using Tcsr.toarray().astype(int) i have used Tcsr.toarray().astype(float). Now its working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.