Skip to content

Commit 2cf5f8b

Browse files
authored
Add postprocess_with_star_set! and postprocess_with_tree_set! (#279)
1 parent 85e2d05 commit 2cf5f8b

File tree

1 file changed

+116
-92
lines changed

1 file changed

+116
-92
lines changed

src/postprocessing.jl

Lines changed: 116 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -22,99 +22,11 @@ function postprocess!(
2222
end
2323

2424
if star_or_tree_set isa StarSet
25-
# only the colors of the hubs are used
26-
(; star, hub) = star_or_tree_set
27-
nb_trivial_stars = 0
28-
29-
# Iterate through all non-trivial stars
30-
for s in eachindex(hub)
31-
h = hub[s]
32-
if h > 0
33-
color_used[color[h]] = true
34-
else
35-
nb_trivial_stars += 1
36-
end
37-
end
38-
39-
# Process the trivial stars (if any)
40-
if nb_trivial_stars > 0
41-
rvS = rowvals(S)
42-
for j in axes(S, 2)
43-
for k in nzrange(S, j)
44-
i = rvS[k]
45-
if i > j
46-
index_ij = edge_to_index[k]
47-
s = star[index_ij]
48-
h = hub[s]
49-
if h < 0
50-
h = abs(h)
51-
spoke = h == j ? i : j
52-
if color_used[color[spoke]]
53-
# Switch the hub and the spoke to possibly avoid adding one more used color
54-
hub[s] = spoke
55-
else
56-
# Keep the current hub
57-
color_used[color[h]] = true
58-
end
59-
end
60-
end
61-
end
62-
end
63-
end
25+
# star_or_tree_set is a StarSet
26+
postprocess_with_star_set!(g, color_used, color, star_or_tree_set)
6427
else
65-
# only the colors of non-leaf vertices are used
66-
(; reverse_bfs_orders, is_star, tree_edge_indices, nt) = star_or_tree_set
67-
nb_trivial_trees = 0
68-
69-
# Iterate through all non-trivial trees
70-
for k in 1:nt
71-
# Position of the first edge in the tree
72-
first = tree_edge_indices[k]
73-
74-
# Total number of edges in the tree
75-
ne_tree = tree_edge_indices[k + 1] - first
76-
77-
# Check if we have more than one edge in the tree (non-trivial tree)
78-
if ne_tree > 1
79-
# Determine if the tree is a star
80-
if is_star[k]
81-
# It is a non-trivial star and only the color of the hub is needed
82-
(_, hub) = reverse_bfs_orders[first]
83-
color_used[color[hub]] = true
84-
else
85-
# It is not a star and both colors are needed during the decompression
86-
(i, j) = reverse_bfs_orders[first]
87-
color_used[color[i]] = true
88-
color_used[color[j]] = true
89-
end
90-
else
91-
nb_trivial_trees += 1
92-
end
93-
end
94-
95-
# Process the trivial trees (if any)
96-
if nb_trivial_trees > 0
97-
for k in 1:nt
98-
# Position of the first edge in the tree
99-
first = tree_edge_indices[k]
100-
101-
# Total number of edges in the tree
102-
ne_tree = tree_edge_indices[k + 1] - first
103-
104-
# Check if we have exactly one edge in the tree
105-
if ne_tree == 1
106-
(i, j) = reverse_bfs_orders[first]
107-
if color_used[color[i]]
108-
# Make i the root to avoid possibly adding one more used color
109-
# Switch it with the (only) leaf
110-
reverse_bfs_orders[first] = (j, i)
111-
else
112-
# Keep j as the root
113-
color_used[color[j]] = true
114-
end
115-
end
116-
end
117-
end
28+
# star_or_tree_set is a TreeSet
29+
postprocess_with_tree_set!(color_used, color, star_or_tree_set)
11830
end
11931

12032
# if at least one of the colors is useless, modify the color assignments of vertices
@@ -144,3 +56,115 @@ function postprocess!(
14456
end
14557
return color
14658
end
59+
60+
function postprocess_with_star_set!(
61+
g::AdjacencyGraph,
62+
color_used::Vector{Bool},
63+
color::AbstractVector{<:Integer},
64+
star_set::StarSet,
65+
)
66+
S = pattern(g)
67+
edge_to_index = edge_indices(g)
68+
69+
# only the colors of the hubs are used
70+
(; star, hub) = star_set
71+
nb_trivial_stars = 0
72+
73+
# Iterate through all non-trivial stars
74+
for s in eachindex(hub)
75+
h = hub[s]
76+
if h > 0
77+
color_used[color[h]] = true
78+
else
79+
nb_trivial_stars += 1
80+
end
81+
end
82+
83+
# Process the trivial stars (if any)
84+
if nb_trivial_stars > 0
85+
rvS = rowvals(S)
86+
for j in axes(S, 2)
87+
for k in nzrange(S, j)
88+
i = rvS[k]
89+
if i > j
90+
index_ij = edge_to_index[k]
91+
s = star[index_ij]
92+
h = hub[s]
93+
if h < 0
94+
h = abs(h)
95+
spoke = h == j ? i : j
96+
if color_used[color[spoke]]
97+
# Switch the hub and the spoke to possibly avoid adding one more used color
98+
hub[s] = spoke
99+
else
100+
# Keep the current hub
101+
color_used[color[h]] = true
102+
end
103+
end
104+
end
105+
end
106+
end
107+
end
108+
return color_used
109+
end
110+
111+
function postprocess_with_tree_set!(
112+
color_used::Vector{Bool},
113+
color::AbstractVector{<:Integer},
114+
tree_set::TreeSet,
115+
)
116+
# only the colors of non-leaf vertices are used
117+
(; reverse_bfs_orders, is_star, tree_edge_indices, nt) = tree_set
118+
nb_trivial_trees = 0
119+
120+
# Iterate through all non-trivial trees
121+
for k in 1:nt
122+
# Position of the first edge in the tree
123+
first = tree_edge_indices[k]
124+
125+
# Total number of edges in the tree
126+
ne_tree = tree_edge_indices[k + 1] - first
127+
128+
# Check if we have more than one edge in the tree (non-trivial tree)
129+
if ne_tree > 1
130+
# Determine if the tree is a star
131+
if is_star[k]
132+
# It is a non-trivial star and only the color of the hub is needed
133+
(_, hub) = reverse_bfs_orders[first]
134+
color_used[color[hub]] = true
135+
else
136+
# It is not a star and both colors are needed during the decompression
137+
(i, j) = reverse_bfs_orders[first]
138+
color_used[color[i]] = true
139+
color_used[color[j]] = true
140+
end
141+
else
142+
nb_trivial_trees += 1
143+
end
144+
end
145+
146+
# Process the trivial trees (if any)
147+
if nb_trivial_trees > 0
148+
for k in 1:nt
149+
# Position of the first edge in the tree
150+
first = tree_edge_indices[k]
151+
152+
# Total number of edges in the tree
153+
ne_tree = tree_edge_indices[k + 1] - first
154+
155+
# Check if we have exactly one edge in the tree
156+
if ne_tree == 1
157+
(i, j) = reverse_bfs_orders[first]
158+
if color_used[color[i]]
159+
# Make i the root to avoid possibly adding one more used color
160+
# Switch it with the (only) leaf
161+
reverse_bfs_orders[first] = (j, i)
162+
else
163+
# Keep j as the root
164+
color_used[color[j]] = true
165+
end
166+
end
167+
end
168+
end
169+
return color_used
170+
end

0 commit comments

Comments
 (0)