Skip to content

Commit d53dc97

Browse files
committed
Remove spokes and nb_spokes
1 parent e559df8 commit d53dc97

1 file changed

Lines changed: 29 additions & 70 deletions

File tree

src/coloring.jl

Lines changed: 29 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ function star_coloring(g::AdjacencyGraph, order::AbstractOrder; postprocessing::
8888
treated = zeros(Int, nv)
8989
star = Vector{Int}(undef, ne)
9090
hub = Int[] # one hub for each star, including the trivial ones
91-
nb_spokes = Int[] # number of spokes for each star
9291
vertices_in_order = vertices(g, order)
9392

9493
# edge_to_index gives an index for each edge
@@ -142,12 +141,12 @@ function star_coloring(g::AdjacencyGraph, order::AbstractOrder; postprocessing::
142141
break
143142
end
144143
end
145-
_update_stars!(star, hub, nb_spokes, g, v, color, first_neighbor, edge_to_index)
144+
_update_stars!(star, hub, g, v, color, first_neighbor, edge_to_index)
146145
end
147-
star_set = StarSet(g, star, hub, nb_spokes, color, edge_to_index)
146+
star_set = StarSet(star, hub)
148147
if postprocessing
149148
# Reuse the vector forbidden_colors to compute offsets during post-processing
150-
postprocess!(color, star_set, g, forbidden_colors)
149+
postprocess!(color, star_set, g, forbidden_colors, edge_to_index)
151150
end
152151
return color, star_set, edge_to_index
153152
end
@@ -174,7 +173,6 @@ function _update_stars!(
174173
# modified
175174
star::AbstractVector{<:Integer},
176175
hub::AbstractVector{<:Integer},
177-
nb_spokes::AbstractVector{<:Integer},
178176
# not modified
179177
g::AdjacencyGraph,
180178
v::Integer,
@@ -193,7 +191,6 @@ function _update_stars!(
193191
index_wx = edge_to_index[S.colptr[w] + ix - 1]
194192
star_wx = star[index_wx]
195193
hub[star_wx] = w # this may already be true
196-
nb_spokes[star_wx] += 1
197194
star[index_vw] = star_wx
198195
x_exists = true
199196
break
@@ -204,11 +201,9 @@ function _update_stars!(
204201
if p == v && q != w # vw, vq ∈ E and color[w] = color[q]
205202
star_vq = star[index_pq]
206203
hub[star_vq] = v # this may already be true
207-
nb_spokes[star_vq] += 1
208204
star[index_vw] = star_vq
209205
else # vw forms a new star
210206
push!(hub, -max(v, w)) # star is trivial (composed only of two vertices) so we set the hub to a negative value, but it allows us to choose one of the two vertices
211-
push!(nb_spokes, 1)
212207
star[index_vw] = length(hub)
213208
end
214209
end
@@ -230,50 +225,6 @@ struct StarSet
230225
star::Vector{Int}
231226
"a mapping from star indices to their hub (undefined hubs for single-edge stars are the negative value of one of the vertices, picked arbitrarily)"
232227
hub::Vector{Int}
233-
"a mapping from star indices to the vector of their spokes"
234-
spokes::Vector{Vector{Int}}
235-
end
236-
237-
function StarSet(
238-
g::AdjacencyGraph,
239-
star::Vector{Int},
240-
hub::Vector{Int},
241-
nb_spokes::Vector{Int},
242-
color::Vector{Int},
243-
edge_to_index::Vector{Int},
244-
)
245-
S = pattern(g)
246-
n = S.n
247-
248-
# Create a list of spokes for each star, preallocating their sizes based on nb_spokes
249-
spokes = [Vector{Int}(undef, ns) for ns in nb_spokes]
250-
251-
# Reuse nb_spokes as counters to track the current index while filling the spokes
252-
fill!(nb_spokes, 0)
253-
254-
rvS = rowvals(S)
255-
for j in axes(S, 2)
256-
for k in nzrange(S, j)
257-
i = rvS[k]
258-
if i > j
259-
index_ij = edge_to_index[k]
260-
s = star[index_ij]
261-
h = abs(hub[s])
262-
nb_spokes[s] += 1
263-
index = nb_spokes[s]
264-
265-
# Assign the non-hub vertex (spoke) to the correct position in spokes
266-
if i == h
267-
# i is the hub and j is the spoke
268-
spokes[s][index] = j
269-
else # j == h
270-
# j is the hub and i is the spoke
271-
spokes[s][index] = i
272-
end
273-
end
274-
end
275-
end
276-
return StarSet(star, hub, spokes)
277228
end
278229

279230
"""
@@ -384,7 +335,7 @@ function acyclic_coloring(g::AdjacencyGraph, order::AbstractOrder; postprocessin
384335
tree_set = TreeSet(g, forest, edge_to_index)
385336
if postprocessing
386337
# Reuse the vector forbidden_colors to compute offsets during post-processing
387-
postprocess!(color, tree_set, g, forbidden_colors)
338+
postprocess!(color, tree_set, g, forbidden_colors, edge_to_index)
388339
end
389340
return color, tree_set
390341
end
@@ -612,6 +563,7 @@ function postprocess!(
612563
star_or_tree_set::Union{StarSet,TreeSet},
613564
g::AdjacencyGraph,
614565
offsets::Vector{Int},
566+
edge_to_index::Vector{Int},
615567
)
616568
(; S) = g
617569
# flag which colors are actually used during decompression
@@ -629,34 +581,41 @@ function postprocess!(
629581

630582
if star_or_tree_set isa StarSet
631583
# only the colors of the hubs are used
632-
(; hub, spokes) = star_or_tree_set
584+
(; star, hub) = star_or_tree_set
633585
nb_trivial_stars = 0
634586

635587
# Iterate through all non-trivial stars
636588
for s in eachindex(hub)
637-
j = hub[s]
638-
if j > 0
639-
color_used[color[j]] = true
589+
h = hub[s]
590+
if h > 0
591+
color_used[color[h]] = true
640592
else
641593
nb_trivial_stars += 1
642594
end
643595
end
644596

645597
# Process the trivial stars (if any)
598+
# TODO: Should we compute "compressed_indices" in the same time?
646599
if nb_trivial_stars > 0
647-
for s in eachindex(hub)
648-
j = hub[s]
649-
if j < 0
650-
i = spokes[s][1]
651-
j = abs(j)
652-
if color_used[color[i]]
653-
# Make i the hub to avoid possibly adding one more used color
654-
# Switch it with the (only) spoke
655-
hub[s] = i
656-
spokes[s][1] = j
657-
else
658-
# Keep j as the hub
659-
color_used[color[j]] = true
600+
rvS = rowvals(S)
601+
for j in axes(S, 2)
602+
for k in nzrange(S, j)
603+
i = rvS[k]
604+
if i > j
605+
index_ij = edge_to_index[k]
606+
s = star[index_ij]
607+
h = hub[s]
608+
if h < 0
609+
h = abs(h)
610+
spoke = h == j ? i : j
611+
if color_used[color[spoke]]
612+
# Switch the hub and the spoke to possibly avoid adding one more used color
613+
hub[s] = spoke
614+
else
615+
# Keep the current hub
616+
color_used[color[h]] = true
617+
end
618+
end
660619
end
661620
end
662621
end

0 commit comments

Comments
 (0)