Skip to content

Commit 50f3f23

Browse files
amontoisongdalle
andauthored
Remove vertices_by_tree for the acyclic coloring (#179)
* Remove vertices_by_tree for the acyclic coloring * Remove vertices_by_tree for the acyclic coloring * Update coloring.jl * Apply suggestions from code review --------- Co-authored-by: Guillaume Dalle <22795598+gdalle@users.noreply.github.com>
1 parent a47d96e commit 50f3f23

3 files changed

Lines changed: 23 additions & 17 deletions

File tree

src/coloring.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,6 @@ Encode a set of 2-colored trees resulting from the [`acyclic_coloring`](@ref) al
432432
$TYPEDFIELDS
433433
"""
434434
struct TreeSet
435-
vertices_by_tree::Vector{Vector{Int}}
436435
reverse_bfs_orders::Vector{Vector{Tuple{Int,Int}}}
437436
end
438437

@@ -445,6 +444,7 @@ function TreeSet(forest::DisjointSets{Tuple{Int,Int}}, nvertices::Int)
445444

446445
# dictionary that maps a tree's root to the index of the tree
447446
roots = Dict{Int,Int}()
447+
sizehint!(roots, ntrees)
448448

449449
# vector of dictionaries where each dictionary stores the neighbors of each vertex in a tree
450450
trees = [Dict{Int,Vector{Int}}() for i in 1:ntrees]
@@ -454,7 +454,6 @@ function TreeSet(forest::DisjointSets{Tuple{Int,Int}}, nvertices::Int)
454454
for edge in forest.revmap
455455
i, j = edge
456456
# forest has already been compressed so this doesn't change its state
457-
# I wanted to use find_root but it is deprecated
458457
root_edge = find_root!(forest, edge)
459458
root = forest.intmap[root_edge]
460459

@@ -485,14 +484,15 @@ function TreeSet(forest::DisjointSets{Tuple{Int,Int}}, nvertices::Int)
485484
# degrees is a vector of integers that stores the degree of each vertex in a tree
486485
degrees = Vector{Int}(undef, nvertices)
487486

488-
# list of vertices for each tree in the forest
489-
vertices_by_tree = [collect(keys(trees[i])) for i in 1:ntrees]
490-
491487
# reverse breadth first (BFS) traversal order for each tree in the forest
492488
reverse_bfs_orders = [Tuple{Int,Int}[] for i in 1:ntrees]
493489

494490
# nvmax is the number of vertices of the biggest tree in the forest
495-
nvmax = mapreduce(length, max, vertices_by_tree; init=0)
491+
nvmax = 0
492+
for k in 1:ntrees
493+
nb_vertices_tree = length(trees[k])
494+
nvmax = max(nvmax, nb_vertices_tree)
495+
end
496496

497497
# Create a queue with a fixed size nvmax
498498
queue = Vector{Int}(undef, nvmax)
@@ -542,7 +542,7 @@ function TreeSet(forest::DisjointSets{Tuple{Int,Int}}, nvertices::Int)
542542
end
543543
end
544544

545-
return TreeSet(vertices_by_tree, reverse_bfs_orders)
545+
return TreeSet(reverse_bfs_orders)
546546
end
547547

548548
## Postprocessing, mirrors decompression code
@@ -554,7 +554,8 @@ function postprocess!(
554554
)
555555
(; S) = g
556556
# flag which colors are actually used during decompression
557-
color_used = zeros(Bool, maximum(color))
557+
nb_colors = maximum(color)
558+
color_used = zeros(Bool, nb_colors)
558559

559560
# nonzero diagonal coefficients force the use of their respective color (there can be no neutral colors if the diagonal is fully nonzero)
560561
if has_diagonal(g)

src/decompression.jl

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ end
517517
function decompress!(
518518
A::AbstractMatrix, B::AbstractMatrix, result::TreeSetColoringResult, uplo::Symbol=:F
519519
)
520-
(; ag, color, vertices_by_tree, reverse_bfs_orders, buffer) = result
520+
(; ag, color, reverse_bfs_orders, buffer) = result
521521
(; S) = ag
522522
uplo == :F && check_same_pattern(A, S)
523523
R = eltype(A)
@@ -539,10 +539,14 @@ function decompress!(
539539
end
540540

541541
# Recover the off-diagonal coefficients of A
542-
for k in eachindex(vertices_by_tree, reverse_bfs_orders)
543-
for vertex in vertices_by_tree[k]
542+
for k in eachindex(reverse_bfs_orders)
543+
# Reset the buffer to zero for all vertices in a tree (except the root)
544+
for (vertex, _) in reverse_bfs_orders[k]
544545
buffer_right_type[vertex] = zero(R)
545546
end
547+
# Reset the buffer to zero for the root vertex
548+
(_, root) = reverse_bfs_orders[k][end]
549+
buffer_right_type[root] = zero(R)
546550

547551
for (i, j) in reverse_bfs_orders[k]
548552
val = B[i, color[j]] - buffer_right_type[i]
@@ -567,7 +571,6 @@ function decompress!(
567571
(;
568572
ag,
569573
color,
570-
vertices_by_tree,
571574
reverse_bfs_orders,
572575
diagonal_indices,
573576
diagonal_nzind,
@@ -612,10 +615,14 @@ function decompress!(
612615
counter = 0
613616

614617
# Recover the off-diagonal coefficients of A
615-
for k in eachindex(vertices_by_tree, reverse_bfs_orders)
616-
for vertex in vertices_by_tree[k]
618+
for k in eachindex(reverse_bfs_orders)
619+
# Reset the buffer to zero for all vertices in a tree (except the root)
620+
for (vertex, _) in reverse_bfs_orders[k]
617621
buffer_right_type[vertex] = zero(R)
618622
end
623+
# Reset the buffer to zero for the root vertex
624+
(_, root) = reverse_bfs_orders[k][end]
625+
buffer_right_type[root] = zero(R)
619626

620627
for (i, j) in reverse_bfs_orders[k]
621628
counter += 1

src/result.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,6 @@ struct TreeSetColoringResult{M<:AbstractMatrix,G<:AdjacencyGraph,V,R} <:
278278
ag::G
279279
color::Vector{Int}
280280
group::V
281-
vertices_by_tree::Vector{Vector{Int}}
282281
reverse_bfs_orders::Vector{Vector{Tuple{Int,Int}}}
283282
diagonal_indices::Vector{Int}
284283
diagonal_nzind::Vector{Int}
@@ -294,7 +293,7 @@ function TreeSetColoringResult(
294293
tree_set::TreeSet,
295294
decompression_eltype::Type{R},
296295
) where {R}
297-
(; vertices_by_tree, reverse_bfs_orders) = tree_set
296+
(; reverse_bfs_orders) = tree_set
298297
(; S) = ag
299298
nvertices = length(color)
300299
group = group_by_color(color)
@@ -369,7 +368,6 @@ function TreeSetColoringResult(
369368
ag,
370369
color,
371370
group,
372-
vertices_by_tree,
373371
reverse_bfs_orders,
374372
diagonal_indices,
375373
diagonal_nzind,

0 commit comments

Comments
 (0)