Skip to content

Commit baf5240

Browse files
authored
More correctness tests for symmetric coloring (#74)
* Add tests for symmetric coloring * Typo
1 parent c3acf52 commit baf5240

5 files changed

Lines changed: 77 additions & 1 deletion

File tree

src/graph.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ vertices(g::Graph) = 1:length(g)
2424
neighbors(g::Graph, v::Integer) = view(g.rowval, g.colptr[v]:(g.colptr[v + 1] - 1))
2525
degree(g::Graph, v::Integer) = length(g.colptr[v]:(g.colptr[v + 1] - 1))
2626

27+
maximum_degree(g::Graph) = maximum(Base.Fix1(degree, g), vertices(g))
28+
minimum_degree(g::Graph) = minimum(Base.Fix1(degree, g), vertices(g))
29+
2730
## Bipartite graph
2831

2932
"""
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
row,modified,group,name,V,E,Δ,δ,δbar,KD2,KS1,KS2
2+
1,false,,mrng1,257000,505048,4,2,3,12,8,10
3+
2,false,,mrng2,1017253,2015714,4,2,3,12,9,10
4+
3,false,DIMACS10,598a,110971,741934,26,5,13,38,27,32
5+
4,false,DIMACS10,144,144649,1074393,26,4,14,41,28,35
6+
5,false,DIMACS10,m14b,214765,1679018,40,4,15,42,29,34
7+
6,false,DIMACS10,auto,448695,3314611,37,4,14,42,29,36

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ include("utils.jl")
4747
@testset "Random instances" begin
4848
include("random.jl")
4949
end
50+
@testset "Instances with known colorings" begin
51+
include("theory.jl")
52+
end
5053
end
5154
@testset verbose = true "Performance" begin
5255
if VERSION >= v"1.10"

test/suitesparse.jl

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using ADTypes: column_coloring, row_coloring, symmetric_coloring
21
using CSV
32
using DataFrames
43
using LinearAlgebra
@@ -79,3 +78,24 @@ what_table_31_32 = CSV.read(
7978
end;
8079

8180
## Star coloring
81+
82+
what_table_41_42 = CSV.read(
83+
joinpath(@__DIR__, "reference", "what_table_41_42.csv"), DataFrame
84+
)
85+
86+
@testset "Star coloring (survey paper)" begin
87+
@testset "$(row[:name])" for row in eachrow(what_table_41_42)
88+
ismissing(row[:group]) && continue
89+
@info "Testing star coloring for $(row[:name]) against survey paper"
90+
original_mat = matrixdepot("$(row[:group])/$(row[:name])")
91+
mat = dropzeros(sparse(original_mat))
92+
ag = adjacency_graph(mat)
93+
bg = bipartite_graph(mat)
94+
@test length(ag) == row[:V]
95+
@test nnz(ag) ÷ 2 == row[:E]
96+
@test maximum_degree(ag) == row[]
97+
@test minimum_degree(ag) == row[]
98+
color_N, _ = star_coloring(ag, NaturalOrder())
99+
@test row[:KS1] <= length(unique(color_N)) <= row[:KS2] # TODO: find better
100+
end
101+
end;

test/theory.jl

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using SparseArrays
2+
using SparseMatrixColorings
3+
using Test
4+
5+
## Banded
6+
7+
#=
8+
Per the recovery paper "Efficient computation of sparse Hessians using Coloring and Automatic Differentiation", for a symmetric banded matrix with ρ non-diagonal bands:
9+
- acyclic coloring uses ⌊ρ/2⌋+1 colors
10+
- star coloring uses 2⌊ρ/2⌋+1 colors
11+
=#
12+
13+
problem = ColoringProblem(; structure=:symmetric, partition=:column)
14+
15+
function banded_matrix(n::Integer, ρ::Integer)
16+
return spdiagm([k => ones(Bool, n - abs(k)) for k in (-÷ 2)):÷ 2)]...)
17+
end
18+
19+
@testset "Star coloring" begin
20+
algo = GreedyColoringAlgorithm(; decompression=:direct)
21+
for n in [5, 10, 20] .* 1000
22+
S = banded_matrix(n, 10)
23+
direct_result = coloring(S, problem, algo)
24+
@test length(column_groups(direct_result)) == 11
25+
26+
S = banded_matrix(n, 20)
27+
direct_result = coloring(S, problem, algo)
28+
@test length(column_groups(direct_result)) == 21
29+
end
30+
end
31+
32+
@testset "Acyclic coloring" begin
33+
algo = GreedyColoringAlgorithm(; decompression=:substitution)
34+
for n in [5, 10, 20] .* 1000
35+
S = banded_matrix(n, 10)
36+
substitution_result = coloring(S, problem, algo)
37+
@test length(column_groups(substitution_result)) == 6
38+
39+
S = banded_matrix(n, 20)
40+
substitution_result = coloring(S, problem, algo)
41+
@test length(column_groups(substitution_result)) == 11
42+
end
43+
end

0 commit comments

Comments
 (0)