Skip to content

Commit 121924f

Browse files
authoredJul 19, 2024··
update (#86)
·
v4.0.1v2.2.0
1 parent 30b6cea commit 121924f

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed
 

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ docs/src/generated/
88
docs/src/assets/indigo.css
99
docs/src/notebooks
1010
.vscode
11+
.DS_Store

‎src/networks/IndependentSet.jl

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,38 @@ end
5353
misv(vals) = vals
5454

5555
"""
56-
mis_compactify!(tropicaltensor)
56+
mis_compactify!(tropicaltensor; potential=nothing)
5757
5858
Compactify tropical tensor for maximum independent set problem. It will eliminate
5959
some entries by setting them to zero, by the criteria that removing these entry
6060
does not change the MIS size of its parent graph (reference to be added).
61+
62+
### Arguments
63+
- `tropicaltensor::AbstractArray{T}`: the tropical tensor
64+
65+
### Keyword arguments
66+
- `potential=nothing`: the maximum possible MIS contribution from each open vertex
6167
"""
62-
function mis_compactify!(a::AbstractArray{T}) where T <: TropicalTypes
68+
function mis_compactify!(a::AbstractArray{T, N}; potential=nothing) where {T <: TropicalTypes, N}
69+
@assert potential === nothing || length(potential) == N "got unexpected potential length: $(length(potential)), expected $N"
6370
for (ind_a, val_a) in enumerate(a)
6471
for (ind_b, val_b) in enumerate(a)
6572
bs_a = ind_a - 1
6673
bs_b = ind_b - 1
67-
@inbounds if bs_a != bs_b && val_a <= val_b && (bs_b & bs_a) == bs_b
68-
a[ind_a] = zero(T)
69-
end
74+
if worse_than(bs_a, bs_b, val_a.n, val_b.n, potential)
75+
@inbounds a[ind_a] = zero(T)
76+
end
7077
end
7178
end
7279
return a
7380
end
81+
function worse_than(bs_a::Integer, bs_b::Integer, val_a::T, val_b::T, potential::AbstractVector) where T
82+
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
83+
end
84+
function worse_than(bs_a::Integer, bs_b::Integer, val_a::T, val_b::T, ::Nothing) where T
85+
bs_a != bs_b && val_a <= val_b && (bs_b & bs_a) == bs_b
86+
end
87+
readbit(bs::Integer, k::Integer) = (bs >> (k-1)) & 1
7488

7589
"""
7690
is_independent_set(g::SimpleGraph, config)

‎test/networks/IndependentSet.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ using GenericTensorNetworks, Test, Graphs
99
m = solve(g, SizeMax())
1010
@test m isa Array{Tropical{Float64}, 4}
1111
@test count(!iszero, m) == 12
12-
mis_compactify!(m)
13-
@test count(!iszero, m) == 3
12+
m1 = mis_compactify!(copy(m))
13+
@test count(!iszero, m1) == 3
14+
potential = zeros(Float64, 4)
15+
m2 = mis_compactify!(copy(m); potential)
16+
@test count(!iszero, m2) == 1
1417
@test get_weights(g) == UnitWeight()
1518
@test get_weights(chweights(g, fill(3, 6))) == fill(3, 6)
1619
end

0 commit comments

Comments
 (0)
Please sign in to comment.