Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for symmetry in _graph_from_weighted_sparse_matrix #782

Open
psl-schaefer opened this issue Jun 5, 2024 · 0 comments
Open

Check for symmetry in _graph_from_weighted_sparse_matrix #782

psl-schaefer opened this issue Jun 5, 2024 · 0 comments

Comments

@psl-schaefer
Copy link

psl-schaefer commented Jun 5, 2024

If one creates a weighted, undirected graph from an adjacency matrix, wouldn't it make sense to check if the adjacency matrix is symmetric? This check was also introduced in rigraph I think, see igraph/rigraph#182.

I am referring to this function:

def _graph_from_weighted_sparse_matrix(

A simple check could be something like symmetric = not np.any((matrix!=matrix.T).data) (here assuming that it is scipy.sparse.csr_matrix).

Here is a simple example (not the most efficient way to check the behavior though):

import numpy as np
from scipy.sparse import csr_matrix
import igraph as ig
rng = np.random.default_rng(0)
n = int(1e3)
sparse_adj = rng.choice(np.array([0, 1, 2]),
                        size=(n**2), replace=True, 
                        p=np.array([0.9, 0.05, 0.05])).reshape(n, n).astype(np.float64)
sparse_adj = csr_matrix(sparse_adj)
symmetric = not np.any((sparse_adj!=sparse_adj.T).data)
print(f"{symmetric=}")
g = ig.Graph.Weighted_Adjacency(sparse_adj, 
                                mode="undirected",
                                attr="weight")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant