Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix bug in adjacency_matrix(::SimpleGraph) for Julia 1.7 #15

Merged
merged 1 commit into from
Oct 22, 2021

Conversation

mtfishman
Copy link
Contributor

This fixes a bug in adjacency_matrix(::SimpleGraph) for Julia 1.7.

This PR is moved from sbromberger/LightGraphs.jl#1595 and fixes the issue described in sbromberger/LightGraphs.jl#1594.

As described in sbromberger/LightGraphs.jl#1594, Julia 1.7 adds a stricter check on the SparseMatrixCSC constructor for consistency about the number of nonzero elements input. The number of nonzero elements are currently being overcounted in the case of SimpleGraph with self-loops, this should fix that overcounting.

Here is a minimal example:

julia> versioninfo()
Julia Version 1.7.0-rc1
Commit 9eade6195e (2021-09-12 06:45 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin19.6.0)
  CPU: Intel(R) Core(TM) m3-7Y32 CPU @ 1.10GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, skylake)

(@v1.7) pkg> st Graphs
      Status `~/.julia/environments/v1.7/Project.toml`
  [86223c79] Graphs v1.4.1 `~/.julia/dev/Graphs`

julia> using Graphs

julia> g = Graph(Edge.([1 => 2, 2 => 3]))
{3, 2} undirected simple Int64 graph

julia> adjacency_matrix(g)
3×3 SparseArrays.SparseMatrixCSC{Int64, Int64} with 4 stored entries:
   1  
 1    1
   1  

julia> add_edge!(g, 1 => 1)
true

julia> adjacency_matrix(g)
3×3 SparseArrays.SparseMatrixCSC{Int64, Int64} with 5 stored entries:
 2  1  
 1    1
   1  

Without this fix I get:

(@v1.7) pkg> st Graphs
      Status `~/.julia/environments/v1.7/Project.toml`
  [86223c79] Graphs v1.4.1

julia> using Graphs

julia> g = Graph(Edge.([1 => 2, 2 => 3]))
{3, 2} undirected simple Int64 graph

julia> adjacency_matrix(g)
3×3 SparseArrays.SparseMatrixCSC{Int64, Int64} with 4 stored entries:
   1  
 1    1
   1  

julia> add_edge!(g, 1 => 1)
true

julia> adjacency_matrix(g)
ERROR: ArgumentError: Illegal buffers for SparseMatrixCSC construction 3 [1, 3, 5, 6] [1, 2, 1, 3, 2] [1, 1, 1, 1, 1, 1]
Stacktrace:
 [1] SparseMatrixCSC
   @ /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.7/SparseArrays/src/sparsematrix.jl:29 [inlined]
 [2] SparseArrays.SparseMatrixCSC(m::Int64, n::Int64, colptr::Vector{Int64}, rowval::Vector{Int64}, nzval::Vector{Int64})
   @ SparseArrays /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.7/SparseArrays/src/sparsematrix.jl:44
 [3] _adjacency_matrix(g::SimpleGraph{Int64}, T::DataType, neighborfn::typeof(inneighbors), nzmult::Int64)
   @ Graphs.LinAlg ~/.julia/packages/Graphs/Mih78/src/linalg/spectral.jl:63
 [4] #adjacency_matrix#2
   @ ~/.julia/dev/Graphs/src/linalg/spectral.jl:25 [inlined]
 [5] adjacency_matrix (repeats 2 times)
   @ ~/.julia/dev/Graphs/src/linalg/spectral.jl:20 [inlined]
 [6] top-level scope
   @ REPL[24]:1

@mtfishman mtfishman changed the title Fix bug when making an adjacency_matrix from a SimpleGraph with self-… Fix bug in adjacency_matrix(::SimpleGraph) for Julia 1.7 Oct 20, 2021
@mtfishman
Copy link
Contributor Author

Also note that a variety of other tests fail on Julia 1.7, many related to sbromberger/LightGraphs.jl#1597.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants