dense_to_sparse
¶
Converts a dense adjacency matrix, or a batch of them, to an
edge_index and edge weights (the non-zero entries).
For a batch of adjacency matrices of shape [batch_size, num_nodes,
num_nodes], the graphs are stacked as one block-diagonal graph: the
nodes of graph b are offset by b * num_nodes, or, if mask is
given, by the number of valid nodes of the previous graphs (only the rows
of the valid nodes are read).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adj
|
array
|
Dense adjacency of shape |
required |
mask
|
Optional[array]
|
Optional boolean mask of the valid nodes of each graph, of shape
|
None
|
Returns:
| Type | Description |
|---|---|
tuple[array, array]
|
A tuple |
Example:
adj = mx.array([[0, 1, 0], [2, 0, 0], [0, 0, 3]])
dense_to_sparse(adj)
# (mx.array([[0, 1, 2], [1, 0, 2]]), mx.array([1, 2, 3]))
Source code in mlx_graphs/utils/sparse.py
from mlx_graphs.utils import dense_to_sparse · mlx_graphs/utils/sparse.py