Skip to content

Commit f937166

Browse files
committed
Add chordal coloring.
1 parent 3cdcf96 commit f937166

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ version = "0.4.14"
55

66
[deps]
77
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
8+
CliqueTrees = "60701a23-6482-424a-84db-faee86b9b1f8"
89
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
910
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
1011
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
@@ -19,6 +20,7 @@ SparseMatrixColoringsColorsExt = "Colors"
1920

2021
[compat]
2122
ADTypes = "1.2.1"
23+
CliqueTrees = "0.5.2"
2224
Colors = "0.12.11, 0.13"
2325
DataStructures = "0.18"
2426
DocStringExtensions = "0.8,0.9"

src/SparseMatrixColorings.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module SparseMatrixColorings
1111

1212
using ADTypes: ADTypes
1313
using Base.Iterators: Iterators
14+
using CliqueTrees: CliqueTrees
1415
using DataStructures: DisjointSets, find_root!, root_union!, num_groups
1516
using DocStringExtensions: README, EXPORTS, SIGNATURES, TYPEDEF, TYPEDFIELDS
1617
using LinearAlgebra:
@@ -49,6 +50,7 @@ include("result.jl")
4950
include("matrices.jl")
5051
include("interface.jl")
5152
include("constant.jl")
53+
include("chordal.jl")
5254
include("adtypes.jl")
5355
include("decompression.jl")
5456
include("check.jl")
@@ -58,7 +60,7 @@ include("show_colors.jl")
5860
export NaturalOrder, RandomOrder, LargestFirst
5961
export DynamicDegreeBasedOrder, SmallestLast, IncidenceDegree, DynamicLargestFirst
6062
export ColoringProblem, GreedyColoringAlgorithm, AbstractColoringResult
61-
export ConstantColoringAlgorithm
63+
export ConstantColoringAlgorithm, ChordalColoringAlgorithm
6264
export coloring, fast_coloring
6365
export column_colors, row_colors, ncolors
6466
export column_groups, row_groups

src/chordal.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
struct ChordalColoringAlgorithm <: ADTypes.AbstractColoringAlgorithm end
2+
3+
struct ChordalColoringResult{M <: AbstractMatrix, V} <: AbstractColoringResult{:symmetric, :column, :direct}
4+
A::M
5+
color::Vector{Int}
6+
group::V
7+
end
8+
9+
function coloring(
10+
A::AbstractMatrix,
11+
::ColoringProblem{:symmetric,:column},
12+
algo::ChordalColoringAlgorithm,
13+
)
14+
# compute an elimination ordering using
15+
# the maximum cardinality search algorithm
16+
perm, invp = CliqueTrees.permutation(A; alg=CliqueTrees.MCS())
17+
18+
# if the ordering is not perfect, then the graph is not chordal
19+
if !CliqueTrees.isperfect(A, perm, invp)
20+
error("Matrix does not have a chordal sparsity pattern.")
21+
end
22+
23+
# if the graph is chordal, then find a minimal vertex coloring
24+
color = CliqueTrees.color(A, invp).colors
25+
26+
# compute groups and return result
27+
group = group_by_color(color)
28+
return ChordalColoringResult(A, color, group)
29+
end

0 commit comments

Comments
 (0)