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
* Add an acyclic coloring
* Group by color
* Typo
* Noref
* Fix test
* Working indirect decompression
* Large random tests pass
---------
Co-authored-by: Guillaume Dalle <22795598+gdalle@users.noreply.github.com>
first_neighbor =fill((0, 0), length(g)) # at first no neighbors have been encountered
82
-
treated =zeros(Int, length(g))
79
+
nvertices =length(g)
80
+
color =zeros(Int, nvertices)
81
+
forbidden_colors =zeros(Int, nvertices)
82
+
first_neighbor =fill((0, 0), nvertices) # at first no neighbors have been encountered
83
+
treated =zeros(Int, nvertices)
83
84
star =Dict{Tuple{Int,Int},Int}()
84
85
hub = Int[]
85
86
vertices_in_order =vertices(g, order)
@@ -119,17 +120,13 @@ function star_coloring(g::Graph, order::AbstractOrder)
119
120
end
120
121
121
122
"""
122
-
$TYPEDEF
123
+
StarSet
123
124
124
-
Encode a set of 2-colored stars resulting from the star coloring algorithm.
125
+
Encode a set of 2-colored stars resulting from the [`star_coloring`](@ref) algorithm.
125
126
126
127
# Fields
127
128
128
129
$TYPEDFIELDS
129
-
130
-
# References
131
-
132
-
> [_New Acyclic and Star Coloring Algorithms with Application to Computing Hessians_](https://epubs.siam.org/doi/abs/10.1137/050639879), Gebremedhin et al. (2007), Algorithm 4.1
133
130
"""
134
131
struct StarSet
135
132
"a mapping from edges (pair of vertices) their to star index"
@@ -262,3 +259,159 @@ function symmetric_coefficient(
262
259
return j, color[h]
263
260
end
264
261
end
262
+
263
+
"""
264
+
acyclic_coloring(g::Graph, order::AbstractOrder)
265
+
266
+
Compute an acyclic coloring of all vertices in the adjacency graph `g` and return a vector of integer colors.
267
+
268
+
An _acyclic coloring_ is a distance-1 coloring with the further restriction that every cycle uses at least 3 colors.
269
+
270
+
The vertices are colored in a greedy fashion, following the `order` supplied.
271
+
272
+
# See also
273
+
274
+
- [`Graph`](@ref)
275
+
- [`AbstractOrder`](@ref)
276
+
277
+
# References
278
+
279
+
> [_New Acyclic and Star Coloring Algorithms with Application to Computing Hessians_](https://epubs.siam.org/doi/abs/10.1137/050639879), Gebremedhin et al. (2007), Algorithm 3.1
_merge_trees!(v, w, x, disjoint_sets) # merge trees T₁ ∋ vw and T₂ ∋ wx if T₁ != T₂
324
+
end
325
+
end
326
+
end
327
+
end
328
+
return color, TreeSet(disjoint_sets, parent)
329
+
end
330
+
331
+
function_prevent_cycle!(
332
+
# not modified
333
+
v::Integer,
334
+
w::Integer,
335
+
x::Integer,
336
+
color::AbstractVector{<:Integer},
337
+
# modified
338
+
first_visit_to_tree::AbstractVector{<:Tuple},
339
+
forbidden_colors::AbstractVector{<:Integer},
340
+
disjoint_sets::DisjointSets{<:Tuple{Int,Int}},
341
+
)
342
+
wx =_sort(w, x)
343
+
root =find_root!(disjoint_sets, wx) # edge wx belongs to the 2-colored tree T represented by edge "root"
344
+
id = disjoint_sets.intmap[root] # ID of the representative edge "root" of a two-colored tree.
345
+
(p, q) = first_visit_to_tree[id]
346
+
if p != v # T is being visited from vertex v for the first time
347
+
vw =_sort(v, w)
348
+
first_visit_to_tree[id] = (v, w)
349
+
elseif q != w # T is connected to vertex v via at least two edges
350
+
forbidden_colors[color[x]] = v
351
+
end
352
+
returnnothing
353
+
end
354
+
355
+
function_grow_star!(
356
+
# not modified
357
+
v::Integer,
358
+
w::Integer,
359
+
color::AbstractVector{<:Integer},
360
+
# modified
361
+
first_neighbor::AbstractVector{<:Tuple},
362
+
disjoint_sets::DisjointSets{Tuple{Int,Int}},
363
+
parent::Vector{Int},
364
+
)
365
+
vw =_sort(v, w)
366
+
push!(disjoint_sets, vw) # Create a new tree T_{vw} consisting only of edge vw
367
+
push!(parent, v) # the vertex v is the parent of the vertex w in tree T_{vw} -- parent[disjoint_sets.intmap[vw]] == v
368
+
(p, q) = first_neighbor[color[w]]
369
+
if p != v # a neighbor of v with color[w] encountered for the first time
370
+
first_neighbor[color[w]] = (v, w)
371
+
else# merge T_{vw} with a two-colored star being grown around v
372
+
vw =_sort(v, w)
373
+
pq =_sort(p, q)
374
+
root1 =find_root!(disjoint_sets, vw)
375
+
root2 =find_root!(disjoint_sets, pq)
376
+
root_union!(disjoint_sets, root1, root2)
377
+
end
378
+
returnnothing
379
+
end
380
+
381
+
function_merge_trees!(
382
+
# not modified
383
+
v::Integer,
384
+
w::Integer,
385
+
x::Integer,
386
+
# modified
387
+
disjoint_sets::DisjointSets{Tuple{Int,Int}},
388
+
)
389
+
vw =_sort(v, w)
390
+
wx =_sort(w, x)
391
+
root1 =find_root!(disjoint_sets, vw)
392
+
root2 =find_root!(disjoint_sets, wx)
393
+
if root1 != root2
394
+
root_union!(disjoint_sets, root1, root2)
395
+
end
396
+
returnnothing
397
+
end
398
+
399
+
"""
400
+
TreeSet
401
+
402
+
Encode a set of 2-colored trees resulting from the acyclic coloring algorithm.
403
+
404
+
# Fields
405
+
406
+
$TYPEDFIELDS
407
+
408
+
# References
409
+
410
+
> [_New Acyclic and Star Coloring Algorithms with Application to Computing Hessians_](https://epubs.siam.org/doi/abs/10.1137/050639879), Gebremedhin et al. (2007), Algorithm 4.1
0 commit comments