From 47598e32a7de96264c1e1db9b6e062cf18df2132 Mon Sep 17 00:00:00 2001 From: Joey Date: Thu, 7 Nov 2024 16:08:38 -0500 Subject: [PATCH 1/2] Fix ambiguity on steiner_tree --- src/steiner_tree.jl | 14 +++++++++++++- test/test_namedgraph.jl | 10 ++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/steiner_tree.jl b/src/steiner_tree.jl index c01207f..1672955 100644 --- a/src/steiner_tree.jl +++ b/src/steiner_tree.jl @@ -1,7 +1,7 @@ using Graphs: Graphs, IsDirected, nv, steiner_tree using SimpleTraits: SimpleTraits, Not, @traitfn -@traitfn function Graphs.steiner_tree( +@traitfn function namedgraph_steiner_tree( g::AbstractNamedGraph::(!IsDirected), term_vert, distmx=weights(g) ) position_tree = steiner_tree( @@ -11,3 +11,15 @@ using SimpleTraits: SimpleTraits, Not, @traitfn ) return typeof(g)(position_tree, map(v -> ordered_vertices(g)[v], vertices(position_tree))) end + +@traitfn function Graphs.steiner_tree( + g::AbstractNamedGraph::(!IsDirected), term_vert, args... +) + return namedgraph_steiner_tree(g, term_vert, args...) +end + +@traitfn function Graphs.steiner_tree( + g::AbstractNamedGraph::(!IsDirected), term_vert::Vector{<:Integer}, args... +) + return namedgraph_steiner_tree(g, term_vert, args...) +end diff --git a/test/test_namedgraph.jl b/test/test_namedgraph.jl index 2d9e6c0..f28f96c 100644 --- a/test/test_namedgraph.jl +++ b/test/test_namedgraph.jl @@ -679,6 +679,16 @@ end for e in es @test has_edge(st, e) end + + g = named_path_graph(4) + terminal_vertices = [1, 3] + st = steiner_tree(g, terminal_vertices) + es = [1 => 2, 2 => 3] + @test ne(st) == 2 + @test nv(st) == 3 + for e in es + @test has_edge(st, e) + end end @testset "topological_sort_by_dfs" begin g = NamedDiGraph(["A", "B", "C", "D", "E", "F", "G"]) From 1f8b9e5e8d55eb9bfe6f4dbb4d8bc75ec8b47ca1 Mon Sep 17 00:00:00 2001 From: Joey Date: Wed, 18 Dec 2024 11:03:14 -0500 Subject: [PATCH 2/2] Fix bug in graph partitioning --- src/lib/PartitionedGraphs/src/partitionedgraph.jl | 10 +++++----- test/test_partitionedgraph.jl | 6 ++++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/lib/PartitionedGraphs/src/partitionedgraph.jl b/src/lib/PartitionedGraphs/src/partitionedgraph.jl index b0ab386..5fa1ade 100644 --- a/src/lib/PartitionedGraphs/src/partitionedgraph.jl +++ b/src/lib/PartitionedGraphs/src/partitionedgraph.jl @@ -90,11 +90,11 @@ function partitionedges(pg::PartitionedGraph) end function Graphs.edges(pg::PartitionedGraph, partitionedge::PartitionEdge) - psrc_vs = vertices(pg, PartitionVertex(src(partitionedge))) - pdst_vs = vertices(pg, PartitionVertex(dst(partitionedge))) - psrc_subgraph = subgraph(unpartitioned_graph(pg), psrc_vs) - pdst_subgraph = subgraph(pg, pdst_vs) - full_subgraph = subgraph(pg, vcat(psrc_vs, pdst_vs)) + psrc_vs = vertices(pg, src(partitionedge)) + pdst_vs = vertices(pg, dst(partitionedge)) + psrc_subgraph, _ = induced_subgraph(unpartitioned_graph(pg), psrc_vs) + pdst_subgraph, _ = induced_subgraph(pg, pdst_vs) + full_subgraph, _ = induced_subgraph(pg, vcat(psrc_vs, pdst_vs)) return setdiff(edges(full_subgraph), vcat(edges(psrc_subgraph), edges(pdst_subgraph))) end diff --git a/test/test_partitionedgraph.jl b/test/test_partitionedgraph.jl index 49db662..6d3e034 100644 --- a/test/test_partitionedgraph.jl +++ b/test/test_partitionedgraph.jl @@ -28,6 +28,7 @@ using NamedGraphs.GraphsExtensions: vertextype using NamedGraphs.NamedGraphGenerators: named_comb_tree, named_grid, named_triangular_lattice_graph +using NamedGraphs.OrderedDictionaries: OrderedDictionary using NamedGraphs.PartitionedGraphs: PartitionEdge, PartitionedGraph, @@ -52,7 +53,7 @@ using Test: @test, @testset pg = PartitionedGraph(g, partitions) @test vertextype(partitioned_graph(pg)) == Int64 @test vertextype(unpartitioned_graph(pg)) == vertextype(g) - @test isa(partitionvertices(pg), Dictionary{Int64,PartitionVertex{Int64}}) + @test isa(partitionvertices(pg), OrderedDictionary{Int64,PartitionVertex{Int64}}) @test isa(partitionedges(pg), Vector{PartitionEdge{Int64,NamedEdge{Int64}}}) @test is_tree(partitioned_graph(pg)) @test nv(pg) == nx * ny @@ -67,7 +68,7 @@ using Test: @test, @testset @test vertextype(unpartitioned_graph(pg)) == vertextype(g) @test isa( partitionvertices(pg), - Dictionary{Tuple{Int64,Int64},PartitionVertex{Tuple{Int64,Int64}}}, + OrderedDictionary{Tuple{Int64,Int64},PartitionVertex{Tuple{Int64,Int64}}}, ) @test isa( partitionedges(pg), @@ -109,6 +110,7 @@ end inter_column_edges = [(1, 1, i) => (2, 1, i) for i in 1:nz] @test length(partitionedges(pg, inter_column_edges)) == 1 @test length(partitionvertices(pg, [(1, 2, i) for i in 1:nz])) == 1 + @test all([length(edges(pg, pe)) == nz for pe in partitionedges(pg)]) boundary_sizes = [length(boundary_partitionedges(pg, pv)) for pv in partitionvertices(pg)] #Partitions into a square grid so each partition should have maximum 4 incoming edges and minimum 2