Skip to content

Commit 7a5d6fe

Browse files
committed
Better test
1 parent 77b920a commit 7a5d6fe

2 files changed

Lines changed: 114 additions & 12 deletions

File tree

src/interface.jl

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -320,18 +320,21 @@ function _coloring(
320320
) where {R}
321321
A_and_Aᵀ, edge_to_index = bidirectional_pattern(A; symmetric_pattern)
322322
ag = AdjacencyGraph(A_and_Aᵀ, edge_to_index; has_diagonal=false)
323-
color_and_star_set_by_order = map(algo.orders) do order
323+
outputs_by_order = map(algo.orders) do order
324324
vertices_in_order = vertices(ag, order)
325-
return star_coloring(ag, vertices_in_order, algo.postprocessing)
325+
color, star_set = star_coloring(ag, vertices_in_order, algo.postprocessing)
326+
row_color, column_color, _ = remap_colors(
327+
eltype(ag), color, maximum(color), size(A)...
328+
)
329+
return (; color, star_set, row_color, column_color)
326330
end
327-
color, star_set = argmin(maximum first, color_and_star_set_by_order)
331+
(; color, star_set, row_color, column_color) = argmin(
332+
t -> maximum(t.row_color) + maximum(t.column_color), outputs_by_order
333+
) # can't use ncolors without computing the full result
328334
if speed_setting isa WithResult
329335
symmetric_result = StarSetColoringResult(A_and_Aᵀ, ag, color, star_set)
330336
return BicoloringResult(A, ag, symmetric_result, R)
331337
else
332-
row_color, column_color, _ = remap_colors(
333-
eltype(ag), color, maximum(color), size(A)...
334-
)
335338
return row_color, column_color
336339
end
337340
end
@@ -346,18 +349,21 @@ function _coloring(
346349
) where {R}
347350
A_and_Aᵀ, edge_to_index = bidirectional_pattern(A; symmetric_pattern)
348351
ag = AdjacencyGraph(A_and_Aᵀ, edge_to_index; has_diagonal=false)
349-
color_and_tree_set_by_order = map(algo.orders) do order
352+
outputs_by_order = map(algo.orders) do order
350353
vertices_in_order = vertices(ag, order)
351-
return acyclic_coloring(ag, vertices_in_order, algo.postprocessing)
354+
color, tree_set = acyclic_coloring(ag, vertices_in_order, algo.postprocessing)
355+
row_color, column_color, _ = remap_colors(
356+
eltype(ag), color, maximum(color), size(A)...
357+
)
358+
return (; color, tree_set, row_color, column_color)
352359
end
353-
color, tree_set = argmin(maximum first, color_and_tree_set_by_order)
360+
(; color, tree_set, row_color, column_color) = argmin(
361+
t -> maximum(t.row_color) + maximum(t.column_color), outputs_by_order
362+
) # can't use ncolors without computing the full result
354363
if speed_setting isa WithResult
355364
symmetric_result = TreeSetColoringResult(A_and_Aᵀ, ag, color, tree_set, R)
356365
return BicoloringResult(A, ag, symmetric_result, R)
357366
else
358-
row_color, column_color, _ = remap_colors(
359-
eltype(ag), color, maximum(color), size(A)...
360-
)
361367
return row_color, column_color
362368
end
363369
end

test/order.jl

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,99 @@ end;
146146
@test isperm(π)
147147
end
148148
end
149+
150+
@testset "Multiple orders" begin
151+
# I used brute force to find examples where LargestFirst is *strictly* better than NaturalOrder, just to check that the best order is indeed selected when multiple orders are provided
152+
@testset "Column coloring" begin
153+
A = [
154+
0 0 1 1
155+
0 1 0 1
156+
0 0 1 1
157+
1 1 0 0
158+
]
159+
problem = ColoringProblem{:nonsymmetric,:column}()
160+
algo = GreedyColoringAlgorithm(NaturalOrder())
161+
better_algo = GreedyColoringAlgorithm((NaturalOrder(), LargestFirst()))
162+
@test ncolors(coloring(A, problem, better_algo)) <
163+
ncolors(coloring(A, problem, algo))
164+
end
165+
@testset "Row coloring" begin
166+
A = [
167+
1 0 0 0
168+
0 0 1 0
169+
0 1 1 1
170+
1 0 0 1
171+
]
172+
problem = ColoringProblem{:nonsymmetric,:row}()
173+
algo = GreedyColoringAlgorithm(NaturalOrder())
174+
better_algo = GreedyColoringAlgorithm((NaturalOrder(), LargestFirst()))
175+
@test ncolors(coloring(A, problem, better_algo)) <
176+
ncolors(coloring(A, problem, algo))
177+
end
178+
@testset "Star coloring" begin
179+
A = [
180+
0 1 0 1 1
181+
1 1 0 1 0
182+
0 0 1 0 1
183+
1 1 0 1 0
184+
1 0 1 0 0
185+
]
186+
problem = ColoringProblem{:symmetric,:column}()
187+
algo = GreedyColoringAlgorithm(NaturalOrder())
188+
better_algo = GreedyColoringAlgorithm((NaturalOrder(), LargestFirst()))
189+
@test ncolors(coloring(A, problem, better_algo)) <
190+
ncolors(coloring(A, problem, algo))
191+
end
192+
@testset "Acyclic coloring" begin
193+
A = [
194+
1 0 0 0 0 1 0
195+
0 0 0 1 0 0 0
196+
0 0 0 1 0 0 0
197+
0 1 1 1 0 1 1
198+
0 0 0 0 0 0 1
199+
1 0 0 1 0 0 1
200+
0 0 0 1 1 1 1
201+
]
202+
problem = ColoringProblem{:symmetric,:column}()
203+
algo = GreedyColoringAlgorithm{:substitution}(NaturalOrder())
204+
better_algo = GreedyColoringAlgorithm{:substitution}((
205+
NaturalOrder(), LargestFirst()
206+
))
207+
@test ncolors(coloring(A, problem, better_algo)) <
208+
ncolors(coloring(A, problem, algo))
209+
end
210+
@testset "Star bicoloring" begin
211+
A = [
212+
0 1 0 0 0
213+
1 0 1 0 0
214+
0 1 0 0 1
215+
0 0 0 0 0
216+
0 0 1 0 1
217+
]
218+
problem = ColoringProblem{:nonsymmetric,:bidirectional}()
219+
algo = GreedyColoringAlgorithm(NaturalOrder())
220+
better_algo = GreedyColoringAlgorithm((NaturalOrder(), LargestFirst()))
221+
@test ncolors(coloring(A, problem, better_algo)) <
222+
ncolors(coloring(A, problem, algo))
223+
end
224+
@testset "Acyclic bicoloring" begin
225+
A = [
226+
0 1 0 1 1 0 1 0 1
227+
1 0 0 0 0 0 0 0 1
228+
0 0 0 0 0 0 0 0 0
229+
1 0 0 1 1 0 1 0 0
230+
1 0 0 1 0 0 0 0 0
231+
0 0 0 0 0 0 0 0 0
232+
1 0 0 1 0 0 0 0 0
233+
0 0 0 0 0 0 0 0 0
234+
1 1 0 0 0 0 0 0 0
235+
]
236+
problem = ColoringProblem{:nonsymmetric,:bidirectional}()
237+
algo = GreedyColoringAlgorithm{:substitution}(NaturalOrder())
238+
better_algo = GreedyColoringAlgorithm{:substitution}((
239+
NaturalOrder(), LargestFirst()
240+
))
241+
@test ncolors(coloring(A, problem, better_algo)) <
242+
ncolors(coloring(A, problem, algo))
243+
end
244+
end

0 commit comments

Comments
 (0)