Skip to content

Commit ecbcbbf

Browse files
authored
Merge pull request #15 from JuliaGraphs/sbromberger/adjmx
adjacency matrices. Fixes #8
2 parents af451dc + 76cc078 commit ecbcbbf

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

src/StaticGraphs.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module StaticGraphs
22

33
using LightGraphs
44
using JLD2
5+
using SparseArrays
56

67
import Base:
78
convert, eltype, show, ==, Pair, Tuple, in, copy, length, issubset, zero, one,
@@ -12,8 +13,7 @@ import LightGraphs:
1213
src, dst, edgetype, nv, ne, vertices, edges, is_directed,
1314
has_vertex, has_edge, inneighbors, outneighbors,
1415
indegree, outdegree, degree, insorted, squash,
15-
16-
AbstractGraphFormat, loadgraph, savegraph
16+
AbstractGraphFormat, loadgraph, savegraph, reverse
1717

1818
import LightGraphs.SimpleGraphs:
1919
AbstractSimpleGraph,

src/overrides.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
function adjacency_matrix(g::StaticGraph)
2-
SparseMatrixCSC{Bool,UInt32}(nv(g), nv(g), g.f_ind, g.f_vec, ones(Bool, ne(g)*2))
3-
end
1+
import LightGraphs.LinAlg: adjacency_matrix
2+
3+
adjacency_matrix(g::StaticGraph{I,U}, T::DataType; dir = :out) where I<:Integer where U<:Integer =
4+
SparseMatrixCSC{T,I}(nv(g), nv(g), g.f_ind, g.f_vec, ones(T, ne(g)*2))
45

5-
function adjacency_matrix(g::StaticDiGraph)
6-
SparseMatrixCSC{Bool,UInt32}(nv(g), nv(g), g.f_ind, g.f_vec, ones(Bool, ne(g)))
6+
function adjacency_matrix(g::StaticDiGraph{I,U}, T::DataType; dir = :out) where I<:Integer where U<:Integer
7+
if dir == :in
8+
return SparseMatrixCSC{T,I}(nv(g), nv(g), g.f_ind, g.f_vec, ones(T, ne(g)*2))
9+
end
10+
z = SparseMatrixCSC{T,I}(nv(g), nv(g), g.b_ind, g.b_vec, ones(T, ne(g)))
11+
dir != :out && @warn("direction `$dir` not defined for adjacency matrices on StaticGraphs; defaulting to `out`")
12+
return z
713
end

src/staticdigraph.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,5 @@ is_directed(::Type{StaticDiGraph}) = true
9191
is_directed(::Type{StaticDiGraph{T}}) where T = true
9292
is_directed(::Type{StaticDiGraph{T, U}}) where T where U = true
9393
is_directed(g::StaticDiGraph) = true
94+
95+
reverse(g::StaticDiGraph) = StaticDiGraph(copy(g.b_vec), copy(g.b_ind), copy(g.f_vec), copy(g.f_ind))

test/runtests.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,19 @@ const testdir = dirname(@__FILE__)
5252
@test @inferred !is_directed(hu)
5353
@test @inferred !is_directed(StaticGraph)
5454
@test @inferred collect(edges(hu)) == collect(edges(sg))
55+
56+
z = @inferred(adjacency_matrix(hu, Bool, dir=:out))
57+
@test z[1,2]
58+
@test !z[1,4]
59+
@test z == adjacency_matrix(hu, Bool, dir=:in) == adjacency_matrix(hu, Bool, dir=:both)
60+
5561
# empty constructors
5662
@test nv(gempty) === 0x00
5763
@test typeof(LightGraphs.nv(gempty)) == UInt8
5864
@test length(LightGraphs.edges(gempty)) === 0x00
5965
@test nv(gdempty) === 0x00
6066
@test length(LightGraphs.edges(gdempty)) === 0x00
67+
6168
end # staticgraph
6269

6370
@testset "staticdigraph" begin
@@ -99,6 +106,15 @@ const testdir = dirname(@__FILE__)
99106
@test @inferred is_directed(dhu)
100107
@test @inferred is_directed(StaticDiGraph)
101108
@test @inferred collect(edges(dhu)) == collect(edges(dsg))
109+
110+
z = @inferred(adjacency_matrix(dhu, Bool, dir=:out))
111+
@test z[1,2]
112+
@test !z[2,1]
113+
z = @inferred(adjacency_matrix(dhu, Bool, dir=:in))
114+
@test z[2,1]
115+
@test !z[1,2]
116+
@test_logs (:warn, r".*") adjacency_matrix(dhu, Bool, dir=:both) == adjacency_matrix(dhu, Bool)
117+
102118
end # staticdigraph
103119

104120
@testset "utils" begin

0 commit comments

Comments
 (0)