Skip to content

Commit e03c509

Browse files
committed
Store the hub(s) as tuple in star coloring
1 parent 2ab1c47 commit e03c509

2 files changed

Lines changed: 23 additions & 33 deletions

File tree

src/coloring.jl

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ function star_coloring(
8787
first_neighbor = fill((zero(T), zero(T), zero(T)), nv) # at first no neighbors have been encountered
8888
treated = zeros(T, nv)
8989
star = Vector{T}(undef, ne)
90-
hub = T[] # one hub for each star, including the trivial ones
90+
# duplicate the hub for non-trivial star -- store both vertices for trivial stars
91+
# we always use the first component of the tuple as the hub for decompression
92+
hub = Tuple{T,T}[]
9193
vertices_in_order = vertices(g, order)
9294

9395
for v in vertices_in_order
@@ -108,7 +110,8 @@ function star_coloring(
108110
for (x, index_wx) in neighbors_with_edge_indices(g, w)
109111
!has_diagonal(g) || (w == x && continue)
110112
(x == v || iszero(color[x])) && continue
111-
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)
113+
(h, h2) = hub[star[index_wx]]
114+
if (h == h2) && (x == h) # potential Case 2 (which is always false for trivial stars with undefined hub)
112115
forbidden_colors[color[x]] = v
113116
end
114117
end
@@ -153,7 +156,7 @@ end
153156
function _update_stars!(
154157
# modified
155158
star::AbstractVector{<:Integer},
156-
hub::AbstractVector{<:Integer},
159+
hub::AbstractVector{<:Tuple},
157160
# not modified
158161
g::AdjacencyGraph,
159162
v::Integer,
@@ -168,7 +171,7 @@ function _update_stars!(
168171
!has_diagonal(g) || (w == x && continue)
169172
if x != v && color[x] == color[v] # vw, wx ∈ E
170173
star_wx = star[index_wx]
171-
hub[star_wx] = w # this may already be true
174+
hub[star_wx] = (w, w) # this may already be true
172175
star[index_vw] = star_wx
173176
x_exists = true
174177
break
@@ -178,10 +181,10 @@ function _update_stars!(
178181
(p, q, index_pq) = first_neighbor[color[w]]
179182
if p == v && q != w # vw, vq ∈ E and color[w] = color[q]
180183
star_vq = star[index_pq]
181-
hub[star_vq] = v # this may already be true
184+
hub[star_vq] = (v, v) # this may already be true
182185
star[index_vw] = star_vq
183186
else # vw forms a new star
184-
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
187+
push!(hub, (v, w)) # star is trivial (composed only of two vertices)
185188
star[index_vw] = length(hub)
186189
end
187190
end
@@ -201,8 +204,8 @@ $TYPEDFIELDS
201204
struct StarSet{T}
202205
"a mapping from edges (pair of vertices) to their star index"
203206
star::Vector{T}
204-
"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)"
205-
hub::Vector{T}
207+
"a mapping from star indices to their hub"
208+
hub::Vector{Tuple{T,T}}
206209
end
207210

208211
"""
@@ -546,9 +549,8 @@ function postprocess!(
546549
nb_trivial_stars = 0
547550

548551
# Iterate through all non-trivial stars
549-
for s in eachindex(hub)
550-
h = hub[s]
551-
if h > 0
552+
for (h, h2) in hub
553+
if h == h2
552554
color_used[color[h]] = true
553555
else
554556
nb_trivial_stars += 1
@@ -557,25 +559,15 @@ function postprocess!(
557559

558560
# Process the trivial stars (if any)
559561
if nb_trivial_stars > 0
560-
rvS = rowvals(S)
561-
for j in axes(S, 2)
562-
for k in nzrange(S, j)
563-
i = rvS[k]
564-
if i > j
565-
index_ij = edge_to_index[k]
566-
s = star[index_ij]
567-
h = hub[s]
568-
if h < 0
569-
h = abs(h)
570-
spoke = h == j ? i : j
571-
if color_used[color[spoke]]
572-
# Switch the hub and the spoke to possibly avoid adding one more used color
573-
hub[s] = spoke
574-
else
575-
# Keep the current hub
576-
color_used[color[h]] = true
577-
end
578-
end
562+
for s in eachindex(hub)
563+
(h, h2) = hub[s]
564+
if h != h2
565+
if color_used[color[h2]]
566+
# Switch the hub and the spoke to possibly avoid adding one more used color
567+
hub[s] = (h2, h)
568+
else
569+
# Keep the current hub
570+
color_used[color[h]] = true
579571
end
580572
end
581573
end

src/result.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,7 @@ function StarSetColoringResult(
273273
# off-diagonal coefficients
274274
index_ij = edge_to_index[k]
275275
s = star[index_ij]
276-
h = abs(hub[s])
277-
278-
# Assign the non-hub vertex (spoke) to the correct position in spokes
276+
h, _ = hub[s]
279277
if i == h
280278
# i is the hub and j is the spoke
281279
c = color[i]

0 commit comments

Comments
 (0)