From e71364338df1ac2266f4e348f4e2d58426505485 Mon Sep 17 00:00:00 2001 From: Seth Bromberger Date: Tue, 29 May 2018 22:19:20 -0700 Subject: [PATCH 1/4] fixes most depwarns as of 20180529 --- benchmark/core.jl | 2 +- src/LightGraphs.jl | 22 +++++++++---------- src/SimpleGraphs/simpledigraph.jl | 2 +- src/centrality/closeness.jl | 4 ++-- src/centrality/eigenvector.jl | 2 +- src/centrality/katz.jl | 4 ++-- src/centrality/radiality.jl | 4 ++-- src/distance.jl | 4 ++-- src/edit_distance.jl | 4 ++-- src/generators/euclideangraphs.jl | 4 ++-- src/generators/randgraphs.jl | 24 ++++++++++----------- src/graphcut/normalized_cut.jl | 24 ++++++++++----------- src/linalg/LinAlg.jl | 6 +++--- src/linalg/graphmatrices.jl | 28 ++++++++++++------------ src/linalg/spectral.jl | 18 ++++++++-------- src/shortestpaths/dijkstra.jl | 6 +++--- src/traversals/diffusion.jl | 2 +- src/traversals/greedy_color.jl | 6 +++--- src/utils.jl | 11 ++-------- test/centrality/betweenness.jl | 4 ++-- test/centrality/eigenvector.jl | 6 +++--- test/centrality/katz.jl | 2 +- test/centrality/pagerank.jl | 4 ++-- test/connectivity.jl | 6 +++--- test/generators/binomial.jl | 3 ++- test/generators/randgraphs.jl | 12 ++++++----- test/graphcut/normalized_cut.jl | 4 ++-- test/linalg/graphmatrices.jl | 36 +++++++++++++++---------------- test/linalg/spectral.jl | 15 ++++++------- test/simplegraphs/simplegraphs.jl | 8 +++---- 30 files changed, 136 insertions(+), 141 deletions(-) diff --git a/benchmark/core.jl b/benchmark/core.jl index c05646d04..a0e9e229a 100644 --- a/benchmark/core.jl +++ b/benchmark/core.jl @@ -7,7 +7,7 @@ function bench_iteredges(g::AbstractGraph) end function bench_has_edge(g::AbstractGraph) - srand(1) + Random.srand(1) nvg = nv(g) srcs = rand([1:nvg;], cld(nvg, 4)) dsts = rand([1:nvg;], cld(nvg, 4)) diff --git a/src/LightGraphs.jl b/src/LightGraphs.jl index 71cb134d8..6d8f4a03d 100644 --- a/src/LightGraphs.jl +++ b/src/LightGraphs.jl @@ -1,25 +1,25 @@ __precompile__(true) module LightGraphs +using SimpleTraits + import CodecZlib import DataStructures - -using SimpleTraits -using SharedArrays -using SparseArrays -using LinearAlgebra -using IterativeEigensolvers -using SharedArrays -using Markdown -using DelimitedFiles +import SharedArrays +import SparseArrays +import LinearAlgebra +import IterativeEigensolvers +import SharedArrays +import Markdown +import DelimitedFiles import Base: write, ==, <, *, ≈, convert, isless, issubset, union, intersect, reverse, reverse!, isassigned, getindex, setindex!, show, print, copy, in, sum, size, eltype, length, ndims, transpose, ctranspose, join, start, next, done, eltype, get, Pair, Tuple, zero -import Random: GLOBAL_RNG +import Random import Distributed: @distributed, @sync import SparseArrays: sparse, blockdiag -import LinearAlgebra: issymmetric, mul! +import LinearAlgebra: issymmetric, mul!, Diagonal export # Interface diff --git a/src/SimpleGraphs/simpledigraph.jl b/src/SimpleGraphs/simpledigraph.jl index 1d6767997..159a9ed61 100644 --- a/src/SimpleGraphs/simpledigraph.jl +++ b/src/SimpleGraphs/simpledigraph.jl @@ -31,7 +31,7 @@ SimpleDiGraph(n::T) where T<:Integer = SimpleDiGraph{T}(n) SimpleDiGraph(::Type{T}) where T<:Integer = SimpleDiGraph{T}(zero(T)) # sparse adjacency matrix constructor: SimpleDiGraph(adjmx) -function SimpleDiGraph{T}(adjmx::SparseMatrixCSC{U}) where T<:Integer where U<:Real +function SimpleDiGraph{T}(adjmx::SparseArrays.SparseMatrixCSC{U}) where T<:Integer where U<:Real dima, dimb = size(adjmx) isequal(dima, dimb) || throw(ArgumentError("Adjacency / distance matrices must be square")) diff --git a/src/centrality/closeness.jl b/src/centrality/closeness.jl index d4ea76cea..bdaa3c7d8 100644 --- a/src/centrality/closeness.jl +++ b/src/centrality/closeness.jl @@ -44,7 +44,7 @@ function parallel_closeness_centrality( n_v = Int(nv(g)) - closeness = SharedVector{Float64}(n_v) + closeness = SharedArrays.SharedVector{Float64}(n_v) @sync @distributed for u in vertices(g) if degree(g, u) == 0 # no need to do Dijkstra here @@ -63,5 +63,5 @@ function parallel_closeness_centrality( end end end - return sdata(closeness) + return SharedArrays.sdata(closeness) end diff --git a/src/centrality/eigenvector.jl b/src/centrality/eigenvector.jl index 191734450..936a50c03 100644 --- a/src/centrality/eigenvector.jl +++ b/src/centrality/eigenvector.jl @@ -24,4 +24,4 @@ eigenvector of the adjacency matrix \$\\mathbf{A}\$. - Mark E. J. Newman: Networks: An Introduction. Oxford University Press, USA, 2010, pp. 169. """ -eigenvector_centrality(g::AbstractGraph) = abs.(vec(eigs(adjacency_matrix(g), nev=1)[2]))::Vector{Float64} +eigenvector_centrality(g::AbstractGraph) = abs.(vec(IterativeEigensolvers.eigs(adjacency_matrix(g), nev=1)[2]))::Vector{Float64} diff --git a/src/centrality/katz.jl b/src/centrality/katz.jl index 37bd15e23..11d9cc4f4 100644 --- a/src/centrality/katz.jl +++ b/src/centrality/katz.jl @@ -30,9 +30,9 @@ the centrality calculated for each node in `g`. function katz_centrality(g::AbstractGraph, α::Real=0.3) nvg = nv(g) v = ones(Float64, nvg) - spI = sparse(one(Float64) * I, nvg, nvg) + spI = sparse(one(Float64) * LinearAlgebra.I, nvg, nvg) A = adjacency_matrix(g, Bool; dir=:in) v = (spI - α * A) \ v - v /= norm(v) + v /= LinearAlgebra.norm(v) return v end diff --git a/src/centrality/radiality.jl b/src/centrality/radiality.jl index f381ca370..8cc4c775e 100644 --- a/src/centrality/radiality.jl +++ b/src/centrality/radiality.jl @@ -36,8 +36,8 @@ function parallel_radiality_centrality(g::AbstractGraph)::Vector{Float64} n_v = nv(g) vs = vertices(g) n = ne(g) - meandists = SharedVector{Float64}(Int(n_v)) - maxdists = SharedVector{Float64}(Int(n_v)) + meandists = SharedArrays.SharedVector{Float64}(Int(n_v)) + maxdists = SharedArrays.SharedVector{Float64}(Int(n_v)) @sync @distributed for i = 1:n_v d = dijkstra_shortest_paths(g, vs[i]) diff --git a/src/distance.jl b/src/distance.jl index 39b764b0e..ecf06689f 100644 --- a/src/distance.jl +++ b/src/distance.jl @@ -77,11 +77,11 @@ function parallel_eccentricity( distmx::AbstractMatrix{T} = weights(g) ) where T <: Real vlen = length(vs) - eccs = SharedVector{T}(vlen) + eccs = SharedArrays.SharedVector{T}(vlen) @sync @distributed for i = 1:vlen eccs[i] = maximum(dijkstra_shortest_paths(g, vs[i], distmx).dists) end - d = sdata(eccs) + d = SharedArrays.sdata(eccs) maximum(d) == typemax(T) && warn("Infinite path length detected") return d end diff --git a/src/edit_distance.jl b/src/edit_distance.jl index fb774014e..4bd684185 100644 --- a/src/edit_distance.jl +++ b/src/edit_distance.jl @@ -119,7 +119,7 @@ vertex v ∈ G₂. `p=1`: the p value for p-norm calculation. """ function MinkowskiCost(μ₁::AbstractVector, μ₂::AbstractVector; p::Real=1) - (u, v) -> norm(μ₁[u] - μ₂[v], p) + (u, v) -> LinearAlgebra.norm(μ₁[u] - μ₂[v], p) end """ @@ -132,5 +132,5 @@ Return value similar to `MinkowskiCost`, but ensure costs smaller than 2τ. `τ=1`: value specifying half of the upper limit of the Minkowski cost. """ function BoundedMinkowskiCost(μ₁::AbstractVector, μ₂::AbstractVector; p::Real=1, τ::Real=1) - (u, v) -> 1 / (1 / (2τ) + exp(-norm(μ₁[u] - μ₂[v], p))) + (u, v) -> 1 / (1 / (2τ) + exp(-LinearAlgebra.norm(μ₁[u] - μ₂[v], p))) end diff --git a/src/generators/euclideangraphs.jl b/src/generators/euclideangraphs.jl index 67df4d224..36f8ef856 100644 --- a/src/generators/euclideangraphs.jl +++ b/src/generators/euclideangraphs.jl @@ -8,7 +8,7 @@ a matrix with the points' positions. function euclidean_graph(N::Int, d::Int; L=1., seed = -1, kws...) rng = LightGraphs.getRNG(seed) - points = rmul!(rand(rng, d, N), L) + points = LinearAlgebra.rmul!(rand(rng, d, N), L) return (euclidean_graph(points; L=L, kws...)..., points) end @@ -49,7 +49,7 @@ function euclidean_graph(points::Matrix; else throw(ArgumentError("$bc is not a valid boundary condition")) end - dist = norm(Δ, p) + dist = LinearAlgebra.norm(Δ, p) if dist < cutoff e = Edge(i, j) add_edge!(g, e) diff --git a/src/generators/randgraphs.jl b/src/generators/randgraphs.jl index b748b914b..a15d46a4d 100644 --- a/src/generators/randgraphs.jl +++ b/src/generators/randgraphs.jl @@ -195,9 +195,9 @@ function _suitable(edges::Set{Edge}, potential_edges::Dict{T,T}) where T<:Intege return false end -_try_creation(n::Integer, k::Integer, rng::AbstractRNG) = _try_creation(n, fill(k, n), rng) +_try_creation(n::Integer, k::Integer, rng::Random.AbstractRNG) = _try_creation(n, fill(k, n), rng) -function _try_creation(n::T, k::Vector{T}, rng::AbstractRNG) where T<:Integer +function _try_creation(n::T, k::Vector{T}, rng::Random.AbstractRNG) where T<:Integer edges = Set{Edge}() m = 0 stubs = zeros(T, sum(k)) @@ -211,7 +211,7 @@ function _try_creation(n::T, k::Vector{T}, rng::AbstractRNG) where T<:Integer while !isempty(stubs) potential_edges = Dict{T,T}() - shuffle!(rng, stubs) + Random.shuffle!(rng, stubs) for i in 1:2:length(stubs) s1, s2 = stubs[i:(i + 1)] if (s1 > s2) @@ -298,7 +298,7 @@ function barabasi_albert!(g::AbstractGraph, n::Integer, k::Integer; seed::Int=-1 n0 == n && return g # seed random number generator - seed > 0 && srand(seed) + seed > 0 && Random.srand(seed) # add missing vertices sizehint!(g.fadjlist, n) @@ -503,7 +503,7 @@ function static_scale_free(n::Integer, m::Integer, α_out::Real, α_in::Float64; fitness_out = _construct_fitness(n, α_out, finite_size_correction) fitness_in = _construct_fitness(n, α_in, finite_size_correction) # eliminate correlation - shuffle!(fitness_in) + Random.shuffle!(fitness_in) static_fitness_model(m, fitness_out, fitness_in, seed=seed) end @@ -755,7 +755,7 @@ mutable struct StochasticBlockModel{T<:Integer,P<:Real} n::T nodemap::Array{T} affinities::Matrix{P} - rng::MersenneTwister + rng::Random.MersenneTwister end ==(sbm::StochasticBlockModel, other::StochasticBlockModel) = @@ -789,7 +789,7 @@ and external probabilities `externalp`. function sbmaffinity(internalp::Vector{T}, externalp::Real, sizes::Vector{U}) where T<:Real where U<:Integer numblocks = length(sizes) numblocks == length(internalp) || throw(ArgumentError("Inconsistent input dimensions: internalp, sizes")) - B = diagm(0=>internalp) + externalp * (ones(numblocks, numblocks) - I) + B = LinearAlgebra.diagm(0=>internalp) + externalp * (ones(numblocks, numblocks) - LinearAlgebra.I) return B end @@ -810,7 +810,7 @@ function StochasticBlockModel(internalp::Vector{T}, externalp::Real, end -const biclique = ones(2, 2) - eye(2) +const biclique = ones(2, 2) - Matrix{Float64}(LinearAlgebra.I, 2, 2) #TODO: this documentation needs work. sbromberger 20170326 """ @@ -826,7 +826,7 @@ The blocks are connected with probability `between`. """ function nearbipartiteaffinity(sizes::Vector{T}, between::Real, intra::Real) where T<:Integer numblocks = div(length(sizes), 2) - return kron(between * eye(numblocks), biclique) + eye(2numblocks) * intra + return kron(between * Matrix{Float64}(LinearAlgebra.I, numblocks, numblocks), biclique) + Matrix{Float64}(LinearAlgebra.I, 2*numblocks, 2*numblocks) * intra end #Return a generator for edges from a stochastic block model near-bipartite graph. @@ -841,7 +841,7 @@ nearbipartiteSBM(sizes, between, inter, noise; seed::Int = -1) = Generate a stream of random pairs in `1:n` using random number generator `RNG`. """ -function random_pair(rng::AbstractRNG, n::Integer) +function random_pair(rng::Random.AbstractRNG, n::Integer) f(ch) = begin while true put!(ch, Edge(rand(rng, 1:n), rand(rng, 1:n))) @@ -938,10 +938,10 @@ function kronecker(SCALE, edgefactor, A=0.57, B=0.19, C=0.19) ij .+= 2^(ib - 1) .* (hcat(ii_bit, jj_bit)) end - p = randperm(N) + p = Random.randperm(N) ij = p[ij] - p = randperm(M) + p = Random.randperm(M) ij = ij[p, :] g = SimpleDiGraph(N) diff --git a/src/graphcut/normalized_cut.jl b/src/graphcut/normalized_cut.jl index 8aa8b7328..8e9658be7 100644 --- a/src/graphcut/normalized_cut.jl +++ b/src/graphcut/normalized_cut.jl @@ -12,13 +12,13 @@ function _normalized_cut_cost(cut, W::AbstractMatrix, D) return cut_cost/sum(D*cut) + cut_cost/sum(D*(.~cut)) end -function _normalized_cut_cost(cut, W::SparseMatrixCSC, D) +function _normalized_cut_cost(cut, W::SparseArrays.SparseMatrixCSC, D) cut_cost = 0 - rows = rowvals(W) - vals = nonzeros(W) + rows = SparseArrays.rowvals(W) + vals = SparseArrays.nonzeros(W) n = size(W, 2) for i = 1:n - for j in nzrange(W, i) + for j in SparseArrays.nzrange(W, i) row = rows[j] if cut[i] != cut[row] cut_cost += vals[j]/2 @@ -65,7 +65,7 @@ function _partition_weightmx(cut, W::AbstractMatrix) return (W1, W2, vmap1, vmap2) end -function _partition_weightmx(cut, W::SparseMatrixCSC) +function _partition_weightmx(cut, W::SparseArrays.SparseMatrixCSC) nv = length(cut) nv2 = sum(cut) nv1 = nv - nv2 @@ -86,13 +86,13 @@ function _partition_weightmx(cut, W::SparseMatrixCSC) end end - rows = rowvals(W) - vals = nonzeros(W) + rows = SparseArrays.rowvals(W) + vals = SparseArrays.nonzeros(W) I1 = Vector{Int}(); I2 = Vector{Int}() J1 = Vector{Int}(); J2 = Vector{Int}() V1 = Vector{Float64}(); V2 = Vector{Float64}() for i = 1:nv - for j in nzrange(W, i) + for j in SparseArrays.nzrange(W, i) row = rows[j] if cut[i] == cut[row] == false push!(I1, newvid[i]) @@ -112,18 +112,18 @@ end function _recursive_normalized_cut(W, thres=thres, num_cuts=num_cuts) m, n = size(W) - D = Diagonal(vec(sum(W, dims=2))) + D = LinearAlgebra.Diagonal(vec(sum(W, dims=2))) m == 1 && return [1] #get eigenvector corresponding to second smallest eigenvalue - # v = eigs(D-W, D, nev=2, which=:SR)[2][:,2] + # v = IterativeEigensolvers.eigs(D-W, D, nev=2, which=:SR)[2][:,2] # At least some versions of ARPACK have a bug, this is a workaround invDroot = sqrt.(inv(D)) # equal to Cholesky factorization for diagonal D if n > 10 - ret = eigs(invDroot'*(D-W)*invDroot, nev=2, which=:SR)[2][:,2] + ret = IterativeEigensolvers.eigs(invDroot'*(D-W)*invDroot, nev=2, which=:SR)[2][:,2] else - ret = eigfact(Matrix(invDroot'*(D-W)*invDroot)).vectors[:,2] + ret = LinearAlgebra.eigen(Matrix(invDroot'*(D-W)*invDroot)).vectors[:,2] end v = invDroot*ret diff --git a/src/linalg/LinAlg.jl b/src/linalg/LinAlg.jl index 63433333e..bb2238fdb 100644 --- a/src/linalg/LinAlg.jl +++ b/src/linalg/LinAlg.jl @@ -1,9 +1,9 @@ module LinAlg using SimpleTraits -using SparseArrays -using LinearAlgebra -using IterativeEigensolvers +import SparseArrays +import LinearAlgebra +import IterativeEigensolvers using ..LightGraphs import LightGraphs: IsDirected, AbstractGraph, inneighbors, diff --git a/src/linalg/graphmatrices.jl b/src/linalg/graphmatrices.jl index 17ef7247e..a84efd0c7 100644 --- a/src/linalg/graphmatrices.jl +++ b/src/linalg/graphmatrices.jl @@ -1,4 +1,4 @@ -const SparseMatrix{T} = SparseMatrixCSC{T,Int64} +const SparseMatrix{T} = SparseArrays.SparseMatrixCSC{T,Int64} """ GraphMatrix{T} @@ -85,14 +85,14 @@ function AveragingAdjacency(adjmat::CombinatorialAdjacency) return AveragingAdjacency(adjmat, sf) end -perron(adjmat::NormalizedAdjacency) = sqrt.(adjmat.A.D) / norm(sqrt.(adjmat.A.D)) +perron(adjmat::NormalizedAdjacency) = sqrt.(adjmat.A.D) / LinearAlgebra.norm(sqrt.(adjmat.A.D)) struct PunchedAdjacency{T} <: Adjacency{T} A::NormalizedAdjacency{T} perron::Vector{T} end function PunchedAdjacency(adjmat::CombinatorialAdjacency) - perron = sqrt.(adjmat.D) / norm(sqrt.(adjmat.D)) + perron = sqrt.(adjmat.D) / LinearAlgebra.norm(sqrt.(adjmat.D)) return PunchedAdjacency(NormalizedAdjacency(adjmat), perron) end @@ -111,7 +111,7 @@ struct Noop end Base.broadcast(::typeof(*), ::Noop, x) = x -Diagonal(::Noop) = Noop() +LinearAlgebra.Diagonal(::Noop) = Noop() ==(g::GraphMatrix, h::GraphMatrix) = typeof(g) == typeof(h) && (g.A == h.A) @@ -197,7 +197,7 @@ convert(::Type{CombinatorialAdjacency}, adjmat::CombinatorialAdjacency) = adjmat function sparse(lapl::M) where M<:Laplacian adjmat = adjacency(lapl) A = sparse(adjmat) - L = sparse(Diagonal(diag(lapl))) - A + L = sparse(LinearAlgebra.Diagonal(diag(lapl))) - A return L end @@ -207,7 +207,7 @@ end function sparse(adjmat::Adjacency) A = sparse(adjmat.A) - return Diagonal(prescalefactor(adjmat)) * (A * Diagonal(postscalefactor(adjmat))) + return LinearAlgebra.Diagonal(prescalefactor(adjmat)) * (A * Diagonal(postscalefactor(adjmat))) end @@ -215,7 +215,7 @@ end function convert(::Type{SparseMatrix{T}}, lapl::Laplacian{T}) where T adjmat = adjacency(lapl) A = convert(SparseMatrix{T}, adjmat) - L = sparse(Diagonal(diag(lapl))) - A + L = sparse(LinearAlgebra.Diagonal(diag(lapl))) - A return L end @@ -236,7 +236,7 @@ diag(lapl::Laplacian) = ones(size(lapl)[2]) function *(adjmat::PunchedAdjacency{T}, x::AbstractVector{T}) where T<:Number y = adjmat.A * x - return y - dot(adjmat.perron, y) * adjmat.perron + return y - LinearAlgebra.dot(adjmat.perron, y) * adjmat.perron end function mul!(Y, A::Adjacency, B) @@ -245,10 +245,10 @@ function mul!(Y, A::Adjacency, B) # The last call to mul! must be (Y, postscalefactor, tmp) # so we need to write to tmp in the second step must be (tmp, A.A, Y) # and the first step (Y, prescalefactor, B) - tmp1 = Diagonal(prescalefactor(A)) * B + tmp1 = LinearAlgebra.Diagonal(prescalefactor(A)) * B tmp = similar(Y) mul!(tmp, A.A, tmp1) - return mul!(Y, Diagonal(postscalefactor(A)), tmp) + return mul!(Y, LinearAlgebra.Diagonal(postscalefactor(A)), tmp) end mul!(Y, A::CombinatorialAdjacency, B) = mul!(Y, A.A, B) @@ -257,14 +257,14 @@ mul!(Y, A::CombinatorialAdjacency, B) = mul!(Y, A.A, B) # This is true for all Adjacency where the postscalefactor is a Noop # at time of writing this is just StochasticAdjacency and CombinatorialAdjacency function mul!(Y, A::StochasticAdjacency, B) - tmp = Diagonal(prescalefactor(A)) * B + tmp = LinearAlgebra.Diagonal(prescalefactor(A)) * B mul!(Y, A.A, tmp) return Y end function mul!(Y, adjmat::PunchedAdjacency, x) y = adjmat.A * x - Y[:] = y - dot(adjmat.perron, y) * adjmat.perron + Y[:] = y - LinearAlgebra.dot(adjmat.perron, y) * adjmat.perron return Y end @@ -290,9 +290,9 @@ function symmetrize(A::SparseMatrix, which=:or) end T = A if which == :triu - T = triu(A) + T = LinearAlgebra.triu(A) elseif which == :tril - T = tril(A) + T = LinearAlgebra.tril(A) elseif which == :sum T = A else diff --git a/src/linalg/spectral.jl b/src/linalg/spectral.jl index 6b8c62b21..fef0713ae 100644 --- a/src/linalg/spectral.jl +++ b/src/linalg/spectral.jl @@ -49,7 +49,7 @@ function _adjacency_matrix(g::AbstractGraph{U}, T::DataType, neighborfn::Functio colpt[j + 1] = colpt[j] + length(dsts) append!(rowval, sort!(dsts)) end - spmx = SparseMatrixCSC(n_v, n_v, colpt, rowval, ones(T, nz)) + spmx = SparseArrays.SparseMatrixCSC(n_v, n_v, colpt, rowval, ones(T, nz)) # this is inefficient. There should be a better way of doing this. # the issue is that adjacency matrix entries for self-loops are 2, @@ -80,7 +80,7 @@ function laplacian_matrix(g::AbstractGraph{U}, T::DataType=Int; dir::Symbol=:uns dir = is_directed(g) ? :both : :out end A = adjacency_matrix(g, T; dir=dir) - D = convert(SparseMatrixCSC{T, U}, Diagonal(sparse(sum(A, dims=2)[:]))) + D = convert(SparseArrays.SparseMatrixCSC{T, U}, LinearAlgebra.Diagonal(sparse(sum(A, dims=2)[:]))) return D - A end @@ -98,10 +98,10 @@ by vertex. Default values for `T` are the same as those in Converts the matrix to dense with ``nv^2`` memory usage. ### Implementation Notes -Use `eigs(laplacian_matrix(g); kwargs...)` to compute some of the +Use `IterativeEigensolvers.eigs(laplacian_matrix(g); kwargs...)` to compute some of the eigenvalues/eigenvectors. """ -laplacian_spectrum(g::AbstractGraph, T::DataType=Int; dir::Symbol=:unspec) = eigvals(Matrix(laplacian_matrix(g, T; dir=dir))) +laplacian_spectrum(g::AbstractGraph, T::DataType=Int; dir::Symbol=:unspec) = LinearAlgebra.eigvals(Matrix(laplacian_matrix(g, T; dir=dir))) """ Return the eigenvalues of the adjacency matrix for a graph `g`, indexed @@ -115,14 +115,14 @@ by vertex. Default values for `T` are the same as those in Converts the matrix to dense with ``nv^2`` memory usage. ### Implementation Notes -Use `eigs(adjacency_matrix(g); kwargs...)` to compute some of the +Use `IterativeEigensolvers.eigs(adjacency_matrix(g); kwargs...)` to compute some of the eigenvalues/eigenvectors. """ function adjacency_spectrum(g::AbstractGraph, T::DataType=Int; dir::Symbol=:unspec) if dir == :unspec dir = is_directed(g) ? :both : :out end - return eigvals(Matrix(adjacency_matrix(g, T; dir=dir))) + return LinearAlgebra.eigvals(Matrix(adjacency_matrix(g, T; dir=dir))) end """ @@ -162,7 +162,7 @@ function incidence_matrix(g::AbstractGraph, T::DataType=Int; oriented=false) end end - spmx = SparseMatrixCSC(n_v, n_e, colpt, rowval, nzval) + spmx = SparseArrays.SparseMatrixCSC(n_v, n_e, colpt, rowval, nzval) return spmx end @@ -183,8 +183,8 @@ function spectral_distance end A₁ = adjacency_matrix(G₁) A₂ = adjacency_matrix(G₂) - λ₁ = k < nv(G₁) - 1 ? eigs(A₁, nev=k, which=:LR)[1] : eigvals(Matrix(A₁))[end:-1:(end - (k - 1))] - λ₂ = k < nv(G₂) - 1 ? eigs(A₂, nev=k, which=:LR)[1] : eigvals(Matrix(A₂))[end:-1:(end - (k - 1))] + λ₁ = k < nv(G₁) - 1 ? IterativeEigensolvers.eigs(A₁, nev=k, which=:LR)[1] : LinearAlgebra.eigvals(Matrix(A₁))[end:-1:(end - (k - 1))] + λ₂ = k < nv(G₂) - 1 ? IterativeEigensolvers.eigs(A₂, nev=k, which=:LR)[1] : LinearAlgebra.eigvals(Matrix(A₂))[end:-1:(end - (k - 1))] return sum(abs, (λ₁ - λ₂)) end diff --git a/src/shortestpaths/dijkstra.jl b/src/shortestpaths/dijkstra.jl index cb09b3ef0..5273f3224 100644 --- a/src/shortestpaths/dijkstra.jl +++ b/src/shortestpaths/dijkstra.jl @@ -138,8 +138,8 @@ function parallel_multisource_dijkstra_shortest_paths( r_v = length(sources) # TODO: remove `Int` once julialang/#23029 / #23032 are resolved - dists = SharedMatrix{T}(Int(r_v), Int(n_v)) - parents = SharedMatrix{U}(Int(r_v), Int(n_v)) + dists = SharedArrays.SharedMatrix{T}(Int(r_v), Int(n_v)) + parents = SharedArrays.SharedMatrix{U}(Int(r_v), Int(n_v)) @sync @distributed for i in 1:r_v state = dijkstra_shortest_paths(g, sources[i], distmx) @@ -147,6 +147,6 @@ function parallel_multisource_dijkstra_shortest_paths( parents[i, :] = state.parents end - result = MultipleDijkstraState(sdata(dists), sdata(parents)) + result = MultipleDijkstraState(SharedArrays.sdata(dists), SharedArrays.sdata(parents)) return result end diff --git a/src/traversals/diffusion.jl b/src/traversals/diffusion.jl index 5dd631d65..de7684daf 100644 --- a/src/traversals/diffusion.jl +++ b/src/traversals/diffusion.jl @@ -54,7 +54,7 @@ function diffusion(g::AbstractGraph{T}, local_p = p end - randsubseq!(randsubseq_buf, outn, local_p) + Random.randsubseq!(randsubseq_buf, outn, local_p) union!(new_infections, randsubseq_buf) end end diff --git a/src/traversals/greedy_color.jl b/src/traversals/greedy_color.jl index b5cdabf1d..2bff13dc3 100644 --- a/src/traversals/greedy_color.jl +++ b/src/traversals/greedy_color.jl @@ -69,7 +69,7 @@ function parallel_random_greedy_color( ) where T<:Integer best = @distributed (best_color) for i in 1:reps - seq = shuffle(vertices(g)) + seq = Random.shuffle(vertices(g)) perm_greedy_color(g, seq) end @@ -87,11 +87,11 @@ function seq_random_greedy_color( reps::Integer ) where T <: Integer - seq = shuffle(vertices(g)) + seq = Random.shuffle(vertices(g)) best = perm_greedy_color(g, seq) for i in 2:reps - shuffle!(seq) + Random.shuffle!(seq) best = best_color(best, perm_greedy_color(g, seq)) end return best diff --git a/src/utils.jl b/src/utils.jl index 6ee94e9af..fa0e33f05 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -9,7 +9,7 @@ Sample `k` element from array `a` without repetition and eventually excluding el ### Implementation Notes Changes the order of the elements in `a`. For a non-mutating version, see [`sample`](@ref). """ -function sample!(rng::AbstractRNG, a::AbstractVector, k::Integer; exclude = ()) +function sample!(rng::Random.AbstractRNG, a::AbstractVector, k::Integer; exclude = ()) minsize = k + length(exclude) length(a) < minsize && throw(ArgumentError("vector must be at least size $minsize")) res = Vector{eltype(a)}() @@ -42,7 +42,7 @@ Unlike [`sample!`](@ref), does not produce side effects. """ sample(a::UnitRange, k::Integer; exclude = ()) = sample!(getRNG(), collect(a), k; exclude = exclude) -getRNG(seed::Integer = -1) = seed >= 0 ? MersenneTwister(seed) : GLOBAL_RNG +getRNG(seed::Integer = -1) = seed >= 0 ? Random.MersenneTwister(seed) : Random.GLOBAL_RNG """ insorted(item, collection) @@ -53,10 +53,3 @@ Return true if `item` is in sorted collection `collection`. Does not verify that `collection` is sorted. """ insorted(item, collection) = !isempty(searchsorted(collection, item)) - -""" - eye(m) - -Return the `m` x `m` identity matrix with type specified by the type of `m`. -""" -eye(m::T) where T <: Number = Matrix{T}(I, m , m) diff --git a/test/centrality/betweenness.jl b/test/centrality/betweenness.jl index 1cef66074..6a414f426 100644 --- a/test/centrality/betweenness.jl +++ b/test/centrality/betweenness.jl @@ -17,8 +17,8 @@ y = @inferred(betweenness_centrality(g, endpoints=true, normalize=false)) yp = parallel_betweenness_centrality(g, endpoints=true, normalize=false) @test all(isapprox(y, yp)) - @test round.(y[1:3], 4) == - round.([122.10760591498584, 159.0072453120582, 176.39547945994505], 4) + @test round.(y[1:3], digits=4) == + round.([122.10760591498584, 159.0072453120582, 176.39547945994505], digits=4) diff --git a/test/centrality/eigenvector.jl b/test/centrality/eigenvector.jl index 86e396c34..764527b69 100644 --- a/test/centrality/eigenvector.jl +++ b/test/centrality/eigenvector.jl @@ -4,13 +4,13 @@ for g in testgraphs(g1) y = @inferred(eigenvector_centrality(g)) - @test round.(y, 3) == round.([ + @test round.(y, digits=3) == round.([ 0.3577513877490464, 0.3577513877490464, 0.5298987782873977, 0.5298987782873977, 0.4271328349194304 - ], 3) + ], digits=3) end for g in testdigraphs(g2) y = @inferred(eigenvector_centrality(g)) - @test round.(y, 3) == round.([0.5, 0.5, 0.5, 0.5], 3) + @test round.(y, digits=3) == round.([0.5, 0.5, 0.5, 0.5], digits=3) end end diff --git a/test/centrality/katz.jl b/test/centrality/katz.jl index 05eead525..99b38d249 100644 --- a/test/centrality/katz.jl +++ b/test/centrality/katz.jl @@ -3,6 +3,6 @@ add_edge!(g5, 1, 2); add_edge!(g5, 2, 3); add_edge!(g5, 1, 3); add_edge!(g5, 3, 4) for g in testdigraphs(g5) z = @inferred(katz_centrality(g, 0.4)) - @test round.(z, 2) == [0.32, 0.44, 0.62, 0.56] + @test round.(z, digits=2) == [0.32, 0.44, 0.62, 0.56] end end diff --git a/test/centrality/pagerank.jl b/test/centrality/pagerank.jl index 45af94c44..9e4490994 100644 --- a/test/centrality/pagerank.jl +++ b/test/centrality/pagerank.jl @@ -6,7 +6,7 @@ M = Matrix{Float64}(adjacency_matrix(g)) M = M' M[:, danglingnodes] .= sum(danglingnodes) ./ nv(g) - M = M * Diagonal(1 ./ sum(M, dims=1)[:]) + M = M * LinearAlgebra.Diagonal(1 ./ sum(M, dims=1)[:]) @assert all(1.01 .>= sum(M, dims=1).>=0.999) # v = inv(I-β*M) * ((1-β)/nv(g) * ones(nv(g), 1)) v = inv(I-α*M) * ((1-α)/nv(g) * ones(nv(g), 1)) @@ -19,7 +19,7 @@ M = Matrix{Float64}(adjacency_matrix(g)) @show M = M' M[:, danglingnodes] = sum(danglingnodes) ./ nv(g) - @show M = M * Diagonal(1 ./ sum(M, dims=1)[:]) + @show M = M * LinearAlgebra.Diagonal(1 ./ sum(M, dims=1)[:]) @show sum(M,1) @assert all(1.01 .>= sum(M, 1).>=0.999) return α*M .+ (1-α)*p diff --git a/test/connectivity.jl b/test/connectivity.jl index c0df23856..1471f8ea1 100644 --- a/test/connectivity.jl +++ b/test/connectivity.jl @@ -128,12 +128,12 @@ # figure 2 example fig2 = spzeros(5, 5) - fig2[[3, 10, 11, 13, 14, 17, 18, 19, 22]] = 1 + fig2[[3, 10, 11, 13, 14, 17, 18, 19, 22]] .= 1 fig2 = SimpleDiGraph(fig2) # figure 3 example fig3 = spzeros(8, 8) - fig3[[1, 7, 9, 13, 14, 15, 18, 20, 23, 27, 28, 31, 33, 34, 37, 45, 46, 49, 57, 63, 64]] = 1 + fig3[[1, 7, 9, 13, 14, 15, 18, 20, 23, 27, 28, 31, 33, 34, 37, 45, 46, 49, 57, 63, 64]] .= 1 fig3 = SimpleDiGraph(fig3) scc_fig3 = Vector[[3, 4], [2, 5, 6], [8], [1, 7]] fig3_cond = SimpleDiGraph(4); @@ -149,7 +149,7 @@ # figure 8 example fig8 = spzeros(6, 6) - fig8[[2, 10, 13, 21, 24, 27, 35]] = 1 + fig8[[2, 10, 13, 21, 24, 27, 35]] .= 1 fig8 = SimpleDiGraph(fig8) @test Set(@inferred(strongly_connected_components(fig1))) == Set(scc_fig1) diff --git a/test/generators/binomial.jl b/test/generators/binomial.jl index 37a741157..f7cf13a24 100644 --- a/test/generators/binomial.jl +++ b/test/generators/binomial.jl @@ -5,6 +5,7 @@ using Distributions using LightGraphs using StatsBase using Base.Test +import Random import Base: - import LightGraphs: randbn @@ -37,7 +38,7 @@ function binomial_test(n, p, s) @show dσ - lσ @test abs(dσ - lσ) / dσ < .10 end -srand(1234) +Random.srand(1234) n = 10000 p = 0.3 s = 100000 diff --git a/test/generators/randgraphs.jl b/test/generators/randgraphs.jl index e25be070d..db313dd5d 100644 --- a/test/generators/randgraphs.jl +++ b/test/generators/randgraphs.jl @@ -185,13 +185,15 @@ @test nv(rd) == 1000 @test ne(rd) == 4000 @test is_directed(rd) - @test std(outdegree(rd)) == 0 + outdegree_rd = @inferred(outdegree(rd)) + @test all(outdegree_rd .== outdegree_rd[1]) rd = random_regular_digraph(1000, 4, dir=:in) @test nv(rd) == 1000 @test ne(rd) == 4000 @test is_directed(rd) - @test std(indegree(rd)) == 0 + indegree_rd = @inferred(indegree(rd)) + @test all(indegree_rd .== indegree_rd[1]) rr = random_regular_graph(10, 8, seed=4) @test nv(rr) == 10 @@ -254,7 +256,7 @@ bp = blockfractions(sbm, g) ./ (sizes * sizes') ratios = bp ./ (sbm.affinities ./ sum(sbm.affinities)) test_sbm(sbm, bp) - @test norm(collect(ratios)) < 0.25 + @test LinearAlgebra.norm(collect(ratios)) < 0.25 sizes = [200, 200, 100] internaldeg = 15 @@ -272,14 +274,14 @@ bp = blockfractions(sbm, g) ./ (sizes * sizes') test_sbm(sbm, bp) ratios = bp ./ (sbm.affinities ./ sum(sbm.affinities)) - @test norm(collect(ratios)) < 0.25 + @test LinearAlgebra.norm(collect(ratios)) < 0.25 # check that average degree is not too high # factor of two is cushion for random process @test mean(degree(g)) <= 4//2*numedges/sum(sizes) # check that the internal degrees are higher than the external degrees # 5//4 is cushion for random process. - @test all(sum(bc-diagm(0=>diag(bc)), dims=1) .<= 5//4 .* diag(bc)) + @test all(sum(bc-LinearAlgebra.diagm(0=>diag(bc)), dims=1) .<= 5//4 .* diag(bc)) sbm2 = StochasticBlockModel(0.5*ones(4), 0.3, 10*ones(Int,4)) diff --git a/test/graphcut/normalized_cut.jl b/test/graphcut/normalized_cut.jl index 25701effb..6eaeedd82 100644 --- a/test/graphcut/normalized_cut.jl +++ b/test/graphcut/normalized_cut.jl @@ -33,7 +33,7 @@ @test labels == [1, 1, 1, 2, 2, 2] || labels == [2, 2, 2, 1, 1, 1] end - w = SparseMatrixCSC(w) + w = SparseArrays.SparseMatrixCSC(w) for g in testgraphs(gx) labels = @inferred(normalized_cut(g, 1, w)) @test labels == [1, 1, 1, 2, 2, 2] || labels == [2, 2, 2, 1, 1, 1] @@ -53,7 +53,7 @@ @test labels == [1, 1, 2, 2] || labels == [2, 2, 1, 1] end - w = SparseMatrixCSC(w) + w = SparseArrays.SparseMatrixCSC(w) for g in testgraphs(gx) labels = @inferred(normalized_cut(g, 0.1, w)) @test labels == [1, 1, 2, 2] || labels == [2, 2, 1, 1] diff --git a/test/linalg/graphmatrices.jl b/test/linalg/graphmatrices.jl index 00ab3fd83..47ca0cbca 100644 --- a/test/linalg/graphmatrices.jl +++ b/test/linalg/graphmatrices.jl @@ -17,10 +17,10 @@ adjmat, stochmat, adjhat, avgmat = constructors(mat) @test adjmat.D == vec(sum(mat, dims=1)) @test adjmat.A == mat - @test isa(sparse(mat), SparseMatrixCSC) - @test isa(sparse(stochmat), SparseMatrixCSC) - @test isa(sparse(adjhat), SparseMatrixCSC) - @test isa(sparse(avgmat), SparseMatrixCSC) + @test isa(sparse(mat), SparseArrays.SparseMatrixCSC) + @test isa(sparse(stochmat), SparseArrays.SparseMatrixCSC) + @test isa(sparse(adjhat), SparseArrays.SparseMatrixCSC) + @test isa(sparse(avgmat), SparseArrays.SparseMatrixCSC) @test isa(convert(CombinatorialAdjacency, adjmat), CombinatorialAdjacency) @test isa(convert(CombinatorialAdjacency, avgmat), CombinatorialAdjacency) @test prescalefactor(adjhat) == postscalefactor(adjhat) @@ -94,13 +94,13 @@ @test g(NormalizedLaplacian(adjhat)) > 1e-13 @test g(StochasticLaplacian(stochmat)) > 1e-13 - @test eigs(adjmat, which=:LR)[1][1] > 1.0 - @test eigs(stochmat, which=:LR)[1][1] ≈ 1.0 - @test eigs(avgmat, which=:LR)[1][1] ≈ 1.0 - @test eigs(lapl, which=:LR)[1][1] > 2.0 - @test_throws MethodError eigs(lapl, which=:SM)[1][1] # --> greater_than(-0.0) + @test IterativeEigensolvers.eigs(adjmat, which=:LR)[1][1] > 1.0 + @test IterativeEigensolvers.eigs(stochmat, which=:LR)[1][1] ≈ 1.0 + @test IterativeEigensolvers.eigs(avgmat, which=:LR)[1][1] ≈ 1.0 + @test IterativeEigensolvers.eigs(lapl, which=:LR)[1][1] > 2.0 + @test_throws MethodError IterativeEigensolvers.eigs(lapl, which=:SM)[1][1] # --> greater_than(-0.0) lhat = NormalizedLaplacian(adjhat) - @test eigs(lhat, which=:LR)[1][1] < 2.0 + 1e-9 + @test IterativeEigensolvers.eigs(lhat, which=:LR)[1][1] < 2.0 + 1e-9 end function test_other(mat, n) @@ -141,8 +141,8 @@ @test_throws MethodError symmetrize(NormalizedAdjacency(adjmat)).A # --> adjmat.A @test symmetrize(adjmat).A == adjmat.A # these tests are basically the code - @test symmetrize(adjmat, :triu).A == triu(adjmat.A) + triu(adjmat.A)' - @test symmetrize(adjmat, :tril).A == tril(adjmat.A) + tril(adjmat.A)' + @test symmetrize(adjmat, :triu).A == LinearAlgebra.triu(adjmat.A) + LinearAlgebra.triu(adjmat.A)' + @test symmetrize(adjmat, :tril).A == LinearAlgebra.tril(adjmat.A) + LinearAlgebra.tril(adjmat.A)' @test symmetrize(adjmat, :sum).A == adjmat.A + adjmat.A @test_throws ArgumentError symmetrize(adjmat, :fake) @@ -152,16 +152,16 @@ adjmat = CombinatorialAdjacency(mat) ahatp = PunchedAdjacency(adjmat) y = ahatp * perron(ahatp) - @test dot(y, ahatp.perron) ≈ 0.0 atol = 1.0e-8 + @test LinearAlgebra.dot(y, ahatp.perron) ≈ 0.0 atol = 1.0e-8 @test sum(abs, y) ≈ 0.0 atol = 1.0e-8 - eval, evecs = eigs(ahatp, which=:LM) + eval, evecs = IterativeEigensolvers.eigs(ahatp, which=:LM) @test eval[1] - (1 + 1.0e-8) <= 0 - @test dot(perron(ahatp), evecs[:, 1]) ≈ 0.0 atol = 1e-8 + @test LinearAlgebra.dot(perron(ahatp), evecs[:, 1]) ≈ 0.0 atol = 1e-8 ahat = ahatp.A @test isa(ahat, NormalizedAdjacency) z = ahatp * perron(ahat) - @test norm(z) ≈ 0.0 atol = 1e-8 + @test LinearAlgebra.norm(z) ≈ 0.0 atol = 1e-8 end @@ -183,7 +183,7 @@ """Computes the stationary distribution of a random walk""" function stationarydistribution(R::StochasticAdjacency; kwargs...) - er = eigs(R, nev=1, which=:LR; kwargs...) + er = IterativeEigensolvers.eigs(R, nev=1, which=:LR; kwargs...) l1 = er[1][1] abs(l1 - 1) < 1e-8 || error("failed to compute stationary distribution") # TODO 0.7: should we change the error type to InexactError? p = real(er[2][:, 1]) @@ -204,7 +204,7 @@ n = 100 p = 16 / n M = sprand(n, n, p) - M.nzval[:] = 1.0 + M.nzval[:] .= 1.0 A = CombinatorialAdjacency(M) sd = stationarydistribution(A; ncv=10) @test all(sd .>= 0) diff --git a/test/linalg/spectral.jl b/test/linalg/spectral.jl index 2147c3464..ae33b258a 100644 --- a/test/linalg/spectral.jl +++ b/test/linalg/spectral.jl @@ -93,9 +93,9 @@ Matrix(nbt::Nonbacktracking) = Matrix(sparse(nbt)) T = eltype(g) amat = adjacency_matrix(g, Float64; dir=dir) lmat = laplacian_matrix(g, Float64; dir=dir) - @test isa(amat, SparseMatrixCSC{Float64,T}) - @test isa(lmat, SparseMatrixCSC{Float64,T}) - evals = eigvals(Matrix(lmat)) + @test isa(amat, SparseArrays.SparseMatrixCSC{Float64,T}) + @test isa(lmat, SparseArrays.SparseMatrixCSC{Float64,T}) + evals = LinearAlgebra.eigvals(Matrix(lmat)) @test all(evals .>= -1e-15) # positive semidefinite @test (minimum(evals)) ≈ 0 atol = 1e-13 end @@ -133,20 +133,20 @@ Matrix(nbt::Nonbacktracking) = Matrix(sparse(nbt)) B, emap = non_backtracking_matrix(g) Bs = sparse(nbt) @test sparse(B) == Bs - @test eigs(nbt, nev=1)[1] ≈ eigs(B, nev=1)[1] atol = 1e-5 + @test IterativeEigensolvers.eigs(nbt, nev=1)[1] ≈ IterativeEigensolvers.eigs(B, nev=1)[1] atol = 1e-5 # check that matvec works x = ones(Float64, nbt.m) y = nbt * x z = B * x - @test norm(y - z) < 1e-8 + @test LinearAlgebra.norm(y - z) < 1e-8 #check that matmat works and Matrix(nbt) == B - @test norm(nbt * LightGraphs.eye(nbt.m) - B) < 1e-8 + @test LinearAlgebra.norm(nbt * Matrix{Float64}(LinearAlgebra.I, nbt.m, nbt.m) - B) < 1e-8 #check that matmat works and Matrix(nbt) == B - @test norm(nbt * LightGraphs.eye(nbt.m) - B) < 1e-8 + @test LinearAlgebra.norm(nbt * Matrix{Float64}(LinearAlgebra.I, nbt.m, nbt.m) - B) < 1e-8 #check that we can use the implicit matvec in nonbacktrack_embedding @test size(y) == size(x) @@ -156,7 +156,6 @@ Matrix(nbt::Nonbacktracking) = Matrix(sparse(nbt)) @test Matrix(B₁) == Matrix(B) @test B₁ * ones(size(B₁)[2]) == B * ones(size(B)[2]) @test size(B₁) == size(B) - # @test norm(eigs(B₁)[1] - eigs(B)[1]) ≈ 0.0 atol=1e-8 @test !issymmetric(B₁) @test eltype(B₁) == Float64 end diff --git a/test/simplegraphs/simplegraphs.jl b/test/simplegraphs/simplegraphs.jl index 9a56381e9..73502af8b 100644 --- a/test/simplegraphs/simplegraphs.jl +++ b/test/simplegraphs/simplegraphs.jl @@ -1,4 +1,4 @@ -using Random +import Random @testset "SimpleGraphs" begin adjmx1 = [0 1 0; 1 0 1; 0 1 0] # graph @@ -194,8 +194,8 @@ using Random # We create an edge list, shuffle it and reverse half of its edges # using this edge list should result in the same graph edge_list = [e for e in edges(g)] - shuffle!(MersenneTwister(0), edge_list) - for i in rand(MersenneTwister(0), 1:length(edge_list), length(edge_list) ÷ 2) + Random.shuffle!(Random.MersenneTwister(0), edge_list) + for i in rand(Random.MersenneTwister(0), 1:length(edge_list), length(edge_list) ÷ 2) e = edge_list[i] Te = typeof(e) edge_list[i] = Te(dst(e), src(e)) @@ -229,7 +229,7 @@ using Random for g in testdigraphs(g_dir) # We create an edge list and shuffle it edge_list = [e for e in edges(g)] - shuffle!(MersenneTwister(0), edge_list) + Random.shuffle!(Random.MersenneTwister(0), edge_list) edge_iter = (e for e in edge_list) edge_set = Set(edge_list) From 646c007a6a95f01d0afa30fb914f8672c9dd8fae Mon Sep 17 00:00:00 2001 From: Seth Bromberger Date: Wed, 30 May 2018 11:37:39 -0700 Subject: [PATCH 2/4] graphmatrices problems --- src/LightGraphs.jl | 17 +-- src/SimpleGraphs/simplegraph.jl | 28 ++-- src/centrality/betweenness.jl | 2 +- src/centrality/closeness.jl | 12 +- src/centrality/katz.jl | 2 +- src/centrality/radiality.jl | 8 +- src/centrality/stress.jl | 2 +- src/distance.jl | 42 +++--- src/generators/randgraphs.jl | 6 +- src/generators/staticgraphs.jl | 4 +- src/graphcut/normalized_cut.jl | 4 +- src/linalg/LinAlg.jl | 2 - src/linalg/graphmatrices.jl | 154 +++++++++---------- src/linalg/nonbacktracking.jl | 10 +- src/linalg/spectral.jl | 6 +- src/operators.jl | 50 +++--- src/shortestpaths/dijkstra.jl | 37 +++-- src/shortestpaths/johnson.jl | 33 ++-- src/traversals/greedy_color.jl | 2 +- test/biconnectivity/articulation.jl | 14 +- test/biconnectivity/biconnect.jl | 10 +- test/community/core-periphery.jl | 2 +- test/community/label_propagation.jl | 20 +-- test/generators/randgraphs.jl | 44 +++--- test/generators/staticgraphs.jl | 12 +- test/linalg/graphmatrices.jl | 20 +-- test/linalg/spectral.jl | 20 +-- test/operators.jl | 12 +- test/shortestpaths/astar.jl | 2 +- test/shortestpaths/bellman-ford.jl | 2 +- test/shortestpaths/dijkstra.jl | 226 ++++++++++++++-------------- test/shortestpaths/yen.jl | 2 +- test/traversals/bipartition.jl | 2 +- 33 files changed, 397 insertions(+), 412 deletions(-) diff --git a/src/LightGraphs.jl b/src/LightGraphs.jl index 6d8f4a03d..f6af17a99 100644 --- a/src/LightGraphs.jl +++ b/src/LightGraphs.jl @@ -5,21 +5,20 @@ using SimpleTraits import CodecZlib import DataStructures -import SharedArrays -import SparseArrays -import LinearAlgebra +import DelimitedFiles +import Distributed import IterativeEigensolvers -import SharedArrays +import LinearAlgebra import Markdown -import DelimitedFiles +import Random +import SharedArrays +import SparseArrays + import Base: write, ==, <, *, ≈, convert, isless, issubset, union, intersect, reverse, reverse!, isassigned, getindex, setindex!, show, print, copy, in, sum, size, eltype, length, ndims, transpose, ctranspose, join, start, next, done, eltype, get, Pair, Tuple, zero -import Random -import Distributed: @distributed, @sync -import SparseArrays: sparse, blockdiag -import LinearAlgebra: issymmetric, mul!, Diagonal + export # Interface diff --git a/src/SimpleGraphs/simplegraph.jl b/src/SimpleGraphs/simplegraph.jl index c889ca577..807159915 100644 --- a/src/SimpleGraphs/simplegraph.jl +++ b/src/SimpleGraphs/simplegraph.jl @@ -5,7 +5,7 @@ const SimpleGraphEdge = SimpleEdge A type representing an undirected graph. """ -mutable struct SimpleGraph{T<:Integer} <: AbstractSimpleGraph{T} +mutable struct SimpleGraph{T <: Integer} <: AbstractSimpleGraph{T} ne::Int fadjlist::Vector{Vector{T}} # [src]: (dst, dst, dst) end @@ -13,7 +13,7 @@ end eltype(x::SimpleGraph{T}) where T = T # Graph{UInt8}(6), Graph{Int16}(7), Graph{UInt8}() -function SimpleGraph{T}(n::Integer = 0) where T<:Integer +function SimpleGraph{T}(n::Integer=0) where T <: Integer fadjlist = [Vector{T}() for _ = one(T):n] return SimpleGraph{T}(0, fadjlist) end @@ -22,26 +22,26 @@ end SimpleGraph() = SimpleGraph{Int}() # SimpleGraph(6), SimpleGraph(0x5) -SimpleGraph(n::T) where T<:Integer = SimpleGraph{T}(n) +SimpleGraph(n::T) where T <: Integer = SimpleGraph{T}(n) # SimpleGraph(UInt8) -SimpleGraph(::Type{T}) where T<:Integer = SimpleGraph{T}(zero(T)) +SimpleGraph(::Type{T}) where T <: Integer = SimpleGraph{T}(zero(T)) # Graph{UInt8}(adjmx) -function SimpleGraph{T}(adjmx::AbstractMatrix) where T<:Integer +function SimpleGraph{T}(adjmx::AbstractMatrix) where T <: Integer dima, dimb = size(adjmx) isequal(dima, dimb) || throw(ArgumentError("Adjacency / distance matrices must be square")) - issymmetric(adjmx) || throw(ArgumentError("Adjacency / distance matrices must be symmetric")) + LinearAlgebra.issymmetric(adjmx) || throw(ArgumentError("Adjacency / distance matrices must be symmetric")) g = SimpleGraph(T(dima)) - @inbounds for i in findall(triu(adjmx).!=0) + @inbounds for i in findall(triu(adjmx) .!= 0) add_edge!(g, i[1], i[2]) end return g end # converts Graph{Int} to Graph{Int32} -function SimpleGraph{T}(g::SimpleGraph) where T<:Integer +function SimpleGraph{T}(g::SimpleGraph) where T <: Integer h_fadj = [Vector{T}(x) for x in fadj(g)] return SimpleGraph(ne(g), h_fadj) end @@ -71,7 +71,7 @@ function SimpleGraph(g::SimpleDiGraph) return SimpleGraph(edgect ÷ 2, newfadj) end -@inbounds function cleanupedges!(fadjlist::Vector{Vector{T}}) where T<:Integer +@inbounds function cleanupedges!(fadjlist::Vector{Vector{T}}) where T <: Integer neg = 0 for v in 1:length(fadjlist) if !issorted(fadjlist[v]) @@ -90,7 +90,7 @@ end return neg ÷ 2 end -function SimpleGraph(edge_list::Vector{SimpleGraphEdge{T}}) where T<:Integer +function SimpleGraph(edge_list::Vector{SimpleGraphEdge{T}}) where T <: Integer nvg = zero(T) @inbounds( for e in edge_list @@ -136,10 +136,10 @@ function SimpleGraph(edge_list::Vector{SimpleGraphEdge{T}}) where T<:Integer end -@inbounds function add_to_fadjlist!(fadjlist::Vector{Vector{T}}, s::T, d::T) where T<:Integer +@inbounds function add_to_fadjlist!(fadjlist::Vector{Vector{T}}, s::T, d::T) where T <: Integer nvg = length(fadjlist) nvg_new = max(nvg, s, d) - for v = (nvg+1):nvg_new + for v = (nvg + 1):nvg_new push!(fadjlist, Vector{T}()) end @@ -174,7 +174,7 @@ function _SimpleGraphFromIterator(iter)::SimpleGraph return g end -function _SimpleGraphFromIterator(iter, ::Type{SimpleGraphEdge{T}}) where T<:Integer +function _SimpleGraphFromIterator(iter, ::Type{SimpleGraphEdge{T}}) where T <: Integer fadjlist = Vector{Vector{T}}() @inbounds( for e in iter @@ -209,7 +209,7 @@ function SimpleGraphFromIterator(iter)::SimpleGraph end -edgetype(::SimpleGraph{T}) where T<:Integer = SimpleGraphEdge{T} +edgetype(::SimpleGraph{T}) where T <: Integer = SimpleGraphEdge{T} """ badj(g::SimpleGraph[, v::Integer]) diff --git a/src/centrality/betweenness.jl b/src/centrality/betweenness.jl index 6823c91ae..d525e0191 100644 --- a/src/centrality/betweenness.jl +++ b/src/centrality/betweenness.jl @@ -75,7 +75,7 @@ function parallel_betweenness_centrality( # Parallel reduction - betweenness = @distributed (+) for s in vs + betweenness = Distributed.@distributed (+) for s in vs temp_betweenness = zeros(n_v) if degree(g, s) > 0 # this might be 1? state = dijkstra_shortest_paths(g, s, distmx; allpaths=true, trackvertices=true) diff --git a/src/centrality/closeness.jl b/src/centrality/closeness.jl index bdaa3c7d8..dfd40eb53 100644 --- a/src/centrality/closeness.jl +++ b/src/centrality/closeness.jl @@ -9,9 +9,8 @@ of the graph `g`. Return a vector representing the centrality calculated for eac node `n` by ``\\frac{|δ_n|}{|V|-1}``, where ``δ_n`` is the set of vertices reachable from node `n`. """ -function closeness_centrality( - g::AbstractGraph, - distmx::AbstractMatrix = weights(g); +function closeness_centrality(g::AbstractGraph, + distmx::AbstractMatrix=weights(g); normalize=true) n_v = nv(g) @@ -37,16 +36,15 @@ function closeness_centrality( return closeness end -function parallel_closeness_centrality( - g::AbstractGraph, - distmx::AbstractMatrix = weights(g); +function parallel_closeness_centrality(g::AbstractGraph, + distmx::AbstractMatrix=weights(g); normalize=true)::Vector{Float64} n_v = Int(nv(g)) closeness = SharedArrays.SharedVector{Float64}(n_v) - @sync @distributed for u in vertices(g) + Distributed.@sync Distributed.@distributed for u in vertices(g) if degree(g, u) == 0 # no need to do Dijkstra here closeness[u] = 0.0 else diff --git a/src/centrality/katz.jl b/src/centrality/katz.jl index 11d9cc4f4..5f6f43dca 100644 --- a/src/centrality/katz.jl +++ b/src/centrality/katz.jl @@ -30,7 +30,7 @@ the centrality calculated for each node in `g`. function katz_centrality(g::AbstractGraph, α::Real=0.3) nvg = nv(g) v = ones(Float64, nvg) - spI = sparse(one(Float64) * LinearAlgebra.I, nvg, nvg) + spI = SparseArrays.sparse(one(Float64) * LinearAlgebra.I, nvg, nvg) A = adjacency_matrix(g, Bool; dir=:in) v = (spI - α * A) \ v v /= LinearAlgebra.norm(v) diff --git a/src/centrality/radiality.jl b/src/centrality/radiality.jl index 8cc4c775e..11395ffb3 100644 --- a/src/centrality/radiality.jl +++ b/src/centrality/radiality.jl @@ -28,8 +28,8 @@ function radiality_centrality(g::AbstractGraph)::Vector{Float64} dmtr = max(dmtr, maximum(d.dists)) meandists[v] = sum(d.dists) / (n_v - 1) # ignore the source vx end - meandists = (dmtr + 1).-(meandists) - return meandists./dmtr + meandists = (dmtr + 1) .- (meandists) + return meandists ./ dmtr end function parallel_radiality_centrality(g::AbstractGraph)::Vector{Float64} @@ -39,12 +39,12 @@ function parallel_radiality_centrality(g::AbstractGraph)::Vector{Float64} meandists = SharedArrays.SharedVector{Float64}(Int(n_v)) maxdists = SharedArrays.SharedVector{Float64}(Int(n_v)) - @sync @distributed for i = 1:n_v + Distributed.@sync Distributed.@distributed for i = 1:n_v d = dijkstra_shortest_paths(g, vs[i]) maxdists[i] = maximum(d.dists) meandists[i] = sum(d.dists) / (n_v - 1) end dmtr = maximum(maxdists) radialities = collect(meandists) - return ((dmtr + 1).-radialities)./dmtr + return ((dmtr + 1) .- radialities) ./ dmtr end diff --git a/src/centrality/stress.jl b/src/centrality/stress.jl index 8139e39df..287c56b3d 100644 --- a/src/centrality/stress.jl +++ b/src/centrality/stress.jl @@ -43,7 +43,7 @@ function parallel_stress_centrality( # Parallel reduction - stress = @distributed (+) for s in vs + stress = Distributed.@distributed (+) for s in vs temp_stress = zeros(Int, n_v) if degree(g, s) > 0 # this might be 1? state = dijkstra_shortest_paths(g, s; allpaths=true, trackvertices=true) diff --git a/src/distance.jl b/src/distance.jl index ecf06689f..e53b89e18 100644 --- a/src/distance.jl +++ b/src/distance.jl @@ -5,7 +5,7 @@ An array-like structure that provides distance values of `1` for any `src, dst` combination. """ -struct DefaultDistance<:AbstractMatrix{Int} +struct DefaultDistance <: AbstractMatrix{Int} nv::Int DefaultDistance(nv::Int=typemax(Int)) = new(nv) end @@ -50,35 +50,29 @@ store, and pass the eccentricities if multiple distance measures are desired. An infinite path length is represented by the `typemax` of the distance matrix. """ -function eccentricity( - g::AbstractGraph, +function eccentricity(g::AbstractGraph, v::Integer, - distmx::AbstractMatrix{T} = weights(g) -) where T <: Real + distmx::AbstractMatrix{T}=weights(g)) where T <: Real e = maximum(dijkstra_shortest_paths(g, v, distmx).dists) e == typemax(T) && warn("Infinite path length detected for vertex $v") return e end -eccentricity( - g::AbstractGraph, - vs::AbstractVector = vertices(g), - distmx::AbstractMatrix = weights(g) -) = [eccentricity(g, v, distmx) for v in vs] +eccentricity(g::AbstractGraph, + vs::AbstractVector=vertices(g), + distmx::AbstractMatrix=weights(g)) = [eccentricity(g, v, distmx) for v in vs] eccentricity(g::AbstractGraph, distmx::AbstractMatrix) = eccentricity(g, vertices(g), distmx) -function parallel_eccentricity( - g::AbstractGraph, - vs::AbstractVector = vertices(g), - distmx::AbstractMatrix{T} = weights(g) -) where T <: Real +function parallel_eccentricity(g::AbstractGraph, + vs::AbstractVector=vertices(g), + distmx::AbstractMatrix{T}=weights(g)) where T <: Real vlen = length(vs) eccs = SharedArrays.SharedVector{T}(vlen) - @sync @distributed for i = 1:vlen + Distributed.@sync Distributed.@distributed for i = 1:vlen eccs[i] = maximum(dijkstra_shortest_paths(g, vs[i], distmx).dists) end d = SharedArrays.sdata(eccs) @@ -98,9 +92,9 @@ Given a graph and optional distance matrix, or a vector of precomputed eccentricities, return the maximum eccentricity of the graph. """ diameter(eccentricities::Vector) = maximum(eccentricities) -diameter(g::AbstractGraph, distmx::AbstractMatrix = weights(g)) = +diameter(g::AbstractGraph, distmx::AbstractMatrix=weights(g)) = maximum(eccentricity(g, distmx)) -parallel_diameter(g::AbstractGraph, distmx::AbstractMatrix = weights(g)) = +parallel_diameter(g::AbstractGraph, distmx::AbstractMatrix=weights(g)) = maximum(parallel_eccentricity(g, distmx)) """ @@ -118,10 +112,10 @@ function periphery(eccentricities::Vector) return filter(x -> eccentricities[x] == diam, 1:length(eccentricities)) end -periphery(g::AbstractGraph, distmx::AbstractMatrix = weights(g)) = +periphery(g::AbstractGraph, distmx::AbstractMatrix=weights(g)) = periphery(eccentricity(g, distmx)) -parallel_periphery(g::AbstractGraph, distmx::AbstractMatrix = weights(g)) = +parallel_periphery(g::AbstractGraph, distmx::AbstractMatrix=weights(g)) = periphery(parallel_eccentricity(g, distmx)) """ @@ -134,9 +128,9 @@ Given a graph and optional distance matrix, or a vector of precomputed eccentricities, return the minimum eccentricity of the graph. """ radius(eccentricities::Vector) = minimum(eccentricities) -radius(g::AbstractGraph, distmx::AbstractMatrix = weights(g)) = +radius(g::AbstractGraph, distmx::AbstractMatrix=weights(g)) = minimum(eccentricity(g, distmx)) -parallel_radius(g::AbstractGraph, distmx::AbstractMatrix = weights(g)) = +parallel_radius(g::AbstractGraph, distmx::AbstractMatrix=weights(g)) = minimum(parallel_eccentricity(g, distmx)) """ @@ -153,8 +147,8 @@ function center(eccentricities::Vector) return filter(x -> eccentricities[x] == rad, 1:length(eccentricities)) end -center(g::AbstractGraph, distmx::AbstractMatrix = weights(g)) = +center(g::AbstractGraph, distmx::AbstractMatrix=weights(g)) = center(eccentricity(g, distmx)) -parallel_center(g::AbstractGraph, distmx::AbstractMatrix = weights(g)) = +parallel_center(g::AbstractGraph, distmx::AbstractMatrix=weights(g)) = center(parallel_eccentricity(g, distmx)) diff --git a/src/generators/randgraphs.jl b/src/generators/randgraphs.jl index a15d46a4d..ac05d80c2 100644 --- a/src/generators/randgraphs.jl +++ b/src/generators/randgraphs.jl @@ -643,9 +643,9 @@ function random_regular_digraph(n::Integer, k::Integer; dir::Symbol=:out, seed:: end if dir == :out - return SimpleDiGraph(sparse(I, J, V, n, n)) + return SimpleDiGraph(SparseArrays.sparse(I, J, V, n, n)) else - return SimpleDiGraph(sparse(I, J, V, n, n)') + return SimpleDiGraph(SparseArrays.sparse(I, J, V, n, n)') end end @@ -897,7 +897,7 @@ function blockcounts(sbm::StochasticBlockModel, A::AbstractMatrix) I = collect(1:sbm.n) J = [sbm.nodemap[i] for i in 1:sbm.n] V = ones(sbm.n) - Q = sparse(I, J, V) + Q = SparseArrays.sparse(I, J, V) # Q = Q / Q'Q # @show Q'Q# < 1e-6 return (Q'A) * Q diff --git a/src/generators/staticgraphs.jl b/src/generators/staticgraphs.jl index f35379cf0..8ffa45629 100644 --- a/src/generators/staticgraphs.jl +++ b/src/generators/staticgraphs.jl @@ -222,7 +222,7 @@ Create a double complete binary tree with `k` levels. function DoubleBinaryTree(k::Integer) gl = BinaryTree(k) gr = BinaryTree(k) - g = blockdiag(gl, gr) + g = SparseArrays.blockdiag(gl, gr) add_edge!(g, 1, nv(gl) + 1) return g end @@ -241,7 +241,7 @@ function RoachGraph(k::Integer) nopole = SimpleGraph(2) antannae = crosspath(k, nopole) body = crosspath(k, dipole) - roach = blockdiag(antannae, body) + roach = SparseArrays.blockdiag(antannae, body) add_edge!(roach, nv(antannae) - 1, nv(antannae) + 1) add_edge!(roach, nv(antannae), nv(antannae) + 2) return roach diff --git a/src/graphcut/normalized_cut.jl b/src/graphcut/normalized_cut.jl index 8e9658be7..cee7b260c 100644 --- a/src/graphcut/normalized_cut.jl +++ b/src/graphcut/normalized_cut.jl @@ -105,8 +105,8 @@ function _partition_weightmx(cut, W::SparseArrays.SparseMatrixCSC) end end end - W1 = sparse(I1, J1, V1) - W2 = sparse(I2, J2, V2) + W1 = SparseArrays.sparse(I1, J1, V1) + W2 = SparseArrays.sparse(I2, J2, V2) return (W1, W2, vmap1, vmap2) end diff --git a/src/linalg/LinAlg.jl b/src/linalg/LinAlg.jl index bb2238fdb..987d11ed1 100644 --- a/src/linalg/LinAlg.jl +++ b/src/linalg/LinAlg.jl @@ -10,8 +10,6 @@ import LightGraphs: IsDirected, AbstractGraph, inneighbors, outneighbors, all_neighbors, is_directed, nv, ne, has_edge, vertices import Base: convert, size, eltype, ndims, ==, *, .*, length -import SparseArrays: sparse, diag -import LinearAlgebra: issymmetric, mul!, Diagonal export convert, SparseMatrix, diff --git a/src/linalg/graphmatrices.jl b/src/linalg/graphmatrices.jl index a84efd0c7..08e40499f 100644 --- a/src/linalg/graphmatrices.jl +++ b/src/linalg/graphmatrices.jl @@ -30,13 +30,13 @@ abstract type Laplacian{T} <: GraphMatrix{T} end The standard adjacency matrix. """ struct CombinatorialAdjacency{T,S,V} <: Adjacency{T} - A::S - D::V + A::S + D::V end function CombinatorialAdjacency(A::SparseMatrix{T}) where T - D = vec(sum(A, dims=1)) - return CombinatorialAdjacency{T,SparseMatrix{T},typeof(D)}(A, D) + D = vec(sum(A, dims=1)) + return CombinatorialAdjacency{T,SparseMatrix{T},typeof(D)}(A, D) end @@ -48,12 +48,12 @@ If A is symmetric, then the normalized adjacency is also symmetric with real eigenvalues bounded by [-1, 1]. """ struct NormalizedAdjacency{T} <: Adjacency{T} - A::CombinatorialAdjacency{T} - scalefactor::Vector{T} + A::CombinatorialAdjacency{T} + scalefactor::Vector{T} end function NormalizedAdjacency(adjmat::CombinatorialAdjacency) - sf = adjmat.D.^(-1 / 2) - return NormalizedAdjacency(adjmat, sf) + sf = adjmat.D.^(-1 / 2) + return NormalizedAdjacency(adjmat, sf) end """ @@ -62,13 +62,13 @@ end A transition matrix for the random walk. """ struct StochasticAdjacency{T} <: Adjacency{T} - A::CombinatorialAdjacency{T} - scalefactor::Vector{T} + A::CombinatorialAdjacency{T} + scalefactor::Vector{T} end function StochasticAdjacency(adjmat::CombinatorialAdjacency) - sf = adjmat.D.^(-1) - return StochasticAdjacency(adjmat, sf) + sf = adjmat.D.^(-1) + return StochasticAdjacency(adjmat, sf) end """ @@ -77,23 +77,23 @@ end The matrix whose action is to average over each neighborhood. """ struct AveragingAdjacency{T} <: Adjacency{T} - A::CombinatorialAdjacency{T} - scalefactor::Vector{T} + A::CombinatorialAdjacency{T} + scalefactor::Vector{T} end function AveragingAdjacency(adjmat::CombinatorialAdjacency) - sf = adjmat.D.^(-1) - return AveragingAdjacency(adjmat, sf) + sf = adjmat.D.^(-1) + return AveragingAdjacency(adjmat, sf) end perron(adjmat::NormalizedAdjacency) = sqrt.(adjmat.A.D) / LinearAlgebra.norm(sqrt.(adjmat.A.D)) struct PunchedAdjacency{T} <: Adjacency{T} - A::NormalizedAdjacency{T} - perron::Vector{T} + A::NormalizedAdjacency{T} + perron::Vector{T} end function PunchedAdjacency(adjmat::CombinatorialAdjacency) - perron = sqrt.(adjmat.D) / LinearAlgebra.norm(sqrt.(adjmat.D)) - return PunchedAdjacency(NormalizedAdjacency(adjmat), perron) + perron = sqrt.(adjmat.D) / LinearAlgebra.norm(sqrt.(adjmat.D)) + return PunchedAdjacency(NormalizedAdjacency(adjmat), perron) end perron(m::PunchedAdjacency) = m.perron @@ -109,7 +109,7 @@ different scaled GraphMatrix types. """ struct Noop end -Base.broadcast(::typeof(*), ::Noop, x) = x +Broadcast.broadcasted(::typeof(*), ::Noop, x) = x LinearAlgebra.Diagonal(::Noop) = Noop() @@ -130,7 +130,7 @@ prescalefactor(adjmat::StochasticAdjacency) = adjmat.scalefactor struct CombinatorialLaplacian{T} <: Laplacian{T} - A::CombinatorialAdjacency{T} + A::CombinatorialAdjacency{T} end """ @@ -141,7 +141,7 @@ If A is symmetric, then the normalized Laplacian is also symmetric with positive eigenvalues bounded by 2. """ struct NormalizedLaplacian{T} <: Laplacian{T} - A::NormalizedAdjacency{T} + A::NormalizedAdjacency{T} end """ @@ -150,7 +150,7 @@ end Laplacian version of the StochasticAdjacency matrix. """ struct StochasticLaplacian{T} <: Laplacian{T} - A::StochasticAdjacency{T} + A::StochasticAdjacency{T} end """ @@ -159,17 +159,17 @@ end Laplacian version of the AveragingAdjacency matrix. """ struct AveragingLaplacian{T} <: Laplacian{T} - A::AveragingAdjacency{T} + A::AveragingAdjacency{T} end -arrayfunctions = (:eltype, :length, :ndims, :size, :strides, :issymmetric) +arrayfunctions = (:eltype, :length, :ndims, :size, :strides) for f in arrayfunctions - @eval $f(a::GraphMatrix) = $f(a.A) + @eval $f(a::GraphMatrix) = $f(a.A) end - +LinearAlgebra.issymmetric(a::GraphMatrix) = LinearAlgebra.issymmetric(a.A) size(a::GraphMatrix, i::Integer) = size(a.A, i) -issymmetric(::StochasticAdjacency) = false -issymmetric(::AveragingAdjacency) = false +LinearAlgebra.issymmetric(::StochasticAdjacency) = false +LinearAlgebra.issymmetric(::AveragingAdjacency) = false """ degrees(adjmat) @@ -194,52 +194,52 @@ convert(::Type{CombinatorialAdjacency}, adjmat::Adjacency) = adjmat.A convert(::Type{CombinatorialAdjacency}, adjmat::CombinatorialAdjacency) = adjmat -function sparse(lapl::M) where M<:Laplacian - adjmat = adjacency(lapl) - A = sparse(adjmat) - L = sparse(LinearAlgebra.Diagonal(diag(lapl))) - A - return L +function SparseArrays.sparse(lapl::M) where M <: Laplacian + adjmat = adjacency(lapl) + A = SparseArrays.sparse(adjmat) + L = SparseArrays.sparse(LinearAlgebra.Diagonal(SparseArrays.diag(lapl))) - A + return L end -function SparseMatrix(lapl::M) where M<:GraphMatrix - return sparse(lapl) +function SparseMatrix(lapl::M) where M <: GraphMatrix + return SparseArrays.sparse(lapl) end -function sparse(adjmat::Adjacency) - A = sparse(adjmat.A) - return LinearAlgebra.Diagonal(prescalefactor(adjmat)) * (A * Diagonal(postscalefactor(adjmat))) +function SparseArrays.sparse(adjmat::Adjacency) + A = SparseArrays.sparse(adjmat.A) + return LinearAlgebra.Diagonal(prescalefactor(adjmat)) * (A * LinearAlgebra.Diagonal(postscalefactor(adjmat))) end function convert(::Type{SparseMatrix{T}}, lapl::Laplacian{T}) where T - adjmat = adjacency(lapl) - A = convert(SparseMatrix{T}, adjmat) - L = sparse(LinearAlgebra.Diagonal(diag(lapl))) - A - return L + adjmat = adjacency(lapl) + A = convert(SparseMatrix{T}, adjmat) + L = SparseArrays.sparse(LinearAlgebra.Diagonal(SparseArrays.diag(lapl))) - A + return L end -diag(lapl::CombinatorialLaplacian) = lapl.A.D -diag(lapl::Laplacian) = ones(size(lapl)[2]) +SparseArrays.diag(lapl::CombinatorialLaplacian) = lapl.A.D +SparseArrays.diag(lapl::Laplacian) = ones(size(lapl)[2]) *(x::AbstractArray, ::Noop) = x *(::Noop, x) = x -*(adjmat::Adjacency{T}, x::AbstractVector{T}) where T<:Number = +*(adjmat::Adjacency{T}, x::AbstractVector{T}) where T <: Number = postscalefactor(adjmat) .* (adjmat.A * (prescalefactor(adjmat) .* x)) -*(adjmat::CombinatorialAdjacency{T}, x::AbstractVector{T}) where T<:Number = +*(adjmat::CombinatorialAdjacency{T}, x::AbstractVector{T}) where T <: Number = adjmat.A * x -*(lapl::Laplacian{T}, x::AbstractVector{T}) where T<:Number = - (diag(lapl) .* x) - (adjacency(lapl) * x) +*(lapl::Laplacian{T}, x::AbstractVector{T}) where T <: Number = + (SparseArrays.diag(lapl) .* x) - (adjacency(lapl) * x) -function *(adjmat::PunchedAdjacency{T}, x::AbstractVector{T}) where T<:Number +function *(adjmat::PunchedAdjacency{T}, x::AbstractVector{T}) where T <: Number y = adjmat.A * x return y - LinearAlgebra.dot(adjmat.perron, y) * adjmat.perron end -function mul!(Y, A::Adjacency, B) +function LinearAlgebra.mul!(Y, A::Adjacency, B) # we need to do 3 matrix products # Y and B can't overlap in any one call to mul! # The last call to mul! must be (Y, postscalefactor, tmp) @@ -247,32 +247,32 @@ function mul!(Y, A::Adjacency, B) # and the first step (Y, prescalefactor, B) tmp1 = LinearAlgebra.Diagonal(prescalefactor(A)) * B tmp = similar(Y) - mul!(tmp, A.A, tmp1) - return mul!(Y, LinearAlgebra.Diagonal(postscalefactor(A)), tmp) + LinearAlgebra.mul!(tmp, A.A, tmp1) + return LinearAlgebra.mul!(Y, LinearAlgebra.Diagonal(postscalefactor(A)), tmp) end -mul!(Y, A::CombinatorialAdjacency, B) = mul!(Y, A.A, B) +LinearAlgebra.mul!(Y, A::CombinatorialAdjacency, B) = LinearAlgebra.mul!(Y, A.A, B) # You can compute the StochasticAdjacency product without allocating a similar of Y. # This is true for all Adjacency where the postscalefactor is a Noop # at time of writing this is just StochasticAdjacency and CombinatorialAdjacency -function mul!(Y, A::StochasticAdjacency, B) +function LinearAlgebra.mul!(Y, A::StochasticAdjacency, B) tmp = LinearAlgebra.Diagonal(prescalefactor(A)) * B - mul!(Y, A.A, tmp) + LinearAlgebra.mul!(Y, A.A, tmp) return Y end -function mul!(Y, adjmat::PunchedAdjacency, x) +function LinearAlgebra.mul!(Y, adjmat::PunchedAdjacency, x) y = adjmat.A * x Y[:] = y - LinearAlgebra.dot(adjmat.perron, y) * adjmat.perron return Y end -function mul!(Y, lapl::Laplacian, B) - mul!(Y, lapl.A, B) - z = diag(lapl) .* B - Y[:] = z - Y[:] - return Y +function LinearAlgebra.mul!(Y, lapl::Laplacian, B) + LinearAlgebra.mul!(Y, lapl.A, B) + z = SparseArrays.diag(lapl) .* B + Y[:] = z - Y[:] + return Y end @@ -283,23 +283,23 @@ Return a symmetric version of graph (represented by sparse matrix `A`) as a spar `which` may be one of `:triu`, `:tril`, `:sum`, or `:or`. Use `:sum` for weighted graphs. """ function symmetrize(A::SparseMatrix, which=:or) - if which == :or - M = A + sparse(A') - M.nzval[M.nzval .== 2] .= 1 - return M + if which == :or + M = A + SparseArrays.sparse(A') + M.nzval[M.nzval .== 2] .= 1 + return M end - T = A - if which == :triu - T = LinearAlgebra.triu(A) - elseif which == :tril - T = LinearAlgebra.tril(A) + T = A + if which == :triu + T = LinearAlgebra.triu(A) + elseif which == :tril + T = LinearAlgebra.tril(A) elseif which == :sum - T = A + T = A else throw(ArgumentError("$which is not a supported method of symmetrizing a matrix")) end - M = T + sparse(T') - return M + M = T + SparseArrays.sparse(T') + return M end """ @@ -317,9 +317,9 @@ symmetrize(adjmat::CombinatorialAdjacency, which=:or) = # per #564 -@deprecate mul!(Y, A::Noop, B) None +# @deprecate LinearAlgebra.mul!(Y, A::Noop, B) None @deprecate convert(::Type{Adjacency}, lapl::Laplacian) None -@deprecate convert(::Type{SparseMatrix}, adjmat::GraphMatrix) sparse(adjmat) +@deprecate convert(::Type{SparseMatrix}, adjmat::GraphMatrix) SparseArrays.sparse(adjmat) diff --git a/src/linalg/nonbacktracking.jl b/src/linalg/nonbacktracking.jl index 943ca2733..6ce6ff0f9 100644 --- a/src/linalg/nonbacktracking.jl +++ b/src/linalg/nonbacktracking.jl @@ -61,7 +61,7 @@ Additionally the `contract!(vertexspace, nbt, edgespace)` method takes vectors represented in the domain of ``B`` and represents them in the domain of the adjacency matrix of `g`. """ -struct Nonbacktracking{G<:AbstractGraph} +struct Nonbacktracking{G <: AbstractGraph} g::G edgeidmap::Dict{Edge,Int} m::Int @@ -85,9 +85,9 @@ end size(nbt::Nonbacktracking) = (nbt.m, nbt.m) eltype(nbt::Nonbacktracking) = Float64 -issymmetric(nbt::Nonbacktracking) = false +LinearAlgebra.issymmetric(nbt::Nonbacktracking) = false -function *(nbt::Nonbacktracking, x::Vector{T}) where T<:Number +function *(nbt::Nonbacktracking, x::Vector{T}) where T <: Number length(x) == nbt.m || error("dimension mismatch") y = zeros(T, length(x)) for (e, u) in nbt.edgeidmap @@ -100,7 +100,7 @@ function *(nbt::Nonbacktracking, x::Vector{T}) where T<:Number end return y end -function mul!(C, nbt::Nonbacktracking, B) +function LinearAlgebra.mul!(C, nbt::Nonbacktracking, B) # computs C = A * B for i in 1:size(B, 2) C[:, i] = nbt * B[:, i] @@ -126,7 +126,7 @@ function coo_sparse(nbt::Nonbacktracking) return I, J, 1.0 end -sparse(nbt::Nonbacktracking) = sparse(coo_sparse(nbt)..., nbt.m, nbt.m) +SparseArrays.sparse(nbt::Nonbacktracking) = SparseArrays.sparse(coo_sparse(nbt)..., nbt.m, nbt.m) function *(nbt::Nonbacktracking, x::AbstractMatrix) y = zero(x) diff --git a/src/linalg/spectral.jl b/src/linalg/spectral.jl index fef0713ae..31c5504cb 100644 --- a/src/linalg/spectral.jl +++ b/src/linalg/spectral.jl @@ -80,7 +80,7 @@ function laplacian_matrix(g::AbstractGraph{U}, T::DataType=Int; dir::Symbol=:uns dir = is_directed(g) ? :both : :out end A = adjacency_matrix(g, T; dir=dir) - D = convert(SparseArrays.SparseMatrixCSC{T, U}, LinearAlgebra.Diagonal(sparse(sum(A, dims=2)[:]))) + D = convert(SparseArrays.SparseMatrixCSC{T,U}, LinearAlgebra.Diagonal(SparseArrays.sparse(sum(A, dims=2)[:]))) return D - A end @@ -179,7 +179,7 @@ If `k` is ommitted, uses full spectrum. function spectral_distance end # can't use Traitor syntax here (https://github.com/mauro3/SimpleTraits.jl/issues/36) -@traitfn function spectral_distance(G₁::G, G₂::G, k::Integer) where {G<:AbstractGraph; !IsDirected{G}} +@traitfn function spectral_distance(G₁::G, G₂::G, k::Integer) where {G <: AbstractGraph; !IsDirected{G}} A₁ = adjacency_matrix(G₁) A₂ = adjacency_matrix(G₂) @@ -190,7 +190,7 @@ function spectral_distance end end # can't use Traitor syntax here (https://github.com/mauro3/SimpleTraits.jl/issues/36) -@traitfn function spectral_distance(G₁::G, G₂::G) where {G<:AbstractGraph; !IsDirected{G}} +@traitfn function spectral_distance(G₁::G, G₂::G) where {G <: AbstractGraph; !IsDirected{G}} nv(G₁) == nv(G₂) || throw(ArgumentError("Spectral distance not defined for |G₁| != |G₂|")) return spectral_distance(G₁, G₂, nv(G₁)) end diff --git a/src/operators.jl b/src/operators.jl index 9802e86b8..8e2c07165 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -24,9 +24,9 @@ function complement(g::DiGraph) gnv = nv(g) h = SimpleDiGraph(gnv) for i in vertices(g), j in vertices(g) - if i != j && !has_edge(g, i, j) - add_edge!(h, i, j) - end + if i != j && !has_edge(g, i, j) + add_edge!(h, i, j) + end end return h end @@ -72,7 +72,7 @@ edges where the vertices an edges from graph `h` are appended to graph `g`. Preserves the eltype of the input graph. Will error if the number of vertices in the generated graph exceeds the eltype. """ -function blockdiag(g::T, h::T) where T<:AbstractGraph +function SparseArrays.blockdiag(g::T, h::T) where T <: AbstractGraph gnv = nv(g) r = T(gnv + nv(h)) for e in edges(g) @@ -93,7 +93,7 @@ Return a graph with edges that are only in both graph `g` and graph `h`. This function may produce a graph with 0-degree vertices. Preserves the eltype of the input graph. """ -function intersect(g::T, h::T) where T<:AbstractGraph +function intersect(g::T, h::T) where T <: AbstractGraph gnv = nv(g) hnv = nv(h) @@ -113,7 +113,7 @@ Return a graph with edges in graph `g` that are not in graph `h`. Note that this function may produce a graph with 0-degree vertices. Preserves the eltype of the input graph. """ -function difference(g::T, h::T) where T<:AbstractGraph +function difference(g::T, h::T) where T <: AbstractGraph gnv = nv(g) hnv = nv(h) @@ -135,7 +135,7 @@ Note that this function may produce a graph with 0-degree vertices. Preserves the eltype of the input graph. Will error if the number of vertices in the generated graph exceeds the eltype. """ -function symmetric_difference(g::T, h::T) where T<:AbstractGraph +function symmetric_difference(g::T, h::T) where T <: AbstractGraph gnv = nv(g) hnv = nv(h) @@ -159,7 +159,7 @@ of all vertices and edges. Preserves the eltype of the input graph. Will error if the number of vertices in the generated graph exceeds the eltype. """ -function union(g::T, h::T) where T<:AbstractGraph +function union(g::T, h::T) where T <: AbstractGraph gnv = nv(g) hnv = nv(h) @@ -188,8 +188,8 @@ adds all the edges between the vertices in `g` and those in `h`. Preserves the eltype of the input graph. Will error if the number of vertices in the generated graph exceeds the eltype. """ -function join(g::T, h::T) where T<:AbstractGraph - r = blockdiag(g, h) +function join(g::T, h::T) where T <: AbstractGraph + r = SparseArrays.blockdiag(g, h) for i in vertices(g) for j = (nv(g) + 1):(nv(g) + nv(h)) add_edge!(r, i, j) @@ -211,7 +211,7 @@ in the generated graph exceeds the eltype. """ function crosspath end # see https://github.com/mauro3/SimpleTraits.jl/issues/47#issuecomment-327880153 for syntax -@traitfn function crosspath(len::Integer, g::AG::(!IsDirected)) where {T, AG<:AbstractGraph{T}} +@traitfn function crosspath(len::Integer, g::AG::(!IsDirected)) where {T, AG <: AbstractGraph{T}} p = PathGraph(len) h = Graph{T}(p) return cartesian_product(h, g) @@ -221,7 +221,7 @@ end # """Provides multiplication of a graph `g` by a vector `v` such that spectral # graph functions in [GraphMatrices.jl](https://github.com/jpfairbanks/GraphMatrices.jl) can utilize LightGraphs natively. # """ -function *(g::Graph, v::Vector{T}) where T<:Real +function *(g::Graph, v::Vector{T}) where T <: Real length(v) == nv(g) || throw(ArgumentError("Vector size must equal number of vertices")) y = zeros(T, nv(g)) for e in edges(g) @@ -233,7 +233,7 @@ function *(g::Graph, v::Vector{T}) where T<:Real return y end -function *(g::DiGraph, v::Vector{T}) where T<:Real +function *(g::DiGraph, v::Vector{T}) where T <: Real length(v) == nv(g) || throw(ArgumentError("Vector size must equal number of vertices")) y = zeros(T, nv(g)) for e in edges(g) @@ -276,11 +276,11 @@ sum(g::AbstractGraph) = ne(g) Return the default adjacency matrix of `g`. """ -sparse(g::AbstractGraph) = adjacency_matrix(g) +SparseArrays.sparse(g::AbstractGraph) = adjacency_matrix(g) length(g::AbstractGraph) = nv(g) * nv(g) ndims(g::AbstractGraph) = 2 -issymmetric(g::AbstractGraph) = !is_directed(g) +LinearAlgebra.issymmetric(g::AbstractGraph) = !is_directed(g) """ cartesian_product(g, h) @@ -292,7 +292,7 @@ of `g` and `h`. Preserves the eltype of the input graph. Will error if the number of vertices in the generated graph exceeds the eltype. """ -function cartesian_product(g::G, h::G) where G<:AbstractGraph +function cartesian_product(g::G, h::G) where G <: AbstractGraph z = G(nv(g) * nv(h)) id(i, j) = (i - 1) * nv(h) + j for e in edges(g) @@ -321,7 +321,7 @@ of `g` and `h`. Preserves the eltype of the input graph. Will error if the number of vertices in the generated graph exceeds the eltype. """ -function tensor_product(g::G, h::G) where G<:AbstractGraph +function tensor_product(g::G, h::G) where G <: AbstractGraph z = G(nv(g) * nv(h)) id(i, j) = (i - 1) * nv(h) + j for e1 in edges(g) @@ -374,7 +374,7 @@ julia> sg, vmap = induced_subgraph(g, elist) julia> @assert sg == g[elist] ``` """ -function induced_subgraph(g::T, vlist::AbstractVector{U}) where T<:AbstractGraph where U<:Integer +function induced_subgraph(g::T, vlist::AbstractVector{U}) where T <: AbstractGraph where U <: Integer allunique(vlist) || throw(ArgumentError("Vertices in subgraph list must be unique")) h = T(length(vlist)) newvid = Dict{U,U}() @@ -398,7 +398,7 @@ function induced_subgraph(g::T, vlist::AbstractVector{U}) where T<:AbstractGraph end -function induced_subgraph(g::AG, elist::AbstractVector{U}) where AG<:AbstractGraph{T} where T where U<:AbstractEdge +function induced_subgraph(g::AG, elist::AbstractVector{U}) where AG <: AbstractGraph{T} where T where U <: AbstractEdge h = zero(g) newvid = Dict{T,T}() vmap = Vector{T}() @@ -463,7 +463,7 @@ function merge_vertices(g::AbstractGraph, vs) labels = collect(1:nv(g)) # Use lowest value as new vertex id. sort!(vs) - nvnew = nv(g) - length(unique(vs)) +1 + nvnew = nv(g) - length(unique(vs)) + 1 nvnew <= nv(g) || return g (v0, vm) = extrema(vs) v0 > 0 || throw(ArgumentError("invalid vertex ID: $v0 in list of vertices to be merged")) @@ -513,11 +513,11 @@ function merge_vertices!(g::Graph{T}, vs::Vector{U} where U <: Integer) where T if (i != merged_vertex) && !insorted(i, vs) nbrs_to_rewire = Set{T}() for j in outneighbors(g, i) - if insorted(j, vs) - push!(nbrs_to_rewire, merged_vertex) - else - push!(nbrs_to_rewire, new_vertex_ids[j]) - end + if insorted(j, vs) + push!(nbrs_to_rewire, merged_vertex) + else + push!(nbrs_to_rewire, new_vertex_ids[j]) + end end g.fadjlist[new_vertex_ids[i]] = sort(collect(nbrs_to_rewire)) diff --git a/src/shortestpaths/dijkstra.jl b/src/shortestpaths/dijkstra.jl index 5273f3224..4d2d612f7 100644 --- a/src/shortestpaths/dijkstra.jl +++ b/src/shortestpaths/dijkstra.jl @@ -3,7 +3,7 @@ An [`AbstractPathState`](@ref) designed for Dijkstra shortest-paths calculations. """ -struct DijkstraState{T<:Real,U<:Integer} <: AbstractPathState +struct DijkstraState{T <: Real,U <: Integer} <: AbstractPathState parents::Vector{U} dists::Vector{T} predecessors::Vector{Vector{U}} @@ -22,13 +22,12 @@ Return a [`LightGraphs.DijkstraState`](@ref) that contains various traversal inf - `allpaths=false`: If true, returns a [`LightGraphs.DijkstraState`](@ref) that keeps track of all predecessors of a given vertex. """ -function dijkstra_shortest_paths( - g::AbstractGraph, +function dijkstra_shortest_paths(g::AbstractGraph, srcs::Vector{U}, distmx::AbstractMatrix{T}=weights(g); allpaths=false, trackvertices=false - ) where T <: Real where U<:Integer + ) where T <: Real where U <: Integer nvg = nv(g) dists = fill(typemax(T), nvg) @@ -55,7 +54,7 @@ function dijkstra_shortest_paths( u = hentry[1] if trackvertices - push!(closest_vertices, u) + push!(closest_vertices, u) end for v in outneighbors(g, u) @@ -91,11 +90,11 @@ function dijkstra_shortest_paths( end if trackvertices - for s in vertices(g) - if !visited[s] - push!(closest_vertices, s) + for s in vertices(g) + if !visited[s] + push!(closest_vertices, s) + end end - end end pathcounts[srcs] .= 1 @@ -107,7 +106,7 @@ function dijkstra_shortest_paths( return DijkstraState{T,U}(parents, dists, preds, pathcounts, closest_vertices) end -dijkstra_shortest_paths(g::AbstractGraph, src::Integer, distmx::AbstractMatrix = weights(g); allpaths=false, trackvertices=false) = +dijkstra_shortest_paths(g::AbstractGraph, src::Integer, distmx::AbstractMatrix=weights(g); allpaths=false, trackvertices=false) = dijkstra_shortest_paths(g, [src;], distmx; allpaths=allpaths, trackvertices=trackvertices) """ @@ -115,7 +114,7 @@ dijkstra_shortest_paths(g, [src;], distmx; allpaths=allpaths, trackvertices=trac An [`AbstractPathState`](@ref) designed for multisource_dijkstra_shortest_paths calculation. """ -struct MultipleDijkstraState{T<:Real,U<:Integer} <: AbstractPathState +struct MultipleDijkstraState{T <: Real,U <: Integer} <: AbstractPathState dists::Matrix{T} parents::Matrix{U} end @@ -128,11 +127,9 @@ Compute the shortest paths between all pairs of vertices in graph `g` by running an optional distance matrix `distmx`. Return a [`MultipleDijkstraState`](@ref) with relevant traversal information. """ -function parallel_multisource_dijkstra_shortest_paths( - g::AbstractGraph{U}, - sources::AbstractVector = vertices(g), - distmx::AbstractMatrix{T} = weights(g) - ) where T <: Real where U +function parallel_multisource_dijkstra_shortest_paths(g::AbstractGraph{U}, + sources::AbstractVector=vertices(g), + distmx::AbstractMatrix{T}=weights(g)) where T <: Real where U n_v = nv(g) r_v = length(sources) @@ -141,10 +138,10 @@ function parallel_multisource_dijkstra_shortest_paths( dists = SharedArrays.SharedMatrix{T}(Int(r_v), Int(n_v)) parents = SharedArrays.SharedMatrix{U}(Int(r_v), Int(n_v)) - @sync @distributed for i in 1:r_v - state = dijkstra_shortest_paths(g, sources[i], distmx) - dists[i, :] = state.dists - parents[i, :] = state.parents + Distributed.@sync Distributed.@distributed for i in 1:r_v + state = dijkstra_shortest_paths(g, sources[i], distmx) + dists[i, :] = state.dists + parents[i, :] = state.parents end result = MultipleDijkstraState(SharedArrays.sdata(dists), SharedArrays.sdata(parents)) diff --git a/src/shortestpaths/johnson.jl b/src/shortestpaths/johnson.jl index b0ee043bc..9df66be0b 100644 --- a/src/shortestpaths/johnson.jl +++ b/src/shortestpaths/johnson.jl @@ -3,7 +3,7 @@ struct JohnsonState{T, U} An [`AbstractPathState`](@ref) designed for Johnson shortest-paths calculations. """ -struct JohnsonState{T<:Real, U<:Integer} <: AbstractPathState +struct JohnsonState{T <: Real,U <: Integer} <: AbstractPathState dists::Matrix{T} parents::Matrix{U} end @@ -31,11 +31,10 @@ bellman_ford_shortest_paths parallel_multisource_dijkstra_shortest_paths dijkstra_shortest_paths """ -function johnson_shortest_paths( - g::AbstractGraph{U}, - distmx::AbstractMatrix{T} = weights(g); - parallel::Bool = false -) where T<:Real where U<:Integer +function johnson_shortest_paths(g::AbstractGraph{U}, + distmx::AbstractMatrix{T}=weights(g); + parallel::Bool=false +) where T <: Real where U <: Integer nvg = nv(g) type_distmx = typeof(distmx) @@ -43,28 +42,28 @@ function johnson_shortest_paths( wt_transform = bellman_ford_shortest_paths(g, vertices(g), distmx).dists if !type_distmx.mutable && type_distmx != LightGraphs.DefaultDistance - distmx = sparse(distmx) #Change reference, not value + distmx = SparseArrays.sparse(distmx) #Change reference, not value end #Weight transform not needed if all weights are positive. if type_distmx != LightGraphs.DefaultDistance for e in edges(g) - distmx[src(e), dst(e)] += wt_transform[src(e)] - wt_transform[dst(e)] + distmx[src(e), dst(e)] += wt_transform[src(e)] - wt_transform[dst(e)] end end if !parallel dists = Matrix{T}(undef, nvg, nvg) parents = Matrix{U}(undef, nvg, nvg) - for v in vertices(g) - dijk_state = dijkstra_shortest_paths(g, v, distmx) - dists[v, :] = dijk_state.dists - parents[v, :] = dijk_state.parents - end + for v in vertices(g) + dijk_state = dijkstra_shortest_paths(g, v, distmx) + dists[v, :] = dijk_state.dists + parents[v, :] = dijk_state.parents + end else - dijk_state = parallel_multisource_dijkstra_shortest_paths(g, vertices(g), distmx) - dists = dijk_state.dists - parents = dijk_state.parents + dijk_state = parallel_multisource_dijkstra_shortest_paths(g, vertices(g), distmx) + dists = dijk_state.dists + parents = dijk_state.parents end broadcast!(-, dists, dists, wt_transform) @@ -81,7 +80,7 @@ function johnson_shortest_paths( return JohnsonState(dists, parents) end -function enumerate_paths(s::JohnsonState{T, U}, v::Integer) where T<:Real where U<:Integer +function enumerate_paths(s::JohnsonState{T,U}, v::Integer) where T <: Real where U <: Integer pathinfo = s.parents[v, :] paths = Vector{Vector{U}}() for i in 1:length(pathinfo) diff --git a/src/traversals/greedy_color.jl b/src/traversals/greedy_color.jl index 2bff13dc3..9935dd0ba 100644 --- a/src/traversals/greedy_color.jl +++ b/src/traversals/greedy_color.jl @@ -68,7 +68,7 @@ function parallel_random_greedy_color( reps::Integer ) where T<:Integer - best = @distributed (best_color) for i in 1:reps + best = Distributed.@distributed (best_color) for i in 1:reps seq = Random.shuffle(vertices(g)) perm_greedy_color(g, seq) end diff --git a/test/biconnectivity/articulation.jl b/test/biconnectivity/articulation.jl index 9100c2a41..3df06873b 100644 --- a/test/biconnectivity/articulation.jl +++ b/test/biconnectivity/articulation.jl @@ -19,21 +19,21 @@ add_edge!(gint, 7, 12) for g in testgraphs(gint) - art = @inferred(articulation(g)) - ans = [1, 7, 8, 12] - @test art == ans + art = @inferred(articulation(g)) + ans = [1, 7, 8, 12] + @test art == ans end for level in 1:6 btree = LightGraphs.BinaryTree(level) for tree in [btree, Graph{UInt8}(btree), Graph{Int16}(btree)] - artpts = @inferred(articulation(tree)) - @test artpts == collect(1:(2^(level - 1) - 1)) + artpts = @inferred(articulation(tree)) + @test artpts == collect(1:(2^(level - 1) - 1)) end end - hint = LightGraphs.blockdiag(WheelGraph(5), WheelGraph(5)) + hint = SparseArrays.blockdiag(WheelGraph(5), WheelGraph(5)) add_edge!(hint, 5, 6) for h in (hint, Graph{UInt8}(hint), Graph{Int16}(hint)) - @test @inferred(articulation(h)) == [5, 6] + @test @inferred(articulation(h)) == [5, 6] end end diff --git a/test/biconnectivity/biconnect.jl b/test/biconnectivity/biconnect.jl index e2c907afc..adda7bdc7 100644 --- a/test/biconnectivity/biconnect.jl +++ b/test/biconnectivity/biconnect.jl @@ -22,8 +22,8 @@ [Edge(11, 12)]] for g in testgraphs(gint) - bcc = @inferred(biconnected_components(g)) - @test bcc == a + bcc = @inferred(biconnected_components(g)) + @test bcc == a end g = SimpleGraph(4) @@ -38,13 +38,13 @@ add_edge!(h, 3, 4) add_edge!(h, 1, 4) - gint = blockdiag(g, h) + gint = SparseArrays.blockdiag(g, h) add_edge!(gint, 4, 5) a = [[Edge(5, 8), Edge(7, 8), Edge(6, 7), Edge(5, 6)], [Edge(4, 5)], [Edge(1, 4), Edge(3, 4), Edge(2, 3), Edge(1, 2)]] for g in testgraphs(gint) - bcc = @inferred(biconnected_components(g)) - @test bcc == a + bcc = @inferred(biconnected_components(g)) + @test bcc == a end end diff --git a/test/community/core-periphery.jl b/test/community/core-periphery.jl index a9f08d290..570fbe9d0 100644 --- a/test/community/core-periphery.jl +++ b/test/community/core-periphery.jl @@ -10,7 +10,7 @@ end g10 = StarGraph(10) - g10 = blockdiag(g10, g10) + g10 = SparseArrays.blockdiag(g10, g10) add_edge!(g10, 1, 11) for g in testgraphs(g10) c = @inferred(core_periphery_deg(g)) diff --git a/test/community/label_propagation.jl b/test/community/label_propagation.jl index 142fb5ccf..037c0f2cb 100644 --- a/test/community/label_propagation.jl +++ b/test/community/label_propagation.jl @@ -2,17 +2,17 @@ n = 10 g10 = CompleteGraph(n) for g in testgraphs(g10) - z = copy(g) - for k = 2:5 - z = blockdiag(z, g) - add_edge!(z, (k - 1) * n, k * n) - c, ch = @inferred(label_propagation(z)) - a = collect(n:n:(k * n)) - a = Int[div(i - 1, n) + 1 for i = 1:(k * n)] + z = copy(g) + for k = 2:5 + z = SparseArrays.blockdiag(z, g) + add_edge!(z, (k - 1) * n, k * n) + c, ch = @inferred(label_propagation(z)) + a = collect(n:n:(k * n)) + a = Int[div(i - 1, n) + 1 for i = 1:(k * n)] # check the number of communities - @test length(unique(a)) == length(unique(c)) + @test length(unique(a)) == length(unique(c)) # check the partition - @test a == c - end + @test a == c + end end end diff --git a/test/generators/randgraphs.jl b/test/generators/randgraphs.jl index db313dd5d..e3b7d1497 100644 --- a/test/generators/randgraphs.jl +++ b/test/generators/randgraphs.jl @@ -1,6 +1,6 @@ @testset "Randgraphs" begin - r1 = SimpleGraph(10,20) - r2 = SimpleDiGraph(5,10) + r1 = SimpleGraph(10, 20) + r2 = SimpleDiGraph(5, 10) @test nv(r1) == 10 @test ne(r1) == 20 @@ -11,15 +11,15 @@ @test eltype(Graph(0x5, 0x2)) == eltype(Graph(0x5, 2)) == UInt8 for T in [UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt, Int] - @test eltype(Graph{T}(5,2)) == T - @test eltype(DiGraph{T}(5,2)) == T + @test eltype(Graph{T}(5, 2)) == T + @test eltype(DiGraph{T}(5, 2)) == T end - @test SimpleGraph(10,20,seed=3) == SimpleGraph(10,20,seed=3) - @test SimpleDiGraph(10,20,seed=3) == SimpleDiGraph(10,20,seed=3) - @test SimpleGraph(10,20,seed=3) == erdos_renyi(10,20,seed=3) - @test ne(Graph(10,40,seed=3)) == 40 - @test ne(DiGraph(10,80,seed=3)) == 80 + @test SimpleGraph(10, 20, seed=3) == SimpleGraph(10, 20, seed=3) + @test SimpleDiGraph(10, 20, seed=3) == SimpleDiGraph(10, 20, seed=3) + @test SimpleGraph(10, 20, seed=3) == erdos_renyi(10, 20, seed=3) + @test ne(Graph(10, 40, seed=3)) == 40 + @test ne(DiGraph(10, 80, seed=3)) == 80 er = erdos_renyi(10, 0.5) @test nv(er) == 10 @@ -32,20 +32,20 @@ @test nv(er) == 10 @test is_directed(er) == false - cl = expected_degree_graph(zeros(10), seed = 17) + cl = expected_degree_graph(zeros(10), seed=17) @test nv(cl) == 10 @test ne(cl) == 0 @test is_directed(cl) == false - cl = expected_degree_graph([3, 2, 1, 2], seed = 17) + cl = expected_degree_graph([3, 2, 1, 2], seed=17) @test nv(cl) == 4 @test is_directed(cl) == false - cl = expected_degree_graph(fill(99, 100), seed = 17) + cl = expected_degree_graph(fill(99, 100), seed=17) @test nv(cl) == 100 @test all(degree(cl) .> 90) - ws = watts_strogatz(10,4,0.2) + ws = watts_strogatz(10, 4, 0.2) @test nv(ws) == 10 @test ne(ws) == 20 @test is_directed(ws) == false @@ -140,7 +140,7 @@ @test ne(rr) == 0 @test is_directed(rr) == false - rd = random_regular_digraph(10,0) + rd = random_regular_digraph(10, 0) @test nv(rd) == 10 @test ne(rd) == 0 @test is_directed(rd) @@ -158,7 +158,7 @@ @test degree(rr, v) == 50 end - rr = random_configuration_model(10, repeat([2,4] ,5), seed=3) + rr = random_configuration_model(10, repeat([2,4], 5), seed=3) @test nv(rr) == 10 @test ne(rr) == 15 @test is_directed(rr) == false @@ -171,7 +171,7 @@ @test num4 == 5 @test num2 == 5 - rr = random_configuration_model(1000, zeros(Int,1000)) + rr = random_configuration_model(1000, zeros(Int, 1000)) @test nv(rr) == 1000 @test ne(rr) == 0 @test is_directed(rr) == false @@ -261,8 +261,8 @@ sizes = [200, 200, 100] internaldeg = 15 externaldeg = 6 - internalp = Float64[internaldeg/i for i in sizes] - externalp = externaldeg/sum(sizes) + internalp = Float64[internaldeg / i for i in sizes] + externalp = externaldeg / sum(sizes) numedges = internaldeg + externaldeg #+ sum(externaldeg.*sizes[2:end]) numedges *= div(sum(sizes), 2) sbm = StochasticBlockModel(internalp, externalp, sizes) @@ -278,19 +278,19 @@ # check that average degree is not too high # factor of two is cushion for random process - @test mean(degree(g)) <= 4//2*numedges/sum(sizes) + @test mean(degree(g)) <= 4 // 2 * numedges / sum(sizes) # check that the internal degrees are higher than the external degrees # 5//4 is cushion for random process. - @test all(sum(bc-LinearAlgebra.diagm(0=>diag(bc)), dims=1) .<= 5//4 .* diag(bc)) + @test all(sum(bc - LinearAlgebra.diagm(0 => SparseArrays.diag(bc)), dims=1) .<= 5 // 4 .* SparseArrays.diag(bc)) - sbm2 = StochasticBlockModel(0.5*ones(4), 0.3, 10*ones(Int,4)) + sbm2 = StochasticBlockModel(0.5 * ones(4), 0.3, 10 * ones(Int, 4)) sbm = StochasticBlockModel(0.5, 0.3, 10, 4) @test sbm == sbm2 sbm.affinities[1,1] = 0 @test sbm != sbm2 - kg = @inferred kronecker(5,5) + kg = @inferred kronecker(5, 5) @test nv(kg) == 32 @test is_directed(kg) end diff --git a/test/generators/staticgraphs.jl b/test/generators/staticgraphs.jl index d8803d81e..79273bd36 100644 --- a/test/generators/staticgraphs.jl +++ b/test/generators/staticgraphs.jl @@ -60,8 +60,8 @@ I = [1, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9] J = [2, 3, 4, 1, 5, 1, 6, 1, 5, 6, 7, 2, 4, 8, 3, 4, 9, 4, 8, 9, 5, 7, 6, 7] V = ones(Int, length(I)) - Adj = sparse(I, J, V) - @test Adj == sparse(g) + Adj = SparseArrays.sparse(I, J, V) + @test Adj == SparseArrays.sparse(g) g = @inferred(DoubleBinaryTree(3)) # [[3, 2, 8] @@ -81,8 +81,8 @@ I = [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 5, 6, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 12, 13, 14] J = [3, 2, 8, 4, 1, 5, 1, 6, 7, 2, 2, 3, 3, 10, 9, 1, 11, 8, 12, 8, 13, 14, 9, 9, 10, 10] V = ones(Int, length(I)) - Adj = sparse(I, J, V) - @test Adj == sparse(g) + Adj = SparseArrays.sparse(I, J, V) + @test Adj == SparseArrays.sparse(g) rg3 = @inferred(RoachGraph(3)) # [3] @@ -100,6 +100,6 @@ I = [1, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 12, 12] J = [3, 4, 1, 5, 2, 6, 3, 7, 4, 8, 9, 8, 5, 10, 7, 6, 11, 10, 7, 8, 9, 12, 9, 12, 10, 11] V = ones(Int, length(I)) - Adj = sparse(I, J, V) - @test Adj == sparse(rg3) + Adj = SparseArrays.sparse(I, J, V) + @test Adj == SparseArrays.sparse(rg3) end diff --git a/test/linalg/graphmatrices.jl b/test/linalg/graphmatrices.jl index 47ca0cbca..8da8a955b 100644 --- a/test/linalg/graphmatrices.jl +++ b/test/linalg/graphmatrices.jl @@ -17,10 +17,10 @@ adjmat, stochmat, adjhat, avgmat = constructors(mat) @test adjmat.D == vec(sum(mat, dims=1)) @test adjmat.A == mat - @test isa(sparse(mat), SparseArrays.SparseMatrixCSC) - @test isa(sparse(stochmat), SparseArrays.SparseMatrixCSC) - @test isa(sparse(adjhat), SparseArrays.SparseMatrixCSC) - @test isa(sparse(avgmat), SparseArrays.SparseMatrixCSC) + @test isa(SparseArrays.sparse(mat), SparseArrays.SparseMatrixCSC) + @test isa(SparseArrays.sparse(stochmat), SparseArrays.SparseMatrixCSC) + @test isa(SparseArrays.sparse(adjhat), SparseArrays.SparseMatrixCSC) + @test isa(SparseArrays.sparse(avgmat), SparseArrays.SparseMatrixCSC) @test isa(convert(CombinatorialAdjacency, adjmat), CombinatorialAdjacency) @test isa(convert(CombinatorialAdjacency, avgmat), CombinatorialAdjacency) @test prescalefactor(adjhat) == postscalefactor(adjhat) @@ -60,7 +60,7 @@ @test_throws MethodError AveragingLaplacian(lapl) @test_throws MethodError convert(CombinatorialAdjacency, lapl) - L = sparse(lapl) + L = SparseArrays.sparse(lapl) @test sum(abs, (sum(L, dims=1))) == 0 end @@ -88,7 +88,7 @@ @test sum(abs, (adjmat * onevec)) > 0.0 @test sum(abs, ((stochmat * onevec) / sum(onevec))) ≈ 1.0 @test sum(abs, (lapl * onevec)) == 0 - g(a) = sum(abs, (sum(sparse(a), dims=1))) + g(a) = sum(abs, (sum(SparseArrays.sparse(a), dims=1))) @test g(lapl) == 0 @test g(NormalizedLaplacian(adjhat)) > 1e-13 @@ -112,20 +112,20 @@ @test_throws MethodError symmetrize(StochasticAdjacency(adjmat)) @test_throws MethodError symmetrize(AveragingAdjacency(adjmat)) - @test !issymmetric(AveragingAdjacency(adjmat)) - @test !issymmetric(StochasticAdjacency(adjmat)) + @test !LinearAlgebra.issymmetric(AveragingAdjacency(adjmat)) + @test !LinearAlgebra.issymmetric(StochasticAdjacency(adjmat)) @test_throws MethodError symmetrize(NormalizedAdjacency(adjmat)).A # --> adjmat.A begin @test CombinatorialAdjacency(mat) == CombinatorialAdjacency(mat) S = StochasticAdjacency(CombinatorialAdjacency(mat)) @test S.A == S.A - @test sparse(S) != S.A + @test SparseArrays.sparse(S) != S.A @test adjacency(S) == S.A @test NormalizedAdjacency(adjmat) != adjmat @test StochasticLaplacian(S) != adjmat @test_throws MethodError StochasticLaplacian(adjmat) # --> not(adjmat) - @test !issymmetric(S) + @test !LinearAlgebra.issymmetric(S) end end diff --git a/test/linalg/spectral.jl b/test/linalg/spectral.jl index ae33b258a..eaabb809a 100644 --- a/test/linalg/spectral.jl +++ b/test/linalg/spectral.jl @@ -1,7 +1,7 @@ import Base: Matrix # just so that we can assert equality of matrices -Matrix(nbt::Nonbacktracking) = Matrix(sparse(nbt)) +Matrix(nbt::Nonbacktracking) = Matrix(SparseArrays.sparse(nbt)) @testset "Spectral" begin @@ -45,17 +45,17 @@ Matrix(nbt::Nonbacktracking) = Matrix(sparse(nbt)) @test length(em) == 2 * ne(g) @test size(B) == (2 * ne(g), 2 * ne(g)) for i = 1:10 - @test sum(B[:, i]) == 8 - @test sum(B[i, :]) == 8 + @test sum(B[:, i]) == 8 + @test sum(B[i, :]) == 8 end - @test !issymmetric(B) + @test !LinearAlgebra.issymmetric(B) v = ones(Float64, ne(g)) z = zeros(Float64, nv(g)) n10 = Nonbacktracking(g) @test size(n10) == (2 * ne(g), 2 * ne(g)) @test eltype(n10) == Float64 - @test !issymmetric(n10) + @test !LinearAlgebra.issymmetric(n10) contract!(z, n10, v) @@ -131,8 +131,8 @@ Matrix(nbt::Nonbacktracking) = Matrix(sparse(nbt)) for g in testgraphs(pg) nbt = Nonbacktracking(g) B, emap = non_backtracking_matrix(g) - Bs = sparse(nbt) - @test sparse(B) == Bs + Bs = SparseArrays.sparse(nbt) + @test SparseArrays.sparse(B) == Bs @test IterativeEigensolvers.eigs(nbt, nev=1)[1] ≈ IterativeEigensolvers.eigs(B, nev=1)[1] atol = 1e-5 # check that matvec works @@ -156,7 +156,7 @@ Matrix(nbt::Nonbacktracking) = Matrix(sparse(nbt)) @test Matrix(B₁) == Matrix(B) @test B₁ * ones(size(B₁)[2]) == B * ones(size(B)[2]) @test size(B₁) == size(B) - @test !issymmetric(B₁) + @test !LinearAlgebra.issymmetric(B₁) @test eltype(B₁) == Float64 end # END tests for Nonbacktracking @@ -165,8 +165,8 @@ Matrix(nbt::Nonbacktracking) = Matrix(sparse(nbt)) for n = 3:10 polygon = random_regular_graph(n, 2) for g in testgraphs(polygon) - @test spectral_distance(g, g) ≈ 0 atol=1e-8 - @test spectral_distance(g, g, 1) ≈ 0 atol=1e-8 + @test spectral_distance(g, g) ≈ 0 atol = 1e-8 + @test spectral_distance(g, g, 1) ≈ 0 atol = 1e-8 end end end diff --git a/test/operators.jl b/test/operators.jl index 86735788f..168545520 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -8,7 +8,7 @@ @test nv(c) == 5 @test ne(c) == 6 - gb = @inferred(blockdiag(g, g)) + gb = @inferred(SparseArrays.blockdiag(g, g)) @test nv(gb) == 10 @test ne(gb) == 8 @@ -138,7 +138,7 @@ T = eltype(g) hc = CompleteGraph(2) h = Graph{T}(hc) - z = @inferred(blockdiag(g, h)) + z = @inferred(SparseArrays.blockdiag(g, h)) @test nv(z) == nv(g) + nv(h) @test ne(z) == ne(g) + ne(h) @test has_edge(z, 1, 2) @@ -173,10 +173,10 @@ @test size(p, 3) == 1 @test sum(p, 1) == sum(p, 2) @test_throws ArgumentError sum(p, 3) - @test sparse(p) == adjacency_matrix(p) + @test SparseArrays.sparse(p) == adjacency_matrix(p) @test length(p) == 100 @test ndims(p) == 2 - @test issymmetric(p) + @test LinearAlgebra.issymmetric(p) end gx = SimpleDiGraph(4) @@ -186,7 +186,7 @@ @test sum(g, 1) == [0, 1, 2, 1] @test sum(g, 2) == [2, 1, 1, 0] @test sum(g) == 4 - @test @inferred(!issymmetric(g)) + @test @inferred(!LinearAlgebra.issymmetric(g)) end nx = 20; ny = 21 @@ -204,7 +204,7 @@ m = nv(h) for i in 1:(len - 1) k = nv(g) - g = blockdiag(g, h) + g = SparseArrays.blockdiag(g, h) for v in 1:m add_edge!(g, v + (k - m), v + k) end diff --git a/test/shortestpaths/astar.jl b/test/shortestpaths/astar.jl index 88fb9550b..bc0c4ec08 100644 --- a/test/shortestpaths/astar.jl +++ b/test/shortestpaths/astar.jl @@ -3,7 +3,7 @@ g4 = PathDiGraph(5) d1 = float([0 1 2 3 4; 5 0 6 7 8; 9 10 0 11 12; 13 14 15 0 16; 17 18 19 20 0]) - d2 = sparse(float([0 1 2 3 4; 5 0 6 7 8; 9 10 0 11 12; 13 14 15 0 16; 17 18 19 20 0])) + d2 = SparseArrays.sparse(float([0 1 2 3 4; 5 0 6 7 8; 9 10 0 11 12; 13 14 15 0 16; 17 18 19 20 0])) for g in testgraphs(g3), dg in testdigraphs(g4) @test @inferred(a_star(g, 1, 4, d1)) == @inferred(a_star(dg, 1, 4, d1)) == diff --git a/test/shortestpaths/bellman-ford.jl b/test/shortestpaths/bellman-ford.jl index cfd977156..1ab42dce2 100644 --- a/test/shortestpaths/bellman-ford.jl +++ b/test/shortestpaths/bellman-ford.jl @@ -2,7 +2,7 @@ g4 = PathDiGraph(5) d1 = float([0 1 2 3 4; 5 0 6 7 8; 9 10 0 11 12; 13 14 15 0 16; 17 18 19 20 0]) - d2 = sparse(float([0 1 2 3 4; 5 0 6 7 8; 9 10 0 11 12; 13 14 15 0 16; 17 18 19 20 0])) + d2 = SparseArrays.sparse(float([0 1 2 3 4; 5 0 6 7 8; 9 10 0 11 12; 13 14 15 0 16; 17 18 19 20 0])) for g in testdigraphs(g4) y = @inferred(bellman_ford_shortest_paths(g, 2, d1)) z = @inferred(bellman_ford_shortest_paths(g, 2, d2)) diff --git a/test/shortestpaths/dijkstra.jl b/test/shortestpaths/dijkstra.jl index 5911c01c0..754c02e41 100644 --- a/test/shortestpaths/dijkstra.jl +++ b/test/shortestpaths/dijkstra.jl @@ -1,21 +1,21 @@ @testset "Dijkstra" begin g4 = PathDiGraph(5) d1 = float([0 1 2 3 4; 5 0 6 7 8; 9 10 0 11 12; 13 14 15 0 16; 17 18 19 20 0]) - d2 = sparse(float([0 1 2 3 4; 5 0 6 7 8; 9 10 0 11 12; 13 14 15 0 16; 17 18 19 20 0])) + d2 = SparseArrays.sparse(float([0 1 2 3 4; 5 0 6 7 8; 9 10 0 11 12; 13 14 15 0 16; 17 18 19 20 0])) for g in testdigraphs(g4) - y = @inferred(dijkstra_shortest_paths(g, 2, d1)) - z = @inferred(dijkstra_shortest_paths(g, 2, d2)) + y = @inferred(dijkstra_shortest_paths(g, 2, d1)) + z = @inferred(dijkstra_shortest_paths(g, 2, d2)) - @test y.parents == z.parents == [0, 0, 2, 3, 4] - @test y.dists == z.dists == [Inf, 0, 6, 17, 33] + @test y.parents == z.parents == [0, 0, 2, 3, 4] + @test y.dists == z.dists == [Inf, 0, 6, 17, 33] - y = @inferred(dijkstra_shortest_paths(g, 2, d1; allpaths=true)) - z = @inferred(dijkstra_shortest_paths(g, 2, d2; allpaths=true)) - @test z.predecessors[3] == y.predecessors[3] == [2] + y = @inferred(dijkstra_shortest_paths(g, 2, d1; allpaths=true)) + z = @inferred(dijkstra_shortest_paths(g, 2, d2; allpaths=true)) + @test z.predecessors[3] == y.predecessors[3] == [2] - @test @inferred(enumerate_paths(z)) == enumerate_paths(y) - @test @inferred(enumerate_paths(z))[4] == + @test @inferred(enumerate_paths(z)) == enumerate_paths(y) + @test @inferred(enumerate_paths(z))[4] == enumerate_paths(z, 4) == enumerate_paths(y, 4) == [2, 3, 4] end @@ -25,9 +25,9 @@ d = ones(Int, 5, 5) d[2, 3] = 100 for g in testgraphs(gx) - z = @inferred(dijkstra_shortest_paths(g, 1, d)) - @test z.dists == [0, 1, 3, 2, 3] - @test z.parents == [0, 1, 4, 2, 4] + z = @inferred(dijkstra_shortest_paths(g, 1, d)) + @test z.dists == [0, 1, 3, 2, 3] + @test z.parents == [0, 1, 4, 2, 4] end # small function to reconstruct the shortest path; I copied it from somewhere, can't find the original source to give the credits @@ -48,18 +48,18 @@ 1. 0. 3. 0.] for g in testgraphs(G) - ds = @inferred(dijkstra_shortest_paths(g, 2, w)) + ds = @inferred(dijkstra_shortest_paths(g, 2, w)) # this loop reconstructs the shortest path for vertices 1, 3 and 4 - @test spaths(ds, [1, 3, 4], 2) == Array[[2 1], + @test spaths(ds, [1, 3, 4], 2) == Array[[2 1], [2 3], [2 1 4]] # here a selflink at source is introduced; it should not change the shortest paths - w[2, 2] = 10.0 - ds = @inferred(dijkstra_shortest_paths(g, 2, w)) - shortest_paths = [] + w[2, 2] = 10.0 + ds = @inferred(dijkstra_shortest_paths(g, 2, w)) + shortest_paths = [] # this loop reconstructs the shortest path for vertices 1, 3 and 4 - @test spaths(ds, [1, 3, 4], 2) == Array[[2 1], + @test spaths(ds, [1, 3, 4], 2) == Array[[2 1], [2 3], [2 1 4]] end @@ -74,15 +74,15 @@ add_edge!(G, 3, 4) add_edge!(G, 4, 5) for g in testgraphs(G) - ds = @inferred(dijkstra_shortest_paths(g, 1, m; allpaths=true)) - @test ds.pathcounts == [1, 1, 1, 1, 2] - @test ds.predecessors == [[], [1], [1], [3], [3, 4]] - @test ds.predecessors == [[], [1], [1], [3], [3, 4]] - - dm = @inferred(dijkstra_shortest_paths(g, 1; allpaths=true, trackvertices=true)) - @test dm.pathcounts == [1, 1, 1, 1, 2] - @test dm.predecessors == [[], [1], [1], [3], [2, 3]] - @test dm.closest_vertices == [1, 2, 3, 5, 4] + ds = @inferred(dijkstra_shortest_paths(g, 1, m; allpaths=true)) + @test ds.pathcounts == [1, 1, 1, 1, 2] + @test ds.predecessors == [[], [1], [1], [3], [3, 4]] + @test ds.predecessors == [[], [1], [1], [3], [3, 4]] + + dm = @inferred(dijkstra_shortest_paths(g, 1; allpaths=true, trackvertices=true)) + @test dm.pathcounts == [1, 1, 1, 1, 2] + @test dm.predecessors == [[], [1], [1], [3], [2, 3]] + @test dm.closest_vertices == [1, 2, 3, 5, 4] end G = SimpleGraph(5) @@ -90,8 +90,8 @@ add_edge!(G, 1, 3) add_edge!(G, 4, 5) for g in testgraphs(G) - dm = @inferred(dijkstra_shortest_paths(g, 1; allpaths=true, trackvertices=true)) - @test dm.closest_vertices == [1, 2, 3, 4, 5] + dm = @inferred(dijkstra_shortest_paths(g, 1; allpaths=true, trackvertices=true)) + @test dm.closest_vertices == [1, 2, 3, 4, 5] end #Testing multisource On undirected Graph @@ -99,51 +99,51 @@ d = [0 1 2 3 4; 1 0 1 0 1; 2 1 0 11 12; 3 0 11 0 5; 4 1 19 5 0] for g in testgraphs(g3) - z = @inferred(floyd_warshall_shortest_paths(g, d)) - zp = @inferred(parallel_multisource_dijkstra_shortest_paths(g, collect(1:5), d)) - @test all(isapprox(z.dists, zp.dists)) - - for i in 1:5 - state = dijkstra_shortest_paths(g, i; allpaths=true); - for j in 1:5 - if z.parents[i, j] != 0 - @test z.parents[i, j] in state.predecessors[j] - else - @test length(state.predecessors[j]) == 0 - @test state.parents[j] == 0 - end + z = @inferred(floyd_warshall_shortest_paths(g, d)) + zp = @inferred(parallel_multisource_dijkstra_shortest_paths(g, collect(1:5), d)) + @test all(isapprox(z.dists, zp.dists)) + + for i in 1:5 + state = dijkstra_shortest_paths(g, i; allpaths=true); + for j in 1:5 + if z.parents[i, j] != 0 + @test z.parents[i, j] in state.predecessors[j] + else + @test length(state.predecessors[j]) == 0 + @test state.parents[j] == 0 + end + end end - end - - z = @inferred(floyd_warshall_shortest_paths(g)) - zp = @inferred(parallel_multisource_dijkstra_shortest_paths(g)) - @test all(isapprox(z.dists, zp.dists)) - - for i in 1:5 - state = dijkstra_shortest_paths(g, i; allpaths=true); - for j in 1:5 - if z.parents[i, j] != 0 - @test z.parents[i, j] in state.predecessors[j] - else - @test length(state.predecessors[j]) == 0 - end + + z = @inferred(floyd_warshall_shortest_paths(g)) + zp = @inferred(parallel_multisource_dijkstra_shortest_paths(g)) + @test all(isapprox(z.dists, zp.dists)) + + for i in 1:5 + state = dijkstra_shortest_paths(g, i; allpaths=true); + for j in 1:5 + if z.parents[i, j] != 0 + @test z.parents[i, j] in state.predecessors[j] + else + @test length(state.predecessors[j]) == 0 + end + end end - end - - z = @inferred(floyd_warshall_shortest_paths(g)) - zp = @inferred(parallel_multisource_dijkstra_shortest_paths(g, [1, 2])) - @test all(isapprox(z.dists[1:2, :], zp.dists)) - - for i in 1:2 - state = dijkstra_shortest_paths(g, i;allpaths=true); - for j in 1:5 - if z.parents[i, j] != 0 - @test z.parents[i, j] in state.predecessors[j] - else - @test length(state.predecessors[j]) == 0 - end + + z = @inferred(floyd_warshall_shortest_paths(g)) + zp = @inferred(parallel_multisource_dijkstra_shortest_paths(g, [1, 2])) + @test all(isapprox(z.dists[1:2, :], zp.dists)) + + for i in 1:2 + state = dijkstra_shortest_paths(g, i;allpaths=true); + for j in 1:5 + if z.parents[i, j] != 0 + @test z.parents[i, j] in state.predecessors[j] + else + @test length(state.predecessors[j]) == 0 + end + end end - end end @@ -152,49 +152,49 @@ d = float([0 1 2 3 4; 5 0 6 7 8; 9 10 0 11 12; 13 14 15 0 16; 17 18 19 20 0]) for g in testdigraphs(g3) - z = @inferred(floyd_warshall_shortest_paths(g, d)) - zp = @inferred(parallel_multisource_dijkstra_shortest_paths(g, collect(1:5), d)) - @test all(isapprox(z.dists, zp.dists)) - - for i in 1:5 - state = dijkstra_shortest_paths(g, i; allpaths=true); - for j in 1:5 - if z.parents[i, j] != 0 - @test z.parents[i, j] in state.predecessors[j] - else - @test length(state.predecessors[j]) == 0 - end + z = @inferred(floyd_warshall_shortest_paths(g, d)) + zp = @inferred(parallel_multisource_dijkstra_shortest_paths(g, collect(1:5), d)) + @test all(isapprox(z.dists, zp.dists)) + + for i in 1:5 + state = dijkstra_shortest_paths(g, i; allpaths=true); + for j in 1:5 + if z.parents[i, j] != 0 + @test z.parents[i, j] in state.predecessors[j] + else + @test length(state.predecessors[j]) == 0 + end + end end - end - - z = @inferred(floyd_warshall_shortest_paths(g)) - zp = @inferred(parallel_multisource_dijkstra_shortest_paths(g)) - @test all(isapprox(z.dists, zp.dists)) - - for i in 1:5 - state = dijkstra_shortest_paths(g, i; allpaths=true); - for j in 1:5 - if z.parents[i, j] != 0 - @test z.parents[i, j] in state.predecessors[j] - else - @test length(state.predecessors[j]) == 0 - end + + z = @inferred(floyd_warshall_shortest_paths(g)) + zp = @inferred(parallel_multisource_dijkstra_shortest_paths(g)) + @test all(isapprox(z.dists, zp.dists)) + + for i in 1:5 + state = dijkstra_shortest_paths(g, i; allpaths=true); + for j in 1:5 + if z.parents[i, j] != 0 + @test z.parents[i, j] in state.predecessors[j] + else + @test length(state.predecessors[j]) == 0 + end + end end - end - - z = @inferred(floyd_warshall_shortest_paths(g)) - zp = @inferred(parallel_multisource_dijkstra_shortest_paths(g, [1, 2])) - @test all(isapprox(z.dists[1:2, :], zp.dists)) - - for i in 1:2 - state = dijkstra_shortest_paths(g, i;allpaths=true); - for j in 1:5 - if z.parents[i, j] != 0 - @test z.parents[i, j] in state.predecessors[j] - else - @test length(state.predecessors[j]) == 0 - end + + z = @inferred(floyd_warshall_shortest_paths(g)) + zp = @inferred(parallel_multisource_dijkstra_shortest_paths(g, [1, 2])) + @test all(isapprox(z.dists[1:2, :], zp.dists)) + + for i in 1:2 + state = dijkstra_shortest_paths(g, i;allpaths=true); + for j in 1:5 + if z.parents[i, j] != 0 + @test z.parents[i, j] in state.predecessors[j] + else + @test length(state.predecessors[j]) == 0 + end + end end - end end end diff --git a/test/shortestpaths/yen.jl b/test/shortestpaths/yen.jl index ebec4b95f..5baef9331 100644 --- a/test/shortestpaths/yen.jl +++ b/test/shortestpaths/yen.jl @@ -1,7 +1,7 @@ @testset "Yen" begin g4 = PathDiGraph(5) d1 = float([0 1 2 3 4; 5 0 6 7 8; 9 10 0 11 12; 13 14 15 0 16; 17 18 19 20 0]) - d2 = sparse(float([0 1 2 3 4; 5 0 6 7 8; 9 10 0 11 12; 13 14 15 0 16; 17 18 19 20 0])) + d2 = SparseArrays.sparse(float([0 1 2 3 4; 5 0 6 7 8; 9 10 0 11 12; 13 14 15 0 16; 17 18 19 20 0])) for g in testdigraphs(g4) x = @inferred(yen_k_shortest_paths(g, 5, 5)) diff --git a/test/traversals/bipartition.jl b/test/traversals/bipartition.jl index eb2de13be..0efb06b70 100644 --- a/test/traversals/bipartition.jl +++ b/test/traversals/bipartition.jl @@ -23,7 +23,7 @@ T = eltype(g) @test @inferred(bipartite_map(g)) == Vector{T}([ones(T, 10); 2 * ones(T, 10)]) - h = blockdiag(g, g) + h = SparseArrays.blockdiag(g, g) @test @inferred(bipartite_map(h)) == Vector{T}([ones(T, 10); 2 * ones(T, 10); ones(T, 10); 2 * ones(T, 10)]) end From 164e654e2a539291a8505e06b0c6896c0b2d93ff Mon Sep 17 00:00:00 2001 From: Seth Bromberger Date: Wed, 30 May 2018 14:24:15 -0700 Subject: [PATCH 3/4] remove tabs --- CONTRIBUTING.md | 10 +-- docs/src/distance.md | 4 +- src/linalg/graphmatrices.jl | 134 +++++++++++++++++------------------ src/shortestpaths/johnson.jl | 18 ++--- 4 files changed, 83 insertions(+), 83 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ed5be4fd0..a4a811aba 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -60,17 +60,17 @@ Locate the section for your github remote in the `.git/config` file. It looks li ``` [remote "origin"] - fetch = +refs/heads/*:refs/remotes/origin/* - url = git@github.com:JuliaGraphs/LightGraphs.jl.git + fetch = +refs/heads/*:refs/remotes/origin/* + url = git@github.com:JuliaGraphs/LightGraphs.jl.git ``` Now add the line `fetch = +refs/pull/*/head:refs/remotes/origin/pr/*` to this section. Obviously, change the github url to match your project's URL. It ends up looking like this: ``` [remote "origin"] - fetch = +refs/heads/*:refs/remotes/origin/* - url = git@github.com:JuliaGraphs/LightGraphs.jl.git - fetch = +refs/pull/*/head:refs/remotes/origin/pr/* + fetch = +refs/heads/*:refs/remotes/origin/* + url = git@github.com:JuliaGraphs/LightGraphs.jl.git + fetch = +refs/pull/*/head:refs/remotes/origin/pr/* ``` Now fetch all the pull requests: diff --git a/docs/src/distance.md b/docs/src/distance.md index 1706835d1..38e963323 100644 --- a/docs/src/distance.md +++ b/docs/src/distance.md @@ -11,8 +11,8 @@ Pages = ["distance.md"] ```@autodocs Modules = [LightGraphs] Pages = [ - "distance.jl", - "transitivity.jl" + "distance.jl", + "transitivity.jl" ] Private = false ``` diff --git a/src/linalg/graphmatrices.jl b/src/linalg/graphmatrices.jl index 08e40499f..6642f66d0 100644 --- a/src/linalg/graphmatrices.jl +++ b/src/linalg/graphmatrices.jl @@ -1,7 +1,7 @@ const SparseMatrix{T} = SparseArrays.SparseMatrixCSC{T,Int64} """ - GraphMatrix{T} + GraphMatrix{T} An abstract type to allow opertions on any type of graph matrix """ @@ -9,7 +9,7 @@ abstract type GraphMatrix{T} end """ - Adjacency{T} + Adjacency{T} The core Adjacency matrix structure. Keeps the vertex degrees around. Subtypes are used to represent the different normalizations of the adjacency matrix. @@ -25,71 +25,71 @@ abstract type Adjacency{T} <: GraphMatrix{T} end abstract type Laplacian{T} <: GraphMatrix{T} end """ - CombinatorialAdjacency{T,S,V} + CombinatorialAdjacency{T,S,V} The standard adjacency matrix. """ struct CombinatorialAdjacency{T,S,V} <: Adjacency{T} - A::S - D::V + A::S + D::V end function CombinatorialAdjacency(A::SparseMatrix{T}) where T - D = vec(sum(A, dims=1)) - return CombinatorialAdjacency{T,SparseMatrix{T},typeof(D)}(A, D) + D = vec(sum(A, dims=1)) + return CombinatorialAdjacency{T,SparseMatrix{T},typeof(D)}(A, D) end """ - NormalizedAdjacency{T} + NormalizedAdjacency{T} The normalized adjacency matrix is ``\\hat{A} = D^{-1/2} A D^{-1/2}``. If A is symmetric, then the normalized adjacency is also symmetric with real eigenvalues bounded by [-1, 1]. """ struct NormalizedAdjacency{T} <: Adjacency{T} - A::CombinatorialAdjacency{T} - scalefactor::Vector{T} + A::CombinatorialAdjacency{T} + scalefactor::Vector{T} end function NormalizedAdjacency(adjmat::CombinatorialAdjacency) - sf = adjmat.D.^(-1 / 2) - return NormalizedAdjacency(adjmat, sf) + sf = adjmat.D.^(-1 / 2) + return NormalizedAdjacency(adjmat, sf) end """ - StochasticAdjacency{T} + StochasticAdjacency{T} A transition matrix for the random walk. """ struct StochasticAdjacency{T} <: Adjacency{T} - A::CombinatorialAdjacency{T} - scalefactor::Vector{T} + A::CombinatorialAdjacency{T} + scalefactor::Vector{T} end function StochasticAdjacency(adjmat::CombinatorialAdjacency) - sf = adjmat.D.^(-1) - return StochasticAdjacency(adjmat, sf) + sf = adjmat.D.^(-1) + return StochasticAdjacency(adjmat, sf) end """ - AveragingAdjacency{T} + AveragingAdjacency{T} The matrix whose action is to average over each neighborhood. """ struct AveragingAdjacency{T} <: Adjacency{T} - A::CombinatorialAdjacency{T} - scalefactor::Vector{T} + A::CombinatorialAdjacency{T} + scalefactor::Vector{T} end function AveragingAdjacency(adjmat::CombinatorialAdjacency) - sf = adjmat.D.^(-1) - return AveragingAdjacency(adjmat, sf) + sf = adjmat.D.^(-1) + return AveragingAdjacency(adjmat, sf) end perron(adjmat::NormalizedAdjacency) = sqrt.(adjmat.A.D) / LinearAlgebra.norm(sqrt.(adjmat.A.D)) struct PunchedAdjacency{T} <: Adjacency{T} - A::NormalizedAdjacency{T} - perron::Vector{T} + A::NormalizedAdjacency{T} + perron::Vector{T} end function PunchedAdjacency(adjmat::CombinatorialAdjacency) perron = sqrt.(adjmat.D) / LinearAlgebra.norm(sqrt.(adjmat.D)) @@ -99,7 +99,7 @@ end perron(m::PunchedAdjacency) = m.perron """ - Noop + Noop A type that represents no action. @@ -130,41 +130,41 @@ prescalefactor(adjmat::StochasticAdjacency) = adjmat.scalefactor struct CombinatorialLaplacian{T} <: Laplacian{T} - A::CombinatorialAdjacency{T} + A::CombinatorialAdjacency{T} end """ - NormalizedLaplacian{T} + NormalizedLaplacian{T} The normalized Laplacian is ``\\hat{L} = I - D^{-1/2} A D^{-1/2}``. If A is symmetric, then the normalized Laplacian is also symmetric with positive eigenvalues bounded by 2. """ struct NormalizedLaplacian{T} <: Laplacian{T} - A::NormalizedAdjacency{T} + A::NormalizedAdjacency{T} end """ - StochasticLaplacian{T} + StochasticLaplacian{T} Laplacian version of the StochasticAdjacency matrix. """ struct StochasticLaplacian{T} <: Laplacian{T} - A::StochasticAdjacency{T} + A::StochasticAdjacency{T} end """ - AveragingLaplacian{T} + AveragingLaplacian{T} Laplacian version of the AveragingAdjacency matrix. """ struct AveragingLaplacian{T} <: Laplacian{T} - A::AveragingAdjacency{T} + A::AveragingAdjacency{T} end arrayfunctions = (:eltype, :length, :ndims, :size, :strides) for f in arrayfunctions - @eval $f(a::GraphMatrix) = $f(a.A) + @eval $f(a::GraphMatrix) = $f(a.A) end LinearAlgebra.issymmetric(a::GraphMatrix) = LinearAlgebra.issymmetric(a.A) size(a::GraphMatrix, i::Integer) = size(a.A, i) @@ -172,14 +172,14 @@ LinearAlgebra.issymmetric(::StochasticAdjacency) = false LinearAlgebra.issymmetric(::AveragingAdjacency) = false """ - degrees(adjmat) + degrees(adjmat) Return the degrees of a graph represented by the [`CombinatorialAdjacency`](@ref) `adjmat`. """ degrees(adjmat::CombinatorialAdjacency) = adjmat.D """ - degrees(graphmx) + degrees(graphmx) Return the degrees of a graph represented by the graph matrix `graphmx`. """ @@ -195,14 +195,14 @@ convert(::Type{CombinatorialAdjacency}, adjmat::CombinatorialAdjacency) = adjmat function SparseArrays.sparse(lapl::M) where M <: Laplacian - adjmat = adjacency(lapl) - A = SparseArrays.sparse(adjmat) - L = SparseArrays.sparse(LinearAlgebra.Diagonal(SparseArrays.diag(lapl))) - A - return L + adjmat = adjacency(lapl) + A = SparseArrays.sparse(adjmat) + L = SparseArrays.sparse(LinearAlgebra.Diagonal(SparseArrays.diag(lapl))) - A + return L end function SparseMatrix(lapl::M) where M <: GraphMatrix - return SparseArrays.sparse(lapl) + return SparseArrays.sparse(lapl) end function SparseArrays.sparse(adjmat::Adjacency) @@ -213,10 +213,10 @@ end function convert(::Type{SparseMatrix{T}}, lapl::Laplacian{T}) where T - adjmat = adjacency(lapl) - A = convert(SparseMatrix{T}, adjmat) - L = SparseArrays.sparse(LinearAlgebra.Diagonal(SparseArrays.diag(lapl))) - A - return L + adjmat = adjacency(lapl) + A = convert(SparseMatrix{T}, adjmat) + L = SparseArrays.sparse(LinearAlgebra.Diagonal(SparseArrays.diag(lapl))) - A + return L end SparseArrays.diag(lapl::CombinatorialLaplacian) = lapl.A.D @@ -225,13 +225,13 @@ SparseArrays.diag(lapl::Laplacian) = ones(size(lapl)[2]) *(x::AbstractArray, ::Noop) = x *(::Noop, x) = x *(adjmat::Adjacency{T}, x::AbstractVector{T}) where T <: Number = - postscalefactor(adjmat) .* (adjmat.A * (prescalefactor(adjmat) .* x)) + postscalefactor(adjmat) .* (adjmat.A * (prescalefactor(adjmat) .* x)) *(adjmat::CombinatorialAdjacency{T}, x::AbstractVector{T}) where T <: Number = - adjmat.A * x + adjmat.A * x *(lapl::Laplacian{T}, x::AbstractVector{T}) where T <: Number = - (SparseArrays.diag(lapl) .* x) - (adjacency(lapl) * x) + (SparseArrays.diag(lapl) .* x) - (adjacency(lapl) * x) function *(adjmat::PunchedAdjacency{T}, x::AbstractVector{T}) where T <: Number @@ -269,41 +269,41 @@ function LinearAlgebra.mul!(Y, adjmat::PunchedAdjacency, x) end function LinearAlgebra.mul!(Y, lapl::Laplacian, B) - LinearAlgebra.mul!(Y, lapl.A, B) - z = SparseArrays.diag(lapl) .* B - Y[:] = z - Y[:] - return Y + LinearAlgebra.mul!(Y, lapl.A, B) + z = SparseArrays.diag(lapl) .* B + Y[:] = z - Y[:] + return Y end """ - symmetrize(A::SparseMatrix, which=:or) + symmetrize(A::SparseMatrix, which=:or) Return a symmetric version of graph (represented by sparse matrix `A`) as a sparse matrix. `which` may be one of `:triu`, `:tril`, `:sum`, or `:or`. Use `:sum` for weighted graphs. """ function symmetrize(A::SparseMatrix, which=:or) - if which == :or - M = A + SparseArrays.sparse(A') - M.nzval[M.nzval .== 2] .= 1 - return M + if which == :or + M = A + SparseArrays.sparse(A') + M.nzval[M.nzval .== 2] .= 1 + return M end - T = A - if which == :triu - T = LinearAlgebra.triu(A) - elseif which == :tril - T = LinearAlgebra.tril(A) + T = A + if which == :triu + T = LinearAlgebra.triu(A) + elseif which == :tril + T = LinearAlgebra.tril(A) elseif which == :sum - T = A + T = A else throw(ArgumentError("$which is not a supported method of symmetrizing a matrix")) end - M = T + SparseArrays.sparse(T') - return M + M = T + SparseArrays.sparse(T') + return M end """ - symmetrize(adjmat, which=:or) + symmetrize(adjmat, which=:or) Return a symmetric version of graph (represented by [`CombinatorialAdjacency`](@ref) `adjmat`) as a [`CombinatorialAdjacency`](@ref). `which` may be one of `:triu`, `:tril`, `:sum`, or `:or`. @@ -313,7 +313,7 @@ Use `:sum` for weighted graphs. Only works on [`Adjacency`](@ref) because the normalizations don't commute with symmetrization. """ symmetrize(adjmat::CombinatorialAdjacency, which=:or) = - CombinatorialAdjacency(symmetrize(adjmat.A, which)) + CombinatorialAdjacency(symmetrize(adjmat.A, which)) # per #564 @@ -324,7 +324,7 @@ symmetrize(adjmat::CombinatorialAdjacency, which=:or) = """ - LinAlg + LinAlg A package for using the type system to check types of graph matrices. """ diff --git a/src/shortestpaths/johnson.jl b/src/shortestpaths/johnson.jl index 9df66be0b..d83f44e42 100644 --- a/src/shortestpaths/johnson.jl +++ b/src/shortestpaths/johnson.jl @@ -48,22 +48,22 @@ function johnson_shortest_paths(g::AbstractGraph{U}, #Weight transform not needed if all weights are positive. if type_distmx != LightGraphs.DefaultDistance for e in edges(g) - distmx[src(e), dst(e)] += wt_transform[src(e)] - wt_transform[dst(e)] + distmx[src(e), dst(e)] += wt_transform[src(e)] - wt_transform[dst(e)] end end if !parallel dists = Matrix{T}(undef, nvg, nvg) parents = Matrix{U}(undef, nvg, nvg) - for v in vertices(g) - dijk_state = dijkstra_shortest_paths(g, v, distmx) - dists[v, :] = dijk_state.dists - parents[v, :] = dijk_state.parents - end + for v in vertices(g) + dijk_state = dijkstra_shortest_paths(g, v, distmx) + dists[v, :] = dijk_state.dists + parents[v, :] = dijk_state.parents + end else - dijk_state = parallel_multisource_dijkstra_shortest_paths(g, vertices(g), distmx) - dists = dijk_state.dists - parents = dijk_state.parents + dijk_state = parallel_multisource_dijkstra_shortest_paths(g, vertices(g), distmx) + dists = dijk_state.dists + parents = dijk_state.parents end broadcast!(-, dists, dists, wt_transform) From 7fb3df486fb5b5f80256c0154408fd034f8048a5 Mon Sep 17 00:00:00 2001 From: Seth Bromberger Date: Wed, 30 May 2018 14:55:03 -0700 Subject: [PATCH 4/4] tabs, again --- src/linalg/graphmatrices.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/linalg/graphmatrices.jl b/src/linalg/graphmatrices.jl index 6642f66d0..4f9f071aa 100644 --- a/src/linalg/graphmatrices.jl +++ b/src/linalg/graphmatrices.jl @@ -292,9 +292,9 @@ function symmetrize(A::SparseMatrix, which=:or) if which == :triu T = LinearAlgebra.triu(A) elseif which == :tril - T = LinearAlgebra.tril(A) + T = LinearAlgebra.tril(A) elseif which == :sum - T = A + T = A else throw(ArgumentError("$which is not a supported method of symmetrizing a matrix")) end