rustworkx icon indicating copy to clipboard operation
rustworkx copied to clipboard

Add networkx's `from_biadjacency_matrix` and `biadjacency_matrix`

Open michaelweinold opened this issue 8 months ago • 3 comments

What is the expected enhancement?

In NetworkX, I can create a graph based on a bipartite adjacency matrix using the from_biadjacency_matrix function. Rustwork currently has no such functionality. Similarly, I cannot create a biadjacency matrix from a graph.

This would be tremendously helpful in applications where I have information on edges between two set of graph nodes stored in a non-sqaure matrix (=biadjacency matrix).

michaelweinold avatar Apr 03 '25 20:04 michaelweinold

I think the challenge for this is similar to what I pointed out at https://github.com/Qiskit/rustworkx/issues/1081#issuecomment-1934107223

Currently, we don't have a way to convert a SciPy sparse array into Rust. We'll need to figure out that first. Implementing the method is trivial, but until we have the conversion we are going to be a bit stuck.

The solution will probably use https://docs.rs/pyo3/latest/pyo3/conversion/trait.FromPyObject.html and call scipy to convert the matrix to a more convenient format we can process, maybe.

IvanIsCoding avatar Apr 04 '25 01:04 IvanIsCoding

Hey @IvanIsCoding, I was going through the sparse matrix problem, and from what I could understand from this and @mtreinish comments is that the solution for this could be, either building a function in rustwork to return something like (row, col, data) or (indptr, indices, data) as a numpy array and the user can chose to call scipy with this data or, we include passing this array to scipy and returning the sparse matrix ourselves, in which case we would have to install scipy in our dependencies. Which route should we take for this then?

Also, how much of an overkill would it be to implement a custom sparse matrix generator in Rust? TIA.

Krishn1412 avatar Oct 01 '25 14:10 Krishn1412

@Krishn1412 the solution is along those lines. As long as scipy remains an optional dependency, it’s fine. But I think the methods should take a scipy sparse array.

With that being said, I have an idea for a better sparse array issue to work on now. I will post it and assign it to you on a little bit. It will be helpful for introducing https://crates.io/crates/nalgebra-sparse

IvanIsCoding avatar Oct 02 '25 00:10 IvanIsCoding