-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83225aa
commit 3fcc0fb
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
""" | ||
compute(::Type{ChromaticNumber}, g::AbstractGraph{T}) where T <: Integer | ||
Return the chromatic number of `g`. | ||
""" | ||
function compute( | ||
::Type{ChromaticNumber}, | ||
g::AbstractGraph{T}; | ||
optimizer = HiGHS.Optimizer | ||
) where T <: Int | ||
min_prop_coloring = compute(MinimumProperColoring, g; optimizer=optimizer) | ||
return length(unique(values(min_prop_coloring))) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
function compute( | ||
::Type{MinimumProperColoring}, | ||
g::AbstractGraph{T}; | ||
optimizer = HiGHS.Optimizer | ||
) where T <: Integer | ||
|
||
# Instantiate the optimization model. | ||
model = Model(optimizer) | ||
JuMP.set_silent(model) | ||
|
||
# Get the vertices and edges of `g`. | ||
V = vertices(g) | ||
E = edges(g) | ||
|
||
# Decision variable for each vertex and color. | ||
@variable(model, x[V, V], Bin) | ||
@variable(model, y[V], Bin) | ||
|
||
# Constraint: Each vertex gets exactly one color | ||
@constraint(model, [v = V], sum(x[v, k] for k in V) == 1) | ||
|
||
# Constraint: Adjacent vertices cannot have the same color | ||
for e in E | ||
u, v = src(e), dst(e) | ||
@constraint(model, [k = V], x[u, k] + x[v, k] <= 1) | ||
end | ||
|
||
# Constraint: If a vertex is colored with k, color k is used | ||
@constraint(model, [v = V, k = V], x[v, k] <= y[k]) | ||
|
||
# Constraint: Use the sequential nature of colors. A color k can be used only if color k-1 is used | ||
for k = 2:nv(g) | ||
@constraint(model, y[k] <= y[k-1]) | ||
end | ||
|
||
# Objective: Minimize the number of colors used | ||
@objective(model, Min, sum(y)) | ||
|
||
optimize!(model) | ||
|
||
# Extracting the color for each vertex | ||
vertex_colors = Dict{Int, Int}() | ||
for v in V | ||
for k in V | ||
if value(x[v, k]) > 0.5 # using 0.5 as a threshold due to potential numerical issues | ||
vertex_colors[v] = k | ||
break | ||
end | ||
end | ||
end | ||
return vertex_colors | ||
end |