diff --git a/src/spanningtrees/kruskal.jl b/src/spanningtrees/kruskal.jl index 52cb936ae..883e39495 100644 --- a/src/spanningtrees/kruskal.jl +++ b/src/spanningtrees/kruskal.jl @@ -1,5 +1,5 @@ """ - kruskal_mst(g, distmx=weights(g); minimize=true) + kruskal_mst(g, distmx=weights(g); minimize=true) Return a vector of edges representing the minimum (by default) spanning tree of a connected, undirected graph `g` with optional distance matrix `distmx` using [Kruskal's algorithm](https://en.wikipedia.org/wiki/Kruskal%27s_algorithm). @@ -15,6 +15,7 @@ function kruskal_mst end connected_vs = IntDisjointSets(nv(g)) mst = Vector{edgetype(g)}() + nv(g) <= 1 && return mst sizehint!(mst, nv(g) - 1) weights = Vector{T}() diff --git a/src/steinertree/steiner_tree.jl b/src/steinertree/steiner_tree.jl index 6f7713cd1..9676b9ae6 100644 --- a/src/steinertree/steiner_tree.jl +++ b/src/steinertree/steiner_tree.jl @@ -1,5 +1,5 @@ """ - filter_non_term_leaves!(g, term_vert) + filter_non_term_leaves!(g, term_vert) Remove edges of `g` so that all non-isolated leaves of `g` are in the set `term_vert` """ @@ -44,6 +44,8 @@ function steiner_tree end g::AG::(!IsDirected), term_vert::Vector{<:Integer}, distmx::AbstractMatrix{U}=weights(g) ) where {U<:Real,T,AG<:AbstractGraph{T}} nvg = nv(g) + length(term_vert) == 0 && return SimpleGraph{T}() + length(term_vert) == 1 && return SimpleGraph{T}(first(term_vert)) term_to_actual = T.(term_vert) unique!(term_to_actual) diff --git a/test/spanningtrees/kruskal.jl b/test/spanningtrees/kruskal.jl index b45eb8cb6..ddf8fead8 100644 --- a/test/spanningtrees/kruskal.jl +++ b/test/spanningtrees/kruskal.jl @@ -44,4 +44,13 @@ @test sort([(src(e), dst(e)) for e in mst2]) == sort([(src(e), dst(e)) for e in vec2]) @test sort([(src(e), dst(e)) for e in max_mst2]) == sort([(src(e), dst(e)) for e in max_vec2]) end + + # non regression test for #362 + g = Graph() + mst = @inferred(kruskal_mst(g)) + @test isempty(mst) + + g = Graph(1) + mst = @inferred(kruskal_mst(g)) + @test isempty(mst) end diff --git a/test/steinertree/steiner_tree.jl b/test/steinertree/steiner_tree.jl index c5ee13b90..35bd323dd 100644 --- a/test/steinertree/steiner_tree.jl +++ b/test/steinertree/steiner_tree.jl @@ -19,6 +19,10 @@ g_copy = SimpleGraph(g) Graphs.filter_non_term_leaves!(g_copy, [2, 5]) @test ne(g_copy) == 2 # [Edge(2, 1), Edge(1, 5)] + + # non regression test for #362 + g_st = @inferred(steiner_tree(g, [2])) + @test ne(g_st) == 0 end g4 = path_graph(11)