Graph from Sparse Matrix

How can I generate a Graph from a SparseMatrixCSC and then convert it into an undirected Light Graph?

You might want to post a MWE because it’s not clear what problem you’re having.

using LightGraphs
using SparseArrays

julia> X = sprand(Bool, 10, 10, 0.1)
10Γ—10 SparseMatrixCSC{Bool, Int64} with 12 stored entries:
 β‹…  1  1  β‹…  β‹…  β‹…  β‹…  β‹…  1  β‹…
 1  β‹…  1  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…
 1  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…
 β‹…  β‹…  1  β‹…  1  1  β‹…  β‹…  β‹…  β‹…
 β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…
 β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…
 β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  1  β‹…  β‹…  1
 β‹…  β‹…  β‹…  β‹…  1  β‹…  β‹…  β‹…  β‹…  β‹…
 β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…
 β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…

julia> A = X'*X
10Γ—10 SparseMatrixCSC{Int64, Int64} with 24 stored entries:
 2  β‹…  1  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…
 β‹…  1  1  β‹…  β‹…  β‹…  β‹…  β‹…  1  β‹…
 1  1  3  β‹…  1  1  β‹…  β‹…  1  β‹…
 β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…
 β‹…  β‹…  1  β‹…  2  1  β‹…  β‹…  β‹…  β‹…
 β‹…  β‹…  1  β‹…  1  1  β‹…  β‹…  β‹…  β‹…
 β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  1  β‹…  β‹…  1
 β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  β‹…
 β‹…  1  1  β‹…  β‹…  β‹…  β‹…  β‹…  1  β‹…
 β‹…  β‹…  β‹…  β‹…  β‹…  β‹…  1  β‹…  β‹…  1

julia> Graph(A)
{10, 16} undirected simple Int64 graph
1 Like

This helps. Thanks. But I think A = (X’+X)/2 + (X-X’)/2 (not A = X’*X) ,where (X’+X)/2 is a symmetric matrix. However, the entries of (X’+X)/2 are no longer just 1’s…

What I was trying to do is (probably) a basic task of creating a LightGraph from a sparse matrix (I don’t care about the weight of edges or the directionality of edges) which have entries 1 and 0. Is there a functionality in LightGraph that can do this directly instead of us having to symmetrize the matrix?