Skip to content

Commit 13300d2

Browse files
committed
Make elimination algorithm parametric
1 parent 226d8d8 commit 13300d2

3 files changed

Lines changed: 16 additions & 10 deletions

File tree

ext/SparseMatrixColoringsCliqueTreesExt.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
module SparseMatrixColoringsCliqueTreesExt
22

3-
import CliqueTrees: permutation, LexBFS, MCS
3+
import CliqueTrees: permutation, EliminationAlgorithm, MCS
44
import SparseArrays: SparseMatrixCSC, rowvals, nnz
55
import SparseMatrixColorings:
66
AdjacencyGraph, BipartiteGraph, PerfectEliminationOrder, pattern, vertices
77

8+
PerfectEliminationOrder() = PerfectEliminationOrder(MCS())
9+
810
function vertices(g::AdjacencyGraph{T}, order::PerfectEliminationOrder) where {T}
911
S = pattern(g)
1012

@@ -13,10 +15,7 @@ function vertices(g::AdjacencyGraph{T}, order::PerfectEliminationOrder) where {T
1315

1416
# construct a perfect elimination order
1517
# self-loops are ignored
16-
# we can also use alg=LexBFS()
17-
# - time complexity: O(|V| + |E|)
18-
# - space complexity: O(|V| + |E|)
19-
order, _ = permutation(M; alg=MCS())
18+
order, _ = permutation(M; alg=order.elimination_algorithm)
2019

2120
return reverse!(order)
2221
end

src/order.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Depending on how the vertices are ordered, the number of colors necessary may va
1414
- [`IncidenceDegree`](@ref) (experimental)
1515
- [`SmallestLast`](@ref) (experimental)
1616
- [`DynamicLargestFirst`](@ref) (experimental)
17+
- [`PerfectEliminationOrder`](@ref)
1718
"""
1819
abstract type AbstractOrder end
1920

@@ -303,10 +304,12 @@ Instance of [`AbstractOrder`](@ref) which sorts vertices from lowest to highest
303304
const DynamicLargestFirst = DynamicDegreeBasedOrder{:forward,:low2high}
304305

305306
"""
306-
PerfectEliminationOrder
307+
PerfectEliminationOrder(elimination_algorithm=CliqueTrees.MCS())
307308
308309
Instance of [`AbstractOrder`](@ref) which computes a perfect elimination ordering when the underlying graph is [chordal](https://en.wikipedia.org/wiki/Chordal_graph). For non-chordal graphs, it computes a suboptimal ordering.
309310
311+
The `elimination_algorithm` must be an instance of `CliqueTrees.EliminationAlgorithm`.
312+
310313
!!! warning
311314
This order can only be applied for symmetric or bidirectional coloring problems.
312315
@@ -317,4 +320,6 @@ Instance of [`AbstractOrder`](@ref) which computes a perfect elimination orderin
317320
318321
- [Simple Linear-Time Algorithms to Test Chordality of Graphs, Test Acyclicity of Hypergraphs, and Selectively Reduce Acyclic Hypergraphs](https://epubs.siam.org/doi/10.1137/0213035), Tarjan and Yannakakis (1984)
319322
"""
320-
struct PerfectEliminationOrder <: AbstractOrder end
323+
struct PerfectEliminationOrder{E} <: AbstractOrder
324+
elimination_algorithm::E
325+
end

test/order.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,9 @@ end;
116116

117117
@testset "PerfectEliminationOrder" begin
118118
problem = ColoringProblem(; structure=:symmetric, partition=:column)
119-
algorithm = GreedyColoringAlgorithm(
120-
PerfectEliminationOrder(); decompression=:substitution
119+
direct_algo = GreedyColoringAlgorithm(PerfectEliminationOrder(); decompression=:direct)
120+
substitution_algo = GreedyColoringAlgorithm(
121+
PerfectEliminationOrder(CliqueTrees.LexBFS()); decompression=:substitution
121122
)
122123

123124
# band graphs
@@ -126,7 +127,8 @@ end;
126127
matrix = permute!(sparse(Symmetric(brand(n, n, m, 0), :L)), perm, perm)
127128
π = vertices(AdjacencyGraph(matrix), PerfectEliminationOrder())
128129
@test isperm(π)
129-
@test ncolors(coloring(matrix, problem, algorithm)) == m + 1
130+
@test ncolors(coloring(matrix, problem, direct_algo)) == 2m + 1
131+
@test ncolors(coloring(matrix, problem, substitution_algo)) == m + 1
130132
end
131133

132134
# random graphs

0 commit comments

Comments
 (0)