From 741b02de98822dc310d7d3a2db673e30e3a32351 Mon Sep 17 00:00:00 2001 From: gzagatti Date: Tue, 1 Mar 2022 16:02:07 +0800 Subject: [PATCH] overrides complement. --- Project.toml | 2 +- src/overrides.jl | 30 ++++++++++++++++++++++++++++++ test/runtests.jl | 32 ++++++++++++++++++++++---------- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/Project.toml b/Project.toml index 0d77389..9bc5a17 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/overrides.jl b/src/overrides.jl index ec5f359..2175109 100644 --- a/src/overrides.jl +++ b/src/overrides.jl @@ -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)) @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index d770e89..665c31e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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) @@ -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)