From a8535ec11b5b8fd9dc836ff2cfec2b111f74451c Mon Sep 17 00:00:00 2001 From: Miles Date: Tue, 28 May 2024 18:57:46 -0400 Subject: [PATCH] Remove degree-zero vertices from steiner_tree output (#82) --- src/steiner_tree.jl | 6 +++++- test/test_namedgraph.jl | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/steiner_tree.jl b/src/steiner_tree.jl index c01207f..6ccc8db 100644 --- a/src/steiner_tree.jl +++ b/src/steiner_tree.jl @@ -9,5 +9,9 @@ using SimpleTraits: SimpleTraits, Not, @traitfn map(v -> vertex_positions(g)[v], term_vert), dist_matrix_to_position_dist_matrix(g, distmx), ) - return typeof(g)(position_tree, map(v -> ordered_vertices(g)[v], vertices(position_tree))) + tree = typeof(g)(position_tree, map(v -> ordered_vertices(g)[v], vertices(position_tree))) + for v in copy(vertices(tree)) + iszero(degree(tree, v)) && rem_vertex!(tree, v) + end + return tree end diff --git a/test/test_namedgraph.jl b/test/test_namedgraph.jl index 2d9e6c0..5ec2802 100644 --- a/test/test_namedgraph.jl +++ b/test/test_namedgraph.jl @@ -675,7 +675,8 @@ end st = steiner_tree(g, terminal_vertices) es = [(1, 2) => (1, 3), (1, 3) => (1, 4), (1, 4) => (2, 4), (2, 4) => (3, 4)] @test ne(st) == 4 - @test nv(st) == 12 + @test nv(st) == 5 + @test !any(v -> iszero(degree(st, v)), vertices(st)) for e in es @test has_edge(st, e) end