Skip to content

Commit 3c5a741

Browse files
committed
adjacency matrices. Fixes #8
1 parent 19c3d81 commit 3c5a741

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
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: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1-
adjacency_matrix(g::StaticGraph) = SparseMatrixCSC{Bool,UInt32}(nv(g), nv(g), g.f_ind, g.f_vec, ones(Bool, ne(g)*2))
2-
adjacency_matrix(g::StaticDiGraph) = SparseMatrixCSC{Bool,UInt32}(nv(g), nv(g), g.f_ind, g.f_vec, ones(Bool, ne(g)))
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))
5+
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
13+
end
14+

src/staticdigraph.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,5 @@ Return `true` if `g` is a directed graph.
8787
is_directed(::Type{StaticDiGraph}) = true
8888
is_directed(::Type{StaticDiGraph{T, U}}) where T where U = true
8989
is_directed(g::StaticDiGraph) = true
90+
91+
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ const testdir = dirname(@__FILE__)
5050
@test @inferred !is_directed(hu)
5151
@test @inferred !is_directed(StaticGraph)
5252
@test @inferred collect(edges(hu)) == collect(edges(sg))
53+
54+
z = @inferred(adjacency_matrix(hu, Bool, dir=:out))
55+
@test z[1,2]
56+
@test !z[1,4]
57+
@test z == adjacency_matrix(hu, Bool, dir=:in) == adjacency_matrix(hu, Bool, dir=:both)
5358
end # staticgraph
5459

5560
@testset "staticdigraph" begin
@@ -91,6 +96,15 @@ const testdir = dirname(@__FILE__)
9196
@test @inferred is_directed(dhu)
9297
@test @inferred is_directed(StaticDiGraph)
9398
@test @inferred collect(edges(dhu)) == collect(edges(dsg))
99+
100+
z = @inferred(adjacency_matrix(dhu, Bool, dir=:out))
101+
@test z[1,2]
102+
@test !z[2,1]
103+
z = @inferred(adjacency_matrix(dhu, Bool, dir=:in))
104+
@test z[2,1]
105+
@test !z[1,2]
106+
@test_logs (:warn, r".*") adjacency_matrix(dhu, Bool, dir=:both) == adjacency_matrix(dhu, Bool)
107+
94108
end # staticdigraph
95109

96110
@testset "utils" begin

0 commit comments

Comments
 (0)