Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new type: Dominating Set problem (without weights) #48

Merged
merged 8 commits into from
Jul 25, 2024
Merged
1 change: 1 addition & 0 deletions src/ProblemReductions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export Coloring, coloring_energy, is_vertex_coloring
export SetCovering, is_set_covering, set_covering_energy
export BoolVar, CNFClause, CNF, Satisfiability, is_kSAT, satisfiable
export MaxCut
export DominatingSet

# rules
export target_problem, AbstractProblem, AbstractReductionResult, reduceto, extract_solution, reduction_complexity
Expand Down
42 changes: 42 additions & 0 deletions src/models/DominatingSet.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
$TYPEDEF
DominatingSet(graph; weights=UnitWeight())

The [dominating set](https://queracomputing.github.io/GenericTensorNetworks.jl/dev/generated/DominatingSet/) problem.

Positional arguments
-------------------------------
* `graph` is the problem graph.

We don't have weights for this problem.
"""
struct DominatingSet{ GT<:AbstractGraph} <: AbstractProblem
graph::GT
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each vertex can be associated with a weight.

function DominatingSet( graph::AbstractGraph)
return new{typeof(graph)}(graph)
end
end
Base.:(==)(a::DominatingSet, b::DominatingSet) = ( a.graph == b.graph )

# Variables Interface
variables(gp::DominatingSet) = [1:nv(gp.graph)...]
flavors(::Type{<:DominatingSet}) = [0, 1]

"""
evaluate(c::DominatingSet, config)

Firstly, we count the number of vertices outside the dominating set and the neighbours of the dominating set:
If this number is zero, this configuration corresponds to a dominating set.
* If the configuration is not a dominating set return Inf;
* If the configuration is a dominating set return size(dominating set).
"""

function evaluate(c::DominatingSet, config)
g = c.graph
num_outside_vertices = count(w -> config[w] == 0 && all(v-> config[v] == 0, neighbors(g, w)), Graphs.vertices(g))
if num_outside_vertices == 0
return count(x -> x == 1, config)
else
return Inf
end
end
1 change: 1 addition & 0 deletions src/models/models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,4 @@ include("Coloring.jl")
include("Satisfiability.jl")
include("SetCovering.jl")
include("MaxCut.jl")
include("DominatingSet.jl")
37 changes: 37 additions & 0 deletions test/models/DominatingSet.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Test, ProblemReductions, Graphs

@testset "dominatingset" begin
# construct two equivalent graphs
g01 = SimpleGraph(5)
add_edge!(g01, 1, 2)
add_edge!(g01, 2, 3)
add_edge!(g01, 3, 4)
add_edge!(g01, 4, 5)

g02 = SimpleGraph(5)
add_edge!(g02, 4, 5)
add_edge!(g02, 1, 2)
add_edge!(g02, 3, 4)
add_edge!(g02, 2, 3)

# construct corresponding DominatingSet problems
DS_01 = DominatingSet(g01)
DS_02 = DominatingSet(g02)
@test DS_01 == DS_02

# variables
@test variables(DS_01) == [1, 2, 3, 4, 5]
@test num_variables(DS_01) == 5
@test flavors(DominatingSet) == [0, 1]

# evaluate
# Positive examples
@test evaluate(DS_01, [1, 0, 1, 0, 1]) == 3
@test evaluate(DS_01, [0, 1, 0, 1, 0]) == 2
@test evaluate(DS_01, [1, 1, 1, 1, 0]) == 4
# Negative examples
@test evaluate(DS_01, [0, 1, 1, 0, 0]) == Inf
@test evaluate(DS_01, [1, 0, 0, 0, 1]) == Inf
# findbest function
@test findbest(DS_01, BruteForce()) == [[1, 0, 0, 1, 0], [0, 1, 0, 1, 0], [0, 1, 0, 0, 1]]
end
4 changes: 4 additions & 0 deletions test/models/models.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ end

@testset "Maxcut" begin
include("MaxCut.jl")
end

@testset "DominatingSet" begin
include("DominatingSet.jl")
end
2 changes: 1 addition & 1 deletion test/rules/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using Test, ProblemReductions, Graphs
include("spinglass_sat.jl")
end

@testset "spinglass_sat" begin
@testset "spinglass_maxcut" begin
include("spinglass_maxcut.jl")
end

Expand Down