Skip to content

Overrides complement #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "StaticGraphs"
uuid = "4c8beaf5-199b-59a0-a7f2-21d17de635b6"
version = "0.3.0"
version = "0.3.1"

[deps]
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
Expand Down
30 changes: 30 additions & 0 deletions src/overrides.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Graphs.LinAlg: adjacency_matrix
import Graphs: induced_subgraph
import Graphs: complement

adjacency_matrix(g::StaticGraph{I,U}, T::DataType; dir = :out!) where I<:Integer where U<:Integer =
SparseMatrixCSC{T,I}(nv(g), nv(g), g.f_ind, g.f_vec, ones(T, ne(g)*2))
Expand Down Expand Up @@ -61,3 +62,32 @@ function induced_subgraph(g::StaticGraph{I, U}, vlist::AbstractVector{T}) where
end
return StaticGraph(f_vec, f_ind), T.(vlist)
end

function complement(g::StaticGraph)
gnv = nv(g)
ne(g) == gnv*(gnv-1) / 2 && return StaticGraph(gnv, Array{Tuple{UInt8, UInt8},1}())
sd1 = Array{Tuple{eltype(g), eltype(g)}, 1}()
for i = one(eltype(g)):gnv
for j = (i + one(eltype(g))):gnv
if !has_edge(g, i, j)
push!(sd1, (i, j))
end
end
end
ds1 = [Tuple(reverse(e)) for e in sd1]
sd = sort(vcat(sd1, ds1))
return StaticGraph(gnv, sd)
end

function complement(g::StaticDiGraph)
gnv = nv(g)
ne(g) == gnv*(gnv-1) && return StaticDiGraph(gnv, Array{Tuple{UInt8, UInt8},1}())
f_sd = Array{Tuple{eltype(g), eltype(g)}, 1}()
for i = one(eltype(g)):gnv, j = one(eltype(g)):gnv
if i != j && !has_edge(g, i, j)
push!(f_sd, (i, j))
end
end
b_sd = sort([Tuple(reverse(e)) for e in f_sd])
return StaticDiGraph(gnv, f_sd, b_sd)
end
32 changes: 22 additions & 10 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,27 @@ const testdir = dirname(@__FILE__)
@testset "staticgraph" begin
@test sprint(show, StaticGraph(Graph())) == "{0, 0} undirected simple static {UInt8, UInt8} graph"
g = smallgraph(:house)
gc = complement(g)
gu = squash(g)
sg = StaticGraph(g)
sgc = complement(sg)
sgu = StaticGraph(gu)
gempty = StaticGraph()
gdempty = StaticDiGraph()
@test eltype(StaticGraph{UInt128, UInt128}(sgu)) == UInt128
@test sprint(show, sg) == "{5, 6} undirected simple static {UInt8, UInt8} graph"
@test sprint(show, sgc) == "{5, 4} undirected simple static {UInt8, UInt8} graph"
@test sprint(show, sgu) == "{5, 6} undirected simple static {UInt8, UInt8} graph"
testfn(fn, args...) =
@inferred(fn(hu, args...)) ==
@inferred(fn(sg, args...)) ==
@inferred(fn(sgu, args...)) ==
function testfn(fn, args...)
@inferred(fn(hu, args...)) ==
@inferred(fn(sg, args...)) ==
@inferred(fn(sgu, args...)) ==
fn(g, args...)

@inferred(fn(sgc, args...)) ==
fn(gc, args...)
end
@test hu == sg == sgu
@test sgc == StaticGraph(gc)
@test @inferred eltype(hu) == UInt8
@test testfn(ne)
@test testfn(nv)
Expand Down Expand Up @@ -77,21 +83,27 @@ const testdir = dirname(@__FILE__)
@testset "staticdigraph" begin
@test sprint(show, StaticDiGraph(DiGraph())) == "{0, 0} directed simple static {UInt8, UInt8} graph"
dg = path_digraph(5)
dgc = complement(dg)
dgu = squash(dg)
dsg = StaticDiGraph(dg)
dsgc = complement(dsg)
dsgu = StaticDiGraph(dgu)
@test eltype(StaticDiGraph{UInt128, UInt128}(dsgu)) == UInt128
@test sprint(show, dsg) == "{5, 4} directed simple static {UInt8, UInt8} graph"
@test sprint(show, dsgc) == "{5, 16} directed simple static {UInt8, UInt8} graph"
@test sprint(show, dsgu) == "{5, 4} directed simple static {UInt8, UInt8} graph"
dhu = loadgraph(joinpath(testdir, "testdata", "pathdg-uint8.jsg"), SDGFormat())

dtestfn(fn, args...) =
@inferred(fn(dhu, args...)) ==
@inferred(fn(dsg, args...)) ==
@inferred(fn(dsgu, args...)) ==
function dtestfn(fn, args...)
@inferred(fn(dhu, args...)) ==
@inferred(fn(dsg, args...)) ==
@inferred(fn(dsgu, args...)) ==
fn(dg, args...)
@inferred(fn(dsgc, args...)) ==
fn(dgc, args...)
end

@test dhu == dsg == dsgu
@test dsgc == StaticDiGraph(dgc)
@test @inferred eltype(dhu) == UInt8
@test dtestfn(ne)
@test dtestfn(nv)
Expand Down