On Tuesday, February 24, 2015 at 2:04:07 PM UTC-8, Douglas Bates wrote:
>
> sizehint! could be applied to the .rowval and .nzval fields but it will
> only make things easier if you then add elements to the last columns. Even
> if the vectors don't need to be resized, adding a non-zero value to an
> interior column of a sparse matrix requires inserting in the middle of a
> vector.
>
> If it makes sense in your application you are better off using nonzeros to
> get the triplet representation, pushing values onto the end of those
> vectors and, when you are finished, using sparse to convert back to
> SparseMatrixCSC.
>
Wow, this is amazing:
function adjmx2(g::AbstractGraph)
n_v = nv(g)
mx = spzeros(Int,0) # this throws a warning but I'll fix it
mx.n = n_v
mx.m = n_v
colindex = 1
for r in g.finclist
colvals = Int[]
for e in r
push!(colvals, dst(e))
push!(mx.nzval, 1)
colindex += 1
end
sort!(colvals)
append!(mx.rowval, colvals)
push!(mx.colptr, colindex)
end
return mx
end
julia> g = Graph(20_000,50_000)
{20000, 50000} undirected graph
julia> @time z = adjacency_matrix(g); # using standard sparsematrix
assignments, already compiled
elapsed time: 2.39827398 seconds (7 MB allocated, 0.34% gc time in 1 pauses
with 0 full sweep)
julia> @time y = LightGraphs.adjmx2(g); # using above, already compiled
elapsed time: 0.01251955 seconds (7 MB allocated)
julia> z == y
true
Thanks for the tips!
Seth.