Skip to content

Commit fc3c473

Browse files
authored
Merge pull request #19 from JuliaGraphs/sbromberger/induced
Sbromberger/induced
2 parents f10be3e + c96abcf commit fc3c473

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
julia 1.0
2-
LightGraphs 1.0
2+
LightGraphs 1.0.2
33
JLD2

src/overrides.jl

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import LightGraphs.LinAlg: adjacency_matrix
2+
import LightGraphs: induced_subgraph
23

3-
adjacency_matrix(g::StaticGraph{I,U}, T::DataType; dir = :out) where I<:Integer where U<:Integer =
4+
adjacency_matrix(g::StaticGraph{I,U}, T::DataType; dir = :out!) where I<:Integer where U<:Integer =
45
SparseMatrixCSC{T,I}(nv(g), nv(g), g.f_ind, g.f_vec, ones(T, ne(g)*2))
56

67
function adjacency_matrix(g::StaticDiGraph{I,U}, T::DataType; dir = :out) where I<:Integer where U<:Integer
@@ -11,3 +12,52 @@ function adjacency_matrix(g::StaticDiGraph{I,U}, T::DataType; dir = :out) where
1112
dir != :out && @warn("direction `$dir` not defined for adjacency matrices on StaticGraphs; defaulting to `out`")
1213
return z
1314
end
15+
16+
# induced subgraphs preserve the eltypes of the vertices.
17+
function induced_subgraph(g::StaticDiGraph{I, U}, vlist::AbstractVector{T}) where T <: Integer where I <: Integer where U<:Integer
18+
vlist_len = length(vlist)
19+
f_vec = Vector{I}()
20+
b_vec = Vector{I}()
21+
f_ind = Vector{U}([1])
22+
b_ind = Vector{U}([1])
23+
24+
let vset = I.(vlist) # needed because of julialang/julia/ issue #15276
25+
sizehint!(f_ind, vlist_len+1)
26+
sizehint!(b_ind, vlist_len+1)
27+
28+
vlist_len == length(vset) || throw(ArgumentError("Vertices in subgraph list must be unique"))
29+
fpos = 1
30+
bpos = 1
31+
@inbounds for v in vlist
32+
o = filter(x -> x in vset, outneighbors(g, v))
33+
i = filter(x -> x in vset, inneighbors(g, v))
34+
35+
fpos += length(o)
36+
bpos += length(i)
37+
38+
append!(f_vec, o)
39+
append!(b_vec, i)
40+
push!(f_ind, fpos)
41+
push!(b_ind, bpos)
42+
end
43+
end
44+
return StaticDiGraph(f_vec, f_ind, b_vec, b_ind), T.(vlist)
45+
end
46+
47+
function induced_subgraph(g::StaticGraph{I, U}, vlist::AbstractVector{T}) where T <: Integer where I <: Integer where U<:Integer
48+
vlist_len = length(vlist)
49+
f_vec = Vector{I}()
50+
f_ind = Vector{U}([1])
51+
let vset = I.(vlist) # needed because of julialang/julia/ issue #15276
52+
sizehint!(f_ind, vlist_len + 1)
53+
vlist_len == length(vset) || throw(ArgumentError("Vertices in subgraph list must be unique"))
54+
fpos = 1
55+
for v in vlist
56+
o = filter(x -> x in vset, outneighbors(g, v))
57+
fpos += length(o)
58+
append!(f_vec, o)
59+
push!(f_ind, fpos)
60+
end
61+
end
62+
return StaticGraph(f_vec, f_ind), T.(vlist)
63+
end

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ const testdir = dirname(@__FILE__)
5353
@test @inferred !is_directed(StaticGraph)
5454
@test @inferred collect(edges(hu)) == collect(edges(sg))
5555

56+
(g1, m1) = @inferred induced_subgraph(hu, [3,2,1])
57+
g2 = @inferred hu[3:-1:1]
58+
@test g1 == g2
59+
@test m1 == [3,2,1]
60+
5661
z = @inferred(adjacency_matrix(hu, Bool, dir=:out))
5762
@test z[1,2]
5863
@test !z[1,4]
@@ -107,6 +112,11 @@ const testdir = dirname(@__FILE__)
107112
@test @inferred is_directed(StaticDiGraph)
108113
@test @inferred collect(edges(dhu)) == collect(edges(dsg))
109114

115+
(g1, m1) = @inferred induced_subgraph(dhu, [3,2,1])
116+
g2 = @inferred dhu[3:-1:1]
117+
@test g1 == g2
118+
@test m1 == [3,2,1]
119+
110120
z = @inferred(adjacency_matrix(dhu, Bool, dir=:out))
111121
@test z[1,2]
112122
@test !z[2,1]

0 commit comments

Comments
 (0)