diff --git a/.gitignore b/.gitignore index 7b6d54c1..db108369 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ docs/src/generated/ docs/src/assets/indigo.css docs/src/notebooks .vscode +.DS_Store diff --git a/src/networks/IndependentSet.jl b/src/networks/IndependentSet.jl index 2e8b3db7..135bba60 100644 --- a/src/networks/IndependentSet.jl +++ b/src/networks/IndependentSet.jl @@ -53,24 +53,38 @@ end misv(vals) = vals """ - mis_compactify!(tropicaltensor) + mis_compactify!(tropicaltensor; potential=nothing) Compactify tropical tensor for maximum independent set problem. It will eliminate some entries by setting them to zero, by the criteria that removing these entry does not change the MIS size of its parent graph (reference to be added). + +### Arguments +- `tropicaltensor::AbstractArray{T}`: the tropical tensor + +### Keyword arguments +- `potential=nothing`: the maximum possible MIS contribution from each open vertex """ -function mis_compactify!(a::AbstractArray{T}) where T <: TropicalTypes +function mis_compactify!(a::AbstractArray{T, N}; potential=nothing) where {T <: TropicalTypes, N} + @assert potential === nothing || length(potential) == N "got unexpected potential length: $(length(potential)), expected $N" for (ind_a, val_a) in enumerate(a) for (ind_b, val_b) in enumerate(a) bs_a = ind_a - 1 bs_b = ind_b - 1 - @inbounds if bs_a != bs_b && val_a <= val_b && (bs_b & bs_a) == bs_b - a[ind_a] = zero(T) - end + if worse_than(bs_a, bs_b, val_a.n, val_b.n, potential) + @inbounds a[ind_a] = zero(T) + end end end return a end +function worse_than(bs_a::Integer, bs_b::Integer, val_a::T, val_b::T, potential::AbstractVector) where T + bs_a != bs_b && val_a + sum(k->readbit(bs_a, k) < readbit(bs_b, k) ? potential[k] : zero(T), 1:length(potential)) <= val_b +end +function worse_than(bs_a::Integer, bs_b::Integer, val_a::T, val_b::T, ::Nothing) where T + bs_a != bs_b && val_a <= val_b && (bs_b & bs_a) == bs_b +end +readbit(bs::Integer, k::Integer) = (bs >> (k-1)) & 1 """ is_independent_set(g::SimpleGraph, config) diff --git a/test/networks/IndependentSet.jl b/test/networks/IndependentSet.jl index da92d136..99212791 100644 --- a/test/networks/IndependentSet.jl +++ b/test/networks/IndependentSet.jl @@ -9,8 +9,11 @@ using GenericTensorNetworks, Test, Graphs m = solve(g, SizeMax()) @test m isa Array{Tropical{Float64}, 4} @test count(!iszero, m) == 12 - mis_compactify!(m) - @test count(!iszero, m) == 3 + m1 = mis_compactify!(copy(m)) + @test count(!iszero, m1) == 3 + potential = zeros(Float64, 4) + m2 = mis_compactify!(copy(m); potential) + @test count(!iszero, m2) == 1 @test get_weights(g) == UnitWeight() @test get_weights(chweights(g, fill(3, 6))) == fill(3, 6) end