You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Optimize star coloring and its postprocessing (#171)
* Optimize star coloring and its postprocessing
* Better handle trivial stars in the postprocessing
* Use JuliaFormatter for coloring.jl
* Remove utils.jl from this PR
* Fix an error in postprocess!
* Switch hub when necessary
---------
Co-authored-by: Guillaume Dalle <22795598+gdalle@users.noreply.github.com>
# Create a list of spokes for each star, preallocating their sizes based on nb_spokes
151
+
spokes = [Vector{Int}(undef, ns) for ns in nb_spokes]
152
+
153
+
# Reuse nb_spokes as counters to track the current index while filling the spokes
154
+
fill!(nb_spokes, 0)
155
+
149
156
for ((i, j), s) inpairs(star)
150
-
h = hub[s]
157
+
h =abs(hub[s])
158
+
nb_spokes[s] +=1
159
+
index = nb_spokes[s]
160
+
161
+
# Assign the non-hub vertex (spoke) to the correct position in spokes
151
162
if i == h
152
-
push!(spokes[s], j)
163
+
spokes[s][index] = j
153
164
elseif j == h
154
-
push!(spokes[s], i)
165
+
spokes[s][index] = i
155
166
end
156
167
end
157
168
returnStarSet(star, hub, spokes)
@@ -181,6 +192,7 @@ function _update_stars!(
181
192
# modified
182
193
star::Dict{<:Tuple,<:Integer},
183
194
hub::AbstractVector{<:Integer},
195
+
nb_spokes::AbstractVector{<:Integer},
184
196
# not modified
185
197
g::AdjacencyGraph,
186
198
v::Integer,
@@ -194,8 +206,10 @@ function _update_stars!(
194
206
for x inneighbors(g, w)
195
207
if x != v && color[x] == color[v] # vw, wx ∈ E
196
208
wx =_sort(w, x)
197
-
hub[star[wx]] = w # this may already be true
198
-
star[vw] = star[wx]
209
+
star_wx = star[wx]
210
+
hub[star_wx] = w # this may already be true
211
+
nb_spokes[star_wx] +=1
212
+
star[vw] = star_wx
199
213
x_exists =true
200
214
break
201
215
end
@@ -204,10 +218,13 @@ function _update_stars!(
204
218
(p, q) = first_neighbor[color[w]]
205
219
if p == v && q != w # vw, vq ∈ E and color[w] = color[q]
206
220
vq =_sort(v, q)
207
-
hub[star[vq]] = v # this may already be true
208
-
star[vw] = star[vq]
221
+
star_vq = star[vq]
222
+
hub[star_vq] = v # this may already be true
223
+
nb_spokes[star_vq] +=1
224
+
star[vw] = star_vq
209
225
else# vw forms a new star
210
226
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
227
+
push!(nb_spokes, 1)
211
228
star[vw] =length(hub)
212
229
end
213
230
end
@@ -543,10 +560,37 @@ function postprocess!(
543
560
end
544
561
if star_or_tree_set isa StarSet
545
562
# only the colors of the hubs are used
546
-
(; hub) = star_or_tree_set
563
+
(; hub, spokes) = star_or_tree_set
564
+
nb_trivial_star =0
565
+
566
+
# Iterate through all non-trivial stars
547
567
for s ineachindex(hub)
548
568
j = hub[s]
549
-
color_used[color[j]] =true
569
+
if j >0
570
+
color_used[color[j]] =true
571
+
else
572
+
nb_trivial_star +=1
573
+
end
574
+
end
575
+
576
+
# Process the trivial stars (if any)
577
+
if nb_trivial_star >0
578
+
for s ineachindex(hub)
579
+
j = hub[s]
580
+
if j <0
581
+
i = spokes[s][1]
582
+
j =abs(j)
583
+
if color_used[color[i]]
584
+
# Make i the hub to avoid possibly adding one more used color
0 commit comments