Skip to content

Commit aa8ae93

Browse files
authored
New: Reduction from Matching to Set Packing (#129)
* save * New:Reduction from Matching to Set Packing
1 parent 525be7e commit aa8ae93

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

src/ProblemReductions.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export ReductionSATToIndependentSet, ReductionSATToDominatingSet
4545
export ReductionIndependentSetToSetPacking, ReductionSetPackingToIndependentSet
4646
export ReductionSATToCircuit
4747
export ReductionIndependentSetToVertexCovering
48+
export ReductionMatchingToSetPacking
4849

4950
# reduction path
5051
export ReductionGraph, reduction_graph, reduction_paths, ConcatenatedReduction

src/rules/matching_setpacking.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
$TYPEDEF
3+
The reduction result of a vertex matching to a set packing problem.
4+
5+
### Fields
6+
- `SetPacking{WT<:AbstractVector{Int}}`: the target set packing problem
7+
"""
8+
struct ReductionMatchingToSetPacking{ET,T,WT<:AbstractVector{T}} <: AbstractReductionResult
9+
setpacking::SetPacking{ET, T, WT}
10+
end
11+
Base.:(==)(a::ReductionMatchingToSetPacking, b::ReductionMatchingToSetPacking) = a.setpacking == b.setpacking
12+
13+
target_problem(res::ReductionMatchingToSetPacking) = res.setpacking
14+
15+
function reduceto(::Type{SetPacking}, s::Matching)
16+
sp = matching2setpacking(s.graph, s.weights)
17+
return ReductionMatchingToSetPacking(sp)
18+
end
19+
20+
function matching2setpacking(g::SimpleGraph, weights)
21+
subsets = Vector{Vector{Int}}()
22+
# map edges to subset
23+
for edge in edges(g)
24+
push!(subsets,[edge.src, edge.dst])
25+
end
26+
return SetPacking(subsets, weights)
27+
end
28+
29+
function extract_solution(res::ReductionMatchingToSetPacking, sol)
30+
return sol
31+
end

src/rules/rules.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,5 @@ include("sat_independentset.jl")
8787
include("sat_dominatingset.jl")
8888
include("independentset_setpacking.jl")
8989
include("circuit_sat.jl")
90-
include("vertexcovering_independentset.jl")
90+
include("vertexcovering_independentset.jl")
91+
include("matching_setpacking.jl")

test/rules/matching_setpacking.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Test, ProblemReductions, Graphs
2+
3+
@testset "matching_setpacking" begin
4+
5+
# UnitWeight Graph
6+
g1 = SimpleGraph(4)
7+
for (i, j) in [(1, 2), (1, 3), (3, 4), (2, 3)]
8+
add_edge!(g1, i, j)
9+
end
10+
Matching1 = Matching(g1)
11+
SP1 = reduceto(SetPacking, Matching1)
12+
@test target_problem(SP1) == SP1.setpacking
13+
@test target_problem(SP1) == SetPacking([[1,2], [1,3],[2,3] ,[3,4]], [1, 1, 1, 1])
14+
sol = findbest(SP1.setpacking, BruteForce())
15+
@test extract_solution(SP1, sol) == sol
16+
17+
# Weighted Graph
18+
g2 = SimpleGraph(4)
19+
for (i, j) in [(1, 2), (1, 3), (3, 4), (2, 3)]
20+
add_edge!(g2, i, j)
21+
end
22+
Matching2 = Matching(g2, [1, 2, 3, 4])
23+
SP2 = reduceto(SetPacking, Matching2)
24+
@test target_problem(SP2) == SP2.setpacking
25+
@test target_problem(SP2) == SetPacking([[1,2], [1,3], [2,3], [3,4]], [1, 2, 3, 4])
26+
sol1 = findbest(SP2.setpacking, BruteForce())
27+
sol2 = findbest(Matching2,BruteForce())
28+
@test extract_solution(SP2, sol1) == sol2
29+
end

test/rules/rules.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ end
4848
include("vertexcovering_independentset.jl")
4949
end
5050

51+
@testset "matching_setpacking" begin
52+
include("matching_setpacking.jl")
53+
end
54+
5155
@testset "rules" begin
5256
circuit = CircuitSAT(@circuit begin
5357
x = a ¬b
@@ -65,6 +69,7 @@ end
6569
is = IndependentSet(graph)
6670
is2 = IndependentSet(graph2)
6771
setpacking = SetPacking([[1, 2, 5], [1, 3], [2, 4], [3, 6], [2, 3, 6]])
72+
matching = Matching(graph)
6873
for (source, target_type) in [
6974
# please add more tests here
7075
circuit => SpinGlass{<:SimpleGraph},
@@ -83,6 +88,7 @@ end
8388
is2 => SetPacking,
8489
setpacking => IndependentSet{<:SimpleGraph},
8590
is => VertexCovering,
91+
matching => SetPacking
8692
]
8793
@info "Testing reduction from $(typeof(source)) to $(target_type)"
8894
# directly solve

0 commit comments

Comments
 (0)