diff --git a/Project.toml b/Project.toml index 9266b44..54865da 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "DataGraphs" uuid = "b5a273c3-7e6c-41f6-98bd-8d7f1525a36a" -version = "0.2.12" authors = ["Matthew Fishman and contributors"] +version = "0.2.13" [deps] Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4" @@ -19,7 +19,7 @@ DataGraphsGraphsFlowsExt = "GraphsFlows" Dictionaries = "0.4" Graphs = "1" GraphsFlows = "0.1.1" -NamedGraphs = "0.6.9, 0.7, 0.8" +NamedGraphs = "0.8.2" SimpleTraits = "0.9" julia = "1.7" diff --git a/src/abstractdatagraph.jl b/src/abstractdatagraph.jl index e533790..46430b4 100644 --- a/src/abstractdatagraph.jl +++ b/src/abstractdatagraph.jl @@ -40,6 +40,11 @@ edge_data_eltype(graph::AbstractDataGraph) = eltype(edge_data(graph)) Base.zero(graph_type::Type{<:AbstractDataGraph}) = graph_type() +# Graphs overloads +function Graphs.vertices(graph::AbstractDataGraph) + return Graphs.vertices(underlying_graph(graph)) +end + # Graphs overloads for f in [ :(Graphs.a_star), @@ -92,7 +97,6 @@ for f in [ :(Graphs.steiner_tree), :(Graphs.topological_sort_by_dfs), :(Graphs.tree), - :(Graphs.vertices), :(GraphsExtensions.boundary_edges), :(GraphsExtensions.boundary_vertices), :(GraphsExtensions.eccentricities), @@ -402,7 +406,7 @@ function Base.setindex!(graph::AbstractDataGraph, x, i1, i2, i...) return graph end -function Graphs.induced_subgraph(graph::AbstractDataGraph, subvertices) +function induced_subgraph_datagraph(graph::AbstractDataGraph, subvertices) underlying_subgraph, vlist = Graphs.induced_subgraph(underlying_graph(graph), subvertices) subgraph = similar_type(graph)(underlying_subgraph) for v in vertices(subgraph) @@ -417,6 +421,15 @@ function Graphs.induced_subgraph(graph::AbstractDataGraph, subvertices) end return subgraph, vlist end +function Graphs.induced_subgraph(graph::AbstractDataGraph, subvertices) + return induced_subgraph_datagraph(graph, subvertices) +end +# Fix ambiguity with Graphs.jl for integer `subvertices`. +function Graphs.induced_subgraph( + graph::AbstractDataGraph, subvertices::AbstractVector{<:Integer} + ) + return induced_subgraph_datagraph(graph, subvertices) +end # # Printing diff --git a/test/Project.toml b/test/Project.toml index 3d7834e..2504a50 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -15,7 +15,7 @@ DataGraphs = "0.2.6" Dictionaries = "0.4.4" Graphs = "1.12" GraphsFlows = "0.1.1" -NamedGraphs = "0.6.6, 0.7, 0.8" +NamedGraphs = "0.8.2" SafeTestsets = "0.1" Suppressor = "0.2.8" Test = "1.10" diff --git a/test/test_basics.jl b/test/test_basics.jl index 31556b5..6270d6c 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -26,7 +26,7 @@ using Graphs: using Graphs.SimpleGraphs: SimpleDiGraph, SimpleEdge, SimpleGraph using GraphsFlows: GraphsFlows using NamedGraphs: NamedDiGraph, NamedEdge, NamedGraph -using NamedGraphs.GraphsExtensions: ⊔, rename_vertices, vertextype +using NamedGraphs.GraphsExtensions: ⊔, rename_vertices, subgraph, vertextype using NamedGraphs.NamedGraphGenerators: named_grid, named_path_graph using NamedGraphs.OrdinalIndexing: nd, st, rd, th using Test: @test, @test_broken, @testset @@ -193,6 +193,40 @@ using Test: @test, @test_broken, @testset @test dg[1 => 2] == :default end + @testset "subgraph" begin + dg = DataGraph(named_path_graph(4)) + dg[1] = "V1" + dg[2] = "V2" + dg[3] = "V3" + dg[4] = "V4" + dg[1 => 2] = "E12" + dg[2 => 3] = "E23" + dg[3 => 4] = "E34" + sg = subgraph(dg, [2, 3, 4]) + @test nv(sg) == 3 + @test ne(sg) == 2 + @test !has_vertex(sg, 1) + @test has_vertex(sg, 2) + @test has_vertex(sg, 3) + @test has_vertex(sg, 4) + @test !has_edge(sg, 1 => 2) + @test !has_edge(sg, 2 => 1) + @test has_edge(sg, 2 => 3) + @test has_edge(sg, 3 => 2) + @test has_edge(sg, 3 => 4) + @test has_edge(sg, 4 => 3) + @test isnothing(get(sg, 1, nothing)) + @test sg[2] == "V2" + @test sg[3] == "V3" + @test sg[4] == "V4" + @test isnothing(get(sg, 1 => 2, nothing)) + @test isnothing(get(sg, 2 => 1, nothing)) + @test sg[2 => 3] == "E23" + @test sg[3 => 2] == "E23" + @test sg[3 => 4] == "E34" + @test sg[4 => 3] == "E34" + end + @testset "Constructors specifying vertex type" begin dg = DataGraph{Float64}( named_path_graph(4); vertex_data_eltype = String, edge_data_eltype = Symbol