diff --git a/src/interface.jl b/src/interface.jl index 936b974c..0d183c9c 100644 --- a/src/interface.jl +++ b/src/interface.jl @@ -305,7 +305,13 @@ function _coloring( vertices_in_order = vertices(ag, order) return acyclic_coloring(ag, vertices_in_order, algo.postprocessing) end - color, tree_set = argmin(maximum ∘ first, color_and_tree_set_by_order) + # if `color` is empty, `maximum` will fail but `color_and_tree_set_by_order` + # is also one so we can just add a special case for this + if length(color_and_tree_set_by_order) == 1 + color, tree_set = only(color_and_tree_set_by_order) + else + color, tree_set = argmin(maximum ∘ first, color_and_tree_set_by_order) + end if speed_setting isa WithResult return TreeSetColoringResult(A, ag, color, tree_set, R) else diff --git a/src/result.jl b/src/result.jl index c74ceaa1..0d47cf13 100644 --- a/src/result.jl +++ b/src/result.jl @@ -82,6 +82,9 @@ Create a color-indexed vector `group` such that `i ∈ group[c]` iff `color[i] = Assumes the colors are contiguously numbered from `0` to some `cmax`. """ function group_by_color(::Type{T}, color::AbstractVector) where {T<:Integer} + if isempty(color) + return typeof(view(T[], 1:0))[] + end cmin, cmax = extrema(color) @assert cmin >= 0 # Compute group sizes and offsets for a joint storage diff --git a/test/check.jl b/test/check.jl index b2701095..246261d0 100644 --- a/test/check.jl +++ b/test/check.jl @@ -1,4 +1,5 @@ using LinearAlgebra +using SparseArrays using SparseMatrixColorings: structurally_orthogonal_columns, symmetrically_orthogonal_columns, @@ -308,3 +309,13 @@ end log = (:warn, "4 colors provided for 3 rows.") @test_logs log !substitutable_bidirectional(A, B, [1, 0, 0, 1], [0, 1, 1]; verbose=true) end + +# See https://github.com/gdalle/SparseMatrixColorings.jl/pull/300 +@testset "Empty matrix" begin + problem = ColoringProblem(; structure=:symmetric, partition=:column) + algo = GreedyColoringAlgorithm(; decompression=:substitution) + S = spzeros(Int, 0, 0) + result = coloring(S, problem, algo) + @test isempty(result.color) + @test isempty(result.group) +end