Skip to content

Commit 95753e8

Browse files
committed
Create a function build_edge_to_index
1 parent ea69572 commit 95753e8

2 files changed

Lines changed: 30 additions & 44 deletions

File tree

src/coloring.jl

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -83,34 +83,13 @@ function star_coloring(g::AdjacencyGraph, order::AbstractOrder, postprocessing::
8383
ne = nb_edges(g)
8484
color = zeros(Int, nv)
8585
forbidden_colors = zeros(Int, nv)
86-
edge_to_index = Vector{Int}(undef, nnz(S))
86+
edge_to_index = build_edge_to_index(S, forbidden_colors)
8787
first_neighbor = fill((0, 0, 0), nv) # at first no neighbors have been encountered
8888
treated = zeros(Int, nv)
8989
star = Vector{Int}(undef, ne)
9090
hub = Int[] # one hub for each star, including the trivial ones
9191
vertices_in_order = vertices(g, order)
9292

93-
# edge_to_index gives an index for each edge
94-
# use forbidden_colors (or color) for the offsets of each column
95-
offsets = forbidden_colors
96-
counter = 0
97-
rvS = rowvals(S)
98-
for j in axes(S, 2)
99-
for k in nzrange(S, j)
100-
i = rvS[k]
101-
if i > j
102-
counter += 1
103-
edge_to_index[k] = counter
104-
k2 = S.colptr[i] + offsets[i]
105-
edge_to_index[k2] = counter
106-
offsets[i] += 1
107-
end
108-
end
109-
end
110-
fill!(offsets, 0)
111-
# Note that we don't need to do that for bicoloring,
112-
# we can build that in the same time than the transposed sparsity pattern of A
113-
11493
for v in vertices_in_order
11594
for (iw, w) in enumerate(neighbors2(g, v))
11695
(v == w || iszero(color[w])) && continue
@@ -257,33 +236,12 @@ function acyclic_coloring(g::AdjacencyGraph, order::AbstractOrder, postprocessin
257236
ne = nb_edges(g)
258237
color = zeros(Int, nv)
259238
forbidden_colors = zeros(Int, nv)
260-
edge_to_index = Vector{Int}(undef, nnz(S))
239+
edge_to_index = build_edge_to_index(S, forbidden_colors)
261240
first_neighbor = fill((0, 0, 0), nv) # at first no neighbors have been encountered
262241
first_visit_to_tree = fill((0, 0), ne)
263242
forest = Forest{Int}(ne)
264243
vertices_in_order = vertices(g, order)
265244

266-
# edge_to_index gives an index for each edge
267-
# use forbidden_colors (or color) for the offsets of each column
268-
offsets = forbidden_colors
269-
counter = 0
270-
rvS = rowvals(S)
271-
for j in axes(S, 2)
272-
for k in nzrange(S, j)
273-
i = rvS[k]
274-
if i > j
275-
counter += 1
276-
edge_to_index[k] = counter
277-
k2 = S.colptr[i] + offsets[i]
278-
edge_to_index[k2] = counter
279-
offsets[i] += 1
280-
end
281-
end
282-
end
283-
fill!(offsets, 0)
284-
# Note that we don't need to do that for bicoloring,
285-
# we can build that in the same time than the transposed sparsity pattern of A
286-
287245
for v in vertices_in_order
288246
for w in neighbors(g, v)
289247
iszero(color[w]) && continue

src/graph.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,34 @@ function bidirectional_pattern(S::SparsityPatternCSC; symmetric_pattern)
163163
return S_and_Sᵀ
164164
end
165165

166+
# Note that we don't need to do that for bicoloring,
167+
# we can build that in the same time than the transposed sparsity pattern of A
168+
function build_edge_to_index(S::SparsityPatternCSC, forbidden_colors::Vector{Int})
169+
# edge_to_index gives an index for each edge
170+
edge_to_index = Vector{Int}(undef, nnz(S))
171+
# use forbidden_colors (or color) for the offsets of each column
172+
offsets = forbidden_colors
173+
counter = 0
174+
rvS = rowvals(S)
175+
for j in axes(S, 2)
176+
for k in nzrange(S, j)
177+
i = rvS[k]
178+
if i > j
179+
counter += 1
180+
edge_to_index[k] = counter
181+
k2 = S.colptr[i] + offsets[i]
182+
edge_to_index[k2] = counter
183+
offsets[i] += 1
184+
elseif i == j
185+
# this should never be used, make sure it errors
186+
edge_to_index[k] = 0
187+
end
188+
end
189+
end
190+
fill!(offsets, 0)
191+
return edge_to_index
192+
end
193+
166194
## Adjacency graph
167195

168196
"""

0 commit comments

Comments
 (0)