Skip to content

Commit 4789944

Browse files
blegatgdalle
andauthored
Fix coloring with empty matrix as input (#300)
* Fix coloring with empty matrix as input * Update src/result.jl Co-authored-by: Guillaume Dalle <22795598+gdalle@users.noreply.github.com> * Add check * Fix format * Change check * Apply suggestion from @gdalle --------- Co-authored-by: Guillaume Dalle <22795598+gdalle@users.noreply.github.com>
1 parent a7cb154 commit 4789944

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

src/interface.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,13 @@ function _coloring(
305305
vertices_in_order = vertices(ag, order)
306306
return acyclic_coloring(ag, vertices_in_order, algo.postprocessing)
307307
end
308-
color, tree_set = argmin(maximum first, color_and_tree_set_by_order)
308+
# if `color` is empty, `maximum` will fail but `color_and_tree_set_by_order`
309+
# is also one so we can just add a special case for this
310+
if length(color_and_tree_set_by_order) == 1
311+
color, tree_set = only(color_and_tree_set_by_order)
312+
else
313+
color, tree_set = argmin(maximum first, color_and_tree_set_by_order)
314+
end
309315
if speed_setting isa WithResult
310316
return TreeSetColoringResult(A, ag, color, tree_set, R)
311317
else

src/result.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ Create a color-indexed vector `group` such that `i ∈ group[c]` iff `color[i] =
8282
Assumes the colors are contiguously numbered from `0` to some `cmax`.
8383
"""
8484
function group_by_color(::Type{T}, color::AbstractVector) where {T<:Integer}
85+
if isempty(color)
86+
return typeof(view(T[], 1:0))[]
87+
end
8588
cmin, cmax = extrema(color)
8689
@assert cmin >= 0
8790
# Compute group sizes and offsets for a joint storage

test/check.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using LinearAlgebra
2+
using SparseArrays
23
using SparseMatrixColorings:
34
structurally_orthogonal_columns,
45
symmetrically_orthogonal_columns,
@@ -308,3 +309,13 @@ end
308309
log = (:warn, "4 colors provided for 3 rows.")
309310
@test_logs log !substitutable_bidirectional(A, B, [1, 0, 0, 1], [0, 1, 1]; verbose=true)
310311
end
312+
313+
# See https://github.com/gdalle/SparseMatrixColorings.jl/pull/300
314+
@testset "Empty matrix" begin
315+
problem = ColoringProblem(; structure=:symmetric, partition=:column)
316+
algo = GreedyColoringAlgorithm(; decompression=:substitution)
317+
S = spzeros(Int, 0, 0)
318+
result = coloring(S, problem, algo)
319+
@test isempty(result.color)
320+
@test isempty(result.group)
321+
end

0 commit comments

Comments
 (0)