# 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?