Skip to content

Commit 60be001

Browse files
committed
Move all subroutines in star_coloring and acyclic_coloring
1 parent 10239ec commit 60be001

1 file changed

Lines changed: 60 additions & 135 deletions

File tree

src/coloring.jl

Lines changed: 60 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,20 @@ function star_coloring(
9999
if p == v # Case 1
100100
if treated[q] != v
101101
# forbid colors of neighbors of q
102-
_treat!(treated, forbidden_colors, g, v, q, color)
102+
for x in neighbors(g, q)
103+
!has_diagonal(g) || (q == x && continue)
104+
iszero(color[x]) && continue
105+
forbidden_colors[color[x]] = v
106+
end
107+
treated[q] = v
103108
end
104109
# forbid colors of neighbors of w
105-
_treat!(treated, forbidden_colors, g, v, w, color)
110+
for x in neighbors(g, w)
111+
!has_diagonal(g) || (w == x && continue)
112+
iszero(color[x]) && continue
113+
forbidden_colors[color[x]] = v
114+
end
115+
treated[w] = v
106116
else
107117
first_neighbor[color[w]] = (v, w, index_vw)
108118
for (x, index_wx) in neighbors_with_edge_indices(g, w)
@@ -120,7 +130,32 @@ function star_coloring(
120130
break
121131
end
122132
end
123-
_update_stars!(star, hub, g, v, color, first_neighbor)
133+
for (w, index_vw) in neighbors_with_edge_indices(g, v)
134+
!has_diagonal(g) || (v == w && continue)
135+
iszero(color[w]) && continue
136+
x_exists = false
137+
for (x, index_wx) in neighbors_with_edge_indices(g, w)
138+
!has_diagonal(g) || (w == x && continue)
139+
if x != v && color[x] == color[v] # vw, wx ∈ E
140+
star_wx = star[index_wx]
141+
hub[star_wx] = w # this may already be true
142+
star[index_vw] = star_wx
143+
x_exists = true
144+
break
145+
end
146+
end
147+
if !x_exists
148+
(p, q, index_pq) = first_neighbor[color[w]]
149+
if p == v && q != w # vw, vq ∈ E and color[w] = color[q]
150+
star_vq = star[index_pq]
151+
hub[star_vq] = v # this may already be true
152+
star[index_vw] = star_vq
153+
else # vw forms a new star
154+
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
155+
star[index_vw] = length(hub)
156+
end
157+
end
158+
end
124159
end
125160
star_set = StarSet(star, hub)
126161
if postprocessing
@@ -131,64 +166,6 @@ function star_coloring(
131166
return color, star_set
132167
end
133168

134-
function _treat!(
135-
# modified
136-
treated::AbstractVector{<:Integer},
137-
forbidden_colors::AbstractVector{<:Integer},
138-
# not modified
139-
g::AdjacencyGraph,
140-
v::Integer,
141-
w::Integer,
142-
color::AbstractVector{<:Integer},
143-
)
144-
for x in neighbors(g, w)
145-
!has_diagonal(g) || (w == x && continue)
146-
iszero(color[x]) && continue
147-
forbidden_colors[color[x]] = v
148-
end
149-
treated[w] = v
150-
return nothing
151-
end
152-
153-
function _update_stars!(
154-
# modified
155-
star::AbstractVector{<:Integer},
156-
hub::AbstractVector{<:Integer},
157-
# not modified
158-
g::AdjacencyGraph,
159-
v::Integer,
160-
color::AbstractVector{<:Integer},
161-
first_neighbor::AbstractVector{<:Tuple},
162-
)
163-
for (w, index_vw) in neighbors_with_edge_indices(g, v)
164-
!has_diagonal(g) || (v == w && continue)
165-
iszero(color[w]) && continue
166-
x_exists = false
167-
for (x, index_wx) in neighbors_with_edge_indices(g, w)
168-
!has_diagonal(g) || (w == x && continue)
169-
if x != v && color[x] == color[v] # vw, wx ∈ E
170-
star_wx = star[index_wx]
171-
hub[star_wx] = w # this may already be true
172-
star[index_vw] = star_wx
173-
x_exists = true
174-
break
175-
end
176-
end
177-
if !x_exists
178-
(p, q, index_pq) = first_neighbor[color[w]]
179-
if p == v && q != w # vw, vq ∈ E and color[w] = color[q]
180-
star_vq = star[index_pq]
181-
hub[star_vq] = v # this may already be true
182-
star[index_vw] = star_vq
183-
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
185-
star[index_vw] = length(hub)
186-
end
187-
end
188-
end
189-
return nothing
190-
end
191-
192169
"""
193170
StarSet
194171
@@ -254,16 +231,13 @@ function acyclic_coloring(
254231
!has_diagonal(g) || (w == x && continue)
255232
iszero(color[x]) && continue
256233
if forbidden_colors[color[x]] != v
257-
_prevent_cycle!(
258-
v,
259-
w,
260-
x,
261-
index_wx,
262-
color,
263-
first_visit_to_tree,
264-
forbidden_colors,
265-
forest,
266-
)
234+
root_wx = find_root!(forest, index_wx) # root of the 2-colored tree to which the edge wx belongs
235+
(p, q) = first_visit_to_tree[root_wx]
236+
if p != v # T is being visited from vertex v for the first time
237+
first_visit_to_tree[root_wx] = (v, w)
238+
elseif q != w # T is connected to vertex v via at least two edges
239+
forbidden_colors[color[x]] = v
240+
end
267241
end
268242
end
269243
end
@@ -276,7 +250,15 @@ function acyclic_coloring(
276250
for (w, index_vw) in neighbors_with_edge_indices(g, v) # grow two-colored stars around the vertex v
277251
!has_diagonal(g) || (v == w && continue)
278252
iszero(color[w]) && continue
279-
_grow_star!(v, w, index_vw, color, first_neighbor, forest)
253+
# Create a new tree T_{vw} consisting only of edge vw
254+
(p, q, index_pq) = first_neighbor[color[w]]
255+
if p != v # a neighbor of v with color[w] encountered for the first time
256+
first_neighbor[color[w]] = (v, w, index_vw)
257+
else # merge T_{vw} with a two-colored star being grown around v
258+
root_vw = find_root!(forest, index_vw)
259+
root_pq = find_root!(forest, index_pq)
260+
root_union!(forest, root_vw, root_pq)
261+
end
280262
end
281263
for (w, index_vw) in neighbors_with_edge_indices(g, v)
282264
!has_diagonal(g) || (v == w && continue)
@@ -285,7 +267,12 @@ function acyclic_coloring(
285267
!has_diagonal(g) || (w == x && continue)
286268
(x == v || iszero(color[x])) && continue
287269
if color[x] == color[v]
288-
_merge_trees!(v, w, x, index_vw, index_wx, forest) # merge trees T₁ ∋ vw and T₂ ∋ wx if T₁ != T₂
270+
# merge trees T₁ ∋ vw and T₂ ∋ wx if T₁ != T₂
271+
root_vw = find_root!(forest, index_vw)
272+
root_wx = find_root!(forest, index_wx)
273+
if root_vw != root_wx
274+
root_union!(forest, root_vw, root_wx)
275+
end
289276
end
290277
end
291278
end
@@ -302,68 +289,6 @@ function acyclic_coloring(
302289
return color, tree_set
303290
end
304291

305-
function _prevent_cycle!(
306-
# not modified
307-
v::Integer,
308-
w::Integer,
309-
x::Integer,
310-
index_wx::Integer,
311-
color::AbstractVector{<:Integer},
312-
# modified
313-
first_visit_to_tree::AbstractVector{<:Tuple},
314-
forbidden_colors::AbstractVector{<:Integer},
315-
forest::Forest{<:Integer},
316-
)
317-
root_wx = find_root!(forest, index_wx) # The edge wx belongs to the 2-colored tree T, represented by an edge with an integer ID
318-
(p, q) = first_visit_to_tree[root_wx]
319-
if p != v # T is being visited from vertex v for the first time
320-
first_visit_to_tree[root_wx] = (v, w)
321-
elseif q != w # T is connected to vertex v via at least two edges
322-
forbidden_colors[color[x]] = v
323-
end
324-
return nothing
325-
end
326-
327-
function _grow_star!(
328-
# not modified
329-
v::Integer,
330-
w::Integer,
331-
index_vw::Integer,
332-
color::AbstractVector{<:Integer},
333-
# modified
334-
first_neighbor::AbstractVector{<:Tuple},
335-
forest::Forest{<:Integer},
336-
)
337-
# Create a new tree T_{vw} consisting only of edge vw
338-
(p, q, index_pq) = first_neighbor[color[w]]
339-
if p != v # a neighbor of v with color[w] encountered for the first time
340-
first_neighbor[color[w]] = (v, w, index_vw)
341-
else # merge T_{vw} with a two-colored star being grown around v
342-
root_vw = find_root!(forest, index_vw)
343-
root_pq = find_root!(forest, index_pq)
344-
root_union!(forest, root_vw, root_pq)
345-
end
346-
return nothing
347-
end
348-
349-
function _merge_trees!(
350-
# not modified
351-
v::Integer,
352-
w::Integer,
353-
x::Integer,
354-
index_vw::Integer,
355-
index_wx::Integer,
356-
# modified
357-
forest::Forest{<:Integer},
358-
)
359-
root_vw = find_root!(forest, index_vw)
360-
root_wx = find_root!(forest, index_wx)
361-
if root_vw != root_wx
362-
root_union!(forest, root_vw, root_wx)
363-
end
364-
return nothing
365-
end
366-
367292
"""
368293
TreeSet
369294

0 commit comments

Comments
 (0)