Skip to content

Commit

Permalink
Fix adjacency_matrix when dir=:both (#233)
Browse files Browse the repository at this point in the history
* Fix adjacency_matrix

* Add tests adjacency_matrix
  • Loading branch information
KeishiS committed May 1, 2023
1 parent 39339cc commit 5649092
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/linalg/spectral.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,12 @@ function _adjacency_matrix(
for j in 1:n_v # this is by column, not by row.
if has_edge(g, j, j)
push!(selfloops, j)
if !(T <: Bool) && !is_directed(g)
nz -= 1
end
end
dsts = sort(collect(neighborfn(g, j))) # TODO for most graphs it might not be necessary to sort
colpt[j + 1] = colpt[j] + length(dsts)
append!(rowval, dsts)
end
spmx = SparseMatrixCSC(n_v, n_v, colpt, rowval, ones(T, nz))
spmx = SparseMatrixCSC(n_v, n_v, colpt, rowval, ones(T, length(rowval)))

# this is inefficient. There should be a better way of doing this.
# the issue is that adjacency matrix entries for self-loops are 2,
Expand Down
8 changes: 8 additions & 0 deletions test/linalg/spectral.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,12 @@ Matrix(nbt::Nonbacktracking) = Matrix(sparse(nbt))
@test spectral_distance(g, g, 1) 0 atol = 1e-8
end
end

@testset "adjacency_matrix with `dir=:both`" begin
edges = [Edge(1, 3), Edge(1, 4), Edge(2, 1), Edge(4, 1)]
g = SimpleDiGraph(edges)
@test all(adjacency_matrix(g; dir=:out) .== [0 0 1 1; 1 0 0 0; 0 0 0 0; 1 0 0 0])
@test all(adjacency_matrix(g; dir=:in) .== [0 1 0 1; 0 0 0 0; 1 0 0 0; 1 0 0 0])
@test all(adjacency_matrix(g; dir=:both) .== [0 1 1 1; 1 0 0 0; 1 0 0 0; 1 0 0 0])
end
end

0 comments on commit 5649092

Please sign in to comment.