-
Notifications
You must be signed in to change notification settings - Fork 3
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
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a8de642
Add new type: Dominating Set problem
SciCodePhy 91784f5
Fix a typo in src/models/DominatingSet.jl
SciCodePhy 50fa0cf
Fix the evaluate function of Dominating Set problem
SciCodePhy 74c1d56
Fix the evaluate function and corresponding tests
SciCodePhy a3bfe72
Update the test
SciCodePhy c8baa1a
Update the test
SciCodePhy 99ff141
A possible typo of spinglass_maxcut reduction test
SciCodePhy cfc8c9c
Merge branch 'main' into Nick/DominatingSet
SciCodePhy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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 | ||
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 |
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,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 |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.