Skip to content

Commit 299b6da

Browse files
committed
Rename has_diagonal into augmented_graph
1 parent 33a1ef1 commit 299b6da

File tree

9 files changed

+45
-47
lines changed

9 files changed

+45
-47
lines changed

src/coloring.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ function star_coloring(
124124

125125
for v in vertices_in_order
126126
for (w, index_vw) in neighbors_with_edge_indices(g, v)
127-
!has_diagonal(g) || (v == w && continue)
127+
augmented_graph(g) || (v == w && continue)
128128
color_w = color[w]
129129
iszero(color_w) && continue
130130
forbidden_colors[color_w] = v
@@ -139,7 +139,7 @@ function star_coloring(
139139
else
140140
first_neighbor[color[w]] = (v, w, index_vw)
141141
for (x, index_wx) in neighbors_with_edge_indices(g, w)
142-
!has_diagonal(g) || (w == x && continue)
142+
augmented_graph(g) || (w == x && continue)
143143
color_x = color[x]
144144
(x == v || iszero(color_x)) && continue
145145
if x == hub[star[index_wx]] # potential Case 2 (which is always false for trivial stars with two vertices, since the associated hub is negative)
@@ -184,7 +184,7 @@ function _treat!(
184184
color::AbstractVector{<:Integer},
185185
)
186186
for x in neighbors(g, w)
187-
!has_diagonal(g) || (w == x && continue)
187+
augmented_graph(g) || (w == x && continue)
188188
color_x = color[x]
189189
iszero(color_x) && continue
190190
forbidden_colors[color_x] = v
@@ -204,12 +204,12 @@ function _update_stars!(
204204
first_neighbor::AbstractVector{<:Tuple},
205205
)
206206
for (w, index_vw) in neighbors_with_edge_indices(g, v)
207-
!has_diagonal(g) || (v == w && continue)
207+
augmented_graph(g) || (v == w && continue)
208208
color_w = color[w]
209209
iszero(color_w) && continue
210210
x_exists = false
211211
for (x, index_wx) in neighbors_with_edge_indices(g, w)
212-
!has_diagonal(g) || (w == x && continue)
212+
augmented_graph(g) || (w == x && continue)
213213
if x != v && color[x] == color[v] # vw, wx ∈ E
214214
star_wx = star[index_wx]
215215
hub[star_wx] = w # this may already be true
@@ -286,16 +286,16 @@ function acyclic_coloring(
286286

287287
for v in vertices_in_order
288288
for w in neighbors(g, v)
289-
!has_diagonal(g) || (v == w && continue)
289+
augmented_graph(g) || (v == w && continue)
290290
color_w = color[w]
291291
iszero(color_w) && continue
292292
forbidden_colors[color_w] = v
293293
end
294294
for w in neighbors(g, v)
295-
!has_diagonal(g) || (v == w && continue)
295+
augmented_graph(g) || (v == w && continue)
296296
iszero(color[w]) && continue
297297
for (x, index_wx) in neighbors_with_edge_indices(g, w)
298-
!has_diagonal(g) || (w == x && continue)
298+
augmented_graph(g) || (w == x && continue)
299299
color_x = color[x]
300300
iszero(color_x) && continue
301301
if forbidden_colors[color_x] != v
@@ -320,16 +320,16 @@ function acyclic_coloring(
320320
end
321321
end
322322
for (w, index_vw) in neighbors_with_edge_indices(g, v) # grow two-colored stars around the vertex v
323-
!has_diagonal(g) || (v == w && continue)
323+
augmented_graph(g) || (v == w && continue)
324324
color_w = color[w]
325325
iszero(color_w) && continue
326326
_grow_star!(v, w, index_vw, color_w, first_neighbor, forest)
327327
end
328328
for (w, index_vw) in neighbors_with_edge_indices(g, v)
329-
!has_diagonal(g) || (v == w && continue)
329+
augmented_graph(g) || (v == w && continue)
330330
iszero(color[w]) && continue
331331
for (x, index_wx) in neighbors_with_edge_indices(g, w)
332-
!has_diagonal(g) || (w == x && continue)
332+
augmented_graph(g) || (w == x && continue)
333333
color_x = color[x]
334334
(x == v || iszero(color_x)) && continue
335335
if color_x == color[v]

src/decompression.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ function decompress!(
547547
end
548548

549549
# Recover the diagonal coefficients of A
550-
if has_diagonal(ag)
550+
if !augmented_graph(ag)
551551
for i in axes(S, 1)
552552
if !iszero(S[i, i])
553553
A[i, i] = B[i, color[i]]
@@ -616,7 +616,7 @@ function decompress!(
616616
end
617617

618618
# Recover the diagonal coefficients of A
619-
if has_diagonal(ag)
619+
if !augmented_graph(ag)
620620
if uplo == :L
621621
for i in diagonal_indices
622622
# A[i, i] is the first element in column i

src/graph.jl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ end
202202
## Adjacency graph
203203

204204
"""
205-
AdjacencyGraph{T,has_diagonal}
205+
AdjacencyGraph{T,augmented_graph}
206206
207207
Undirected graph without self-loops representing the nonzeros of a symmetric matrix (typically a Hessian matrix).
208208
@@ -213,18 +213,18 @@ The adjacency graph of a symmetric matrix `A ∈ ℝ^{n × n}` is `G(A) = (V, E)
213213
214214
# Constructors
215215
216-
AdjacencyGraph(A::SparseMatrixCSC; has_diagonal::Bool=true)
216+
AdjacencyGraph(A::SparseMatrixCSC; augmented_graph::Bool=false)
217217
218218
# Fields
219219
220-
- `S::SparsityPatternCSC{T}`: Underlying sparsity pattern, whose diagonal is empty whenever `has_diagonal` is `false`
220+
- `S::SparsityPatternCSC{T}`: Underlying sparsity pattern, which represents an augmented graph whenever `augmented_graph` is `true`.
221221
- `edge_to_index::Vector{T}`: A vector mapping each nonzero of `S` to a unique edge index (ignoring diagonal and accounting for symmetry, so that `(i, j)` and `(j, i)` get the same index)
222222
223223
# References
224224
225225
> [_What Color Is Your Jacobian? SparsityPatternCSC Coloring for Computing Derivatives_](https://epubs.siam.org/doi/10.1137/S0036144504444711), Gebremedhin et al. (2005)
226226
"""
227-
struct AdjacencyGraph{T<:Integer,has_diagonal}
227+
struct AdjacencyGraph{T<:Integer,augmented_graph}
228228
S::SparsityPatternCSC{T}
229229
edge_to_index::Vector{T}
230230
end
@@ -234,24 +234,24 @@ Base.eltype(::AdjacencyGraph{T}) where {T} = T
234234
function AdjacencyGraph(
235235
S::SparsityPatternCSC{T},
236236
edge_to_index::Vector{T}=build_edge_to_index(S);
237-
has_diagonal::Bool=true,
237+
augmented_graph::Bool=false,
238238
) where {T}
239-
return AdjacencyGraph{T,has_diagonal}(S, edge_to_index)
239+
return AdjacencyGraph{T,augmented_graph}(S, edge_to_index)
240240
end
241241

242-
function AdjacencyGraph(A::SparseMatrixCSC; has_diagonal::Bool=true)
243-
return AdjacencyGraph(SparsityPatternCSC(A); has_diagonal)
242+
function AdjacencyGraph(A::SparseMatrixCSC; augmented_graph::Bool=false)
243+
return AdjacencyGraph(SparsityPatternCSC(A); augmented_graph)
244244
end
245245

246-
function AdjacencyGraph(A::AbstractMatrix; has_diagonal::Bool=true)
247-
return AdjacencyGraph(SparseMatrixCSC(A); has_diagonal)
246+
function AdjacencyGraph(A::AbstractMatrix; augmented_graph::Bool=false)
247+
return AdjacencyGraph(SparseMatrixCSC(A); augmented_graph)
248248
end
249249

250250
pattern(g::AdjacencyGraph) = g.S
251251
edge_indices(g::AdjacencyGraph) = g.edge_to_index
252252
nb_vertices(g::AdjacencyGraph) = pattern(g).n
253253
vertices(g::AdjacencyGraph) = 1:nb_vertices(g)
254-
has_diagonal(::AdjacencyGraph{T,hd}) where {T,hd} = hd
254+
augmented_graph(::AdjacencyGraph{T,ag}) where {T,ag} = ag
255255

256256
function neighbors(g::AdjacencyGraph, v::Integer)
257257
S = pattern(g)
@@ -267,17 +267,17 @@ function neighbors_with_edge_indices(g::AdjacencyGraph, v::Integer)
267267
return zip(neighbors_v, edges_indices_v)
268268
end
269269

270-
degree(g::AdjacencyGraph{T,false}, v::Integer) where {T} = g.S.colptr[v + 1] - g.S.colptr[v]
270+
degree(g::AdjacencyGraph{T,true}, v::Integer) where {T} = g.S.colptr[v + 1] - g.S.colptr[v]
271271

272-
function degree(g::AdjacencyGraph{T,true}, v::Integer) where {T}
272+
function degree(g::AdjacencyGraph{T,false}, v::Integer) where {T}
273273
neigh = neighbors(g, v)
274274
has_selfloop = insorted(v, neigh)
275275
return g.S.colptr[v + 1] - g.S.colptr[v] - has_selfloop
276276
end
277277

278-
nb_edges(g::AdjacencyGraph{T,false}) where {T} = nnz(g.S) ÷ 2
278+
nb_edges(g::AdjacencyGraph{T,true}) where {T} = nnz(g.S) ÷ 2
279279

280-
function nb_edges(g::AdjacencyGraph{T,true}) where {T}
280+
function nb_edges(g::AdjacencyGraph{T,false}) where {T}
281281
ne = 0
282282
for v in vertices(g)
283283
ne += degree(g, v)
@@ -290,7 +290,7 @@ minimum_degree(g::AdjacencyGraph) = minimum(Base.Fix1(degree, g), vertices(g))
290290

291291
function has_neighbor(g::AdjacencyGraph, v::Integer, u::Integer)
292292
for w in neighbors(g, v)
293-
!has_diagonal(g) || (v == w && continue)
293+
augmented_graph(g) || (v == w && continue)
294294
if w == u
295295
return true
296296
end

src/interface.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ function _coloring(
279279
symmetric_pattern::Bool;
280280
forced_colors::Union{AbstractVector{<:Integer},Nothing}=nothing,
281281
)
282-
ag = AdjacencyGraph(A; has_diagonal=true)
282+
ag = AdjacencyGraph(A; augmented_graph=false)
283283
color_and_star_set_by_order = map(algo.orders) do order
284284
vertices_in_order = vertices(ag, order)
285285
return star_coloring(ag, vertices_in_order, algo.postprocessing; forced_colors)
@@ -300,7 +300,7 @@ function _coloring(
300300
decompression_eltype::Type{R},
301301
symmetric_pattern::Bool,
302302
) where {R}
303-
ag = AdjacencyGraph(A; has_diagonal=true)
303+
ag = AdjacencyGraph(A; augmented_graph=false)
304304
color_and_tree_set_by_order = map(algo.orders) do order
305305
vertices_in_order = vertices(ag, order)
306306
return acyclic_coloring(ag, vertices_in_order, algo.postprocessing)
@@ -323,7 +323,7 @@ function _coloring(
323323
forced_colors::Union{AbstractVector{<:Integer},Nothing}=nothing,
324324
) where {R}
325325
A_and_Aᵀ, edge_to_index = bidirectional_pattern(A; symmetric_pattern)
326-
ag = AdjacencyGraph(A_and_Aᵀ, edge_to_index; has_diagonal=false)
326+
ag = AdjacencyGraph(A_and_Aᵀ, edge_to_index; augmented_graph=true)
327327
outputs_by_order = map(algo.orders) do order
328328
vertices_in_order = vertices(ag, order)
329329
_color, _star_set = star_coloring(
@@ -370,7 +370,7 @@ function _coloring(
370370
symmetric_pattern::Bool,
371371
) where {R}
372372
A_and_Aᵀ, edge_to_index = bidirectional_pattern(A; symmetric_pattern)
373-
ag = AdjacencyGraph(A_and_Aᵀ, edge_to_index; has_diagonal=false)
373+
ag = AdjacencyGraph(A_and_Aᵀ, edge_to_index; augmented_graph=true)
374374
outputs_by_order = map(algo.orders) do order
375375
vertices_in_order = vertices(ag, order)
376376
_color, _tree_set = acyclic_coloring(ag, vertices_in_order, algo.postprocessing)

src/order.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ function vertices(
334334

335335
π[index] = u
336336
for v in neighbors(g, u)
337-
!has_diagonal(g) || (u == v && continue)
337+
augmented_graph(g) || (u == v && continue)
338338
dv = degrees[v]
339339
dv == -1 && continue
340340
update_bucket!(db, v, dv; degtype, direction)

src/postprocessing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function postprocess!(
1313
color_used = zeros(Bool, nb_colors)
1414

1515
# nonzero diagonal coefficients force the use of their respective color (there can be no neutral colors if the diagonal is fully nonzero)
16-
if has_diagonal(g)
16+
if !augmented_system(g)
1717
for i in axes(S, 1)
1818
if !iszero(S[i, i])
1919
color_used[color[i]] = true

src/result.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ function TreeSetColoringResult(
411411
diagonal_nzind = T[]
412412
ndiag = 0
413413

414-
if has_diagonal(ag)
414+
if !augmented_graph(ag)
415415
for j in axes(S, 2)
416416
for k in nzrange(S, j)
417417
i = rv[k]

test/graph.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ end;
166166
@test degree(g, 7) == 6
167167
@test degree(g, 8) == 6
168168

169-
g = AdjacencyGraph(transpose(A) * A; has_diagonal=false)
169+
g = AdjacencyGraph(transpose(A) * A; augmented_graph=true)
170170
# wrong degree
171171
@test degree(g, 1) == 4
172172
@test degree(g, 2) == 4

test/order.jl

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,14 @@ end;
6161
end;
6262

6363
@testset "LargestFirst" begin
64-
for has_diagonal in (false, true)
65-
A = sparse([
66-
0 1 0 0
67-
1 0 1 1
68-
0 1 0 1
69-
0 1 1 0
70-
])
71-
ag = AdjacencyGraph(A; has_diagonal)
72-
@test vertices(ag, LargestFirst()) == [2, 3, 4, 1]
73-
end
64+
A = sparse([
65+
0 1 0 0
66+
1 0 1 1
67+
0 1 0 1
68+
0 1 1 0
69+
])
70+
ag = AdjacencyGraph(A)
71+
@test vertices(ag, LargestFirst()) == [2, 3, 4, 1]
7472

7573
A = sparse([
7674
1 1 0 0

0 commit comments

Comments
 (0)