Skip to content

Commit 5649092

Browse files
authored
Fix adjacency_matrix when dir=:both (#233)
* Fix adjacency_matrix * Add tests adjacency_matrix
1 parent 39339cc commit 5649092

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/linalg/spectral.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,12 @@ function _adjacency_matrix(
5757
for j in 1:n_v # this is by column, not by row.
5858
if has_edge(g, j, j)
5959
push!(selfloops, j)
60-
if !(T <: Bool) && !is_directed(g)
61-
nz -= 1
62-
end
6360
end
6461
dsts = sort(collect(neighborfn(g, j))) # TODO for most graphs it might not be necessary to sort
6562
colpt[j + 1] = colpt[j] + length(dsts)
6663
append!(rowval, dsts)
6764
end
68-
spmx = SparseMatrixCSC(n_v, n_v, colpt, rowval, ones(T, nz))
65+
spmx = SparseMatrixCSC(n_v, n_v, colpt, rowval, ones(T, length(rowval)))
6966

7067
# this is inefficient. There should be a better way of doing this.
7168
# the issue is that adjacency matrix entries for self-loops are 2,

test/linalg/spectral.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,12 @@ Matrix(nbt::Nonbacktracking) = Matrix(sparse(nbt))
186186
@test spectral_distance(g, g, 1) 0 atol = 1e-8
187187
end
188188
end
189+
190+
@testset "adjacency_matrix with `dir=:both`" begin
191+
edges = [Edge(1, 3), Edge(1, 4), Edge(2, 1), Edge(4, 1)]
192+
g = SimpleDiGraph(edges)
193+
@test all(adjacency_matrix(g; dir=:out) .== [0 0 1 1; 1 0 0 0; 0 0 0 0; 1 0 0 0])
194+
@test all(adjacency_matrix(g; dir=:in) .== [0 1 0 1; 0 0 0 0; 1 0 0 0; 1 0 0 0])
195+
@test all(adjacency_matrix(g; dir=:both) .== [0 1 1 1; 1 0 0 0; 1 0 0 0; 1 0 0 0])
196+
end
189197
end

0 commit comments

Comments
 (0)