Skip to content

Commit

Permalink
added chromatic coloring stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
RandyRDavila committed Oct 3, 2023
1 parent 83225aa commit 3fcc0fb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/GraphInvariants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ struct IndependenceNumber <: AbstractOptimalCardinality end
struct MaximumClique <: AbstractOptimalSet end
struct CliqueNumber <: AbstractOptimalCardinality end

struct MinimumProperColoring <: AbstractOptimalSet end
struct ChromaticNumber <: AbstractOptimalCardinality end

struct MinimumDominatingSet <: AbstractOptimalSet end
struct DominationNumber <: AbstractOptimalCardinality end

Expand Down Expand Up @@ -87,6 +90,9 @@ include("basics/from_graphs.jl")
include("cliques/maximimum_clique.jl")
include("cliques/clique_number.jl")

include("chromatic_colorings/proper_colorings.jl")
include("chromatic_colorings/chromatic_number.jl")

include("degree_sequence_invariants/havel_hakimi_residue.jl")

include("domination/minimum_dominating_sets.jl")
Expand Down Expand Up @@ -123,6 +129,9 @@ export IndependenceNumber
export MaximumClique
export CliqueNumber

export MinimumProperColoring
export ChromaticNumber

export MinimumDominatingSet
export DominationNumber
export DominationRule
Expand Down
15 changes: 15 additions & 0 deletions src/chromatic_colorings/chromatic_number.jl
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
52 changes: 52 additions & 0 deletions src/chromatic_colorings/proper_colorings.jl
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

0 comments on commit 3fcc0fb

Please sign in to comment.