Skip to content

Commit 7d5e1a5

Browse files
committed
Respond to PR comments.
1 parent 0279098 commit 7d5e1a5

3 files changed

Lines changed: 26 additions & 30 deletions

File tree

ext/SparseMatrixColoringsCliqueTreesExt.jl

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,22 @@
11
module SparseMatrixColoringsCliqueTreesExt
22

3-
using CliqueTrees: CliqueTrees
4-
using SparseArrays
5-
using SparseMatrixColorings:
6-
SparseMatrixColorings, AdjacencyGraph, BipartiteGraph, PerfectEliminationOrder, pattern
3+
import CliqueTrees: permutation, LexBFS, MCS
4+
import SparseArrays: SparseMatrixCSC, rowvals, nnz
5+
import SparseMatrixColorings:
6+
AdjacencyGraph, BipartiteGraph, PerfectEliminationOrder, pattern, vertices
77

8-
function SparseMatrixColorings.vertices(
9-
g::AdjacencyGraph{T}, order::PerfectEliminationOrder
10-
) where {T}
8+
function vertices(g::AdjacencyGraph{T}, order::PerfectEliminationOrder) where {T}
119
S = pattern(g)
1210

1311
# construct matrix with sparsity pattern S
1412
M = SparseMatrixCSC{Bool,T}(size(S)..., S.colptr, rowvals(S), ones(Bool, nnz(S)))
1513

16-
# can also use alg=CliqueTrees.LexBFS()
17-
order, _ = CliqueTrees.permutation(M; alg=CliqueTrees.MCS())
18-
19-
return reverse!(order)
20-
end
21-
22-
function SparseMatrixColorings.vertices(
23-
bg::BipartiteGraph{T}, ::Val{side}, order::PerfectEliminationOrder
24-
) where {T,side}
25-
S = pattern(bg, Val(side))
26-
27-
# construct matrix with sparsity pattern S
28-
M = SparseMatrixCSC{Bool,T}(size(S)..., S.colptr, rowvals(S), ones(Bool, nnz(S)))
29-
30-
# can also use alg=CliqueTrees.LexBFS()
31-
order, _ = CliqueTrees.permutation(M; alg=CliqueTrees.MCS())
14+
# construct a perfect elimination order
15+
# 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())
3220

3321
return reverse!(order)
3422
end

src/order.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,10 +305,10 @@ const DynamicLargestFirst = DynamicDegreeBasedOrder{:forward,:low2high}
305305
"""
306306
PerfectEliminationOrder
307307
308-
Instance of [`AbstractOrder`](@ref) which sorts the vertices of a chordal graph in a perfect elimination order.
308+
A linear-time ordering code for symmetric graphs. On [chordal graphs](https://en.wikipedia.org/wiki/Chordal_graph), the code computes a perfect elimination ordering. Otherwise, it computes a suboptimal ordering.
309309
310310
!!! danger
311-
This order is implemented as a package extension and requires loading CliqueTrees.jl.
311+
This order is implemented as a package extension and requires loading [CliqueTrees.jl](https://github.com/AlgebraicJulia/CliqueTrees.jl).
312312
313313
# References
314314

test/order.jl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,24 @@ end;
115115
end;
116116

117117
@testset "PerfectEliminationOrder" begin
118-
problem1 = ColoringProblem(; structure=:symmetric, partition=:column)
119-
problem2 = ColoringProblem(; structure=:nonsymmetric, partition=:column)
118+
problem = ColoringProblem(; structure=:symmetric, partition=:column)
120119
algorithm = GreedyColoringAlgorithm(
121120
PerfectEliminationOrder(); decompression=:substitution
122121
)
123122

123+
# band graphs
124124
for (n, m) in ((800, 80), (400, 40), (200, 20), (100, 10))
125-
perm = randperm(n)
125+
perm = randperm(rng, n)
126126
matrix = permute!(sparse(Symmetric(brand(n, n, m, 0), :L)), perm, perm)
127-
@test ncolors(coloring(matrix, problem1, algorithm)) == 1m + 1
128-
@test ncolors(coloring(matrix, problem2, algorithm)) == 2m + 1
127+
π = vertices(AdjacencyGraph(matrix), PerfectEliminationOrder())
128+
@test isperm(π)
129+
@test ncolors(coloring(matrix, problem, algorithm)) == m + 1
130+
end
131+
132+
# random graphs
133+
for (n, p) in Iterators.product(20:20:100, 0.0:0.1:0.2)
134+
matrix = sparse(Symmetric(sprand(rng, Bool, n, n, p)))
135+
π = vertices(AdjacencyGraph(matrix), PerfectEliminationOrder())
136+
@test isperm(π)
129137
end
130138
end

0 commit comments

Comments
 (0)