-
Notifications
You must be signed in to change notification settings - Fork 59
/
combinations.jl
277 lines (232 loc) · 6.31 KB
/
combinations.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
export combinations,
CoolLexCombinations,
multiset_combinations,
with_replacement_combinations,
powerset
#The Combinations iterator
struct Combinations
n::Int
t::Int
end
function Base.iterate(c::Combinations, s = [min(c.t - 1, i) for i in 1:c.t])
if c.t == 0 # special case to generate 1 result for t==0
isempty(s) && return (s, [1])
return
end
for i in c.t:-1:1
s[i] += 1
if s[i] > (c.n - (c.t - i))
continue
end
for j in i+1:c.t
s[j] = s[j-1] + 1
end
break
end
s[1] > c.n - c.t + 1 && return
(s, s)
end
Base.length(c::Combinations) = binomial(c.n, c.t)
Base.eltype(::Type{Combinations}) = Vector{Int}
"""
combinations(a, n)
Generate all combinations of `n` elements from an indexable object `a`. Because the number
of combinations can be very large, this function returns an iterator object.
Use `collect(combinations(a, n))` to get an array of all combinations.
"""
function combinations(a, t::Integer)
if t < 0
# generate 0 combinations for negative argument
t = length(a) + 1
end
reorder(c) = [a[ci] for ci in c]
(reorder(c) for c in Combinations(length(a), t))
end
"""
combinations(a)
Generate combinations of the elements of `a` of all orders. Chaining of order iterators
is eager, but the sequence at each order is lazy.
"""
combinations(a) = Iterators.flatten([combinations(a, k) for k = 1:length(a)])
# cool-lex combinations iterator
"""
CoolLexCombinations
Produce ``(n,k)``-combinations in cool-lex order.
# Reference
Ruskey, F., & Williams, A. (2009). The coolest way to generate combinations.
*Discrete Mathematics*, 309(17), 5305-5320.
"""
struct CoolLexCombinations
n::Int
t::Int
end
struct CoolLexIterState{T<:Integer}
R0::T
R1::T
R2::T
R3::T
end
function Base.iterate(C::CoolLexCombinations)
if C.n < 0
throw(DomainError(C.n))
end
if C.t ≤ 0
throw(DomainError(C.t))
end
#What integer size should I use?
if C.n < 8sizeof(Int)
T = Int
else
T = BigInt
end
state = CoolLexIterState{T}(0, 0, T(1) << C.n, (T(1) << C.t) - 1)
iterate(C, state)
end
function Base.iterate(C::CoolLexCombinations, S::CoolLexIterState)
(S.R3 & S.R2 != 0) && return
R0 = S.R0
R1 = S.R1
R2 = S.R2
R3 = S.R3
R0 = R3 & (R3 + 1)
R1 = xor(R0, R0 - 1)
R0 = R1 + 1
R1 &= R3
R0 = max((R0 & R3) - 1, 0)
R3 += R1 - R0
_cool_lex_visit(S.R3), CoolLexIterState(R0, R1, R2, R3)
end
#Converts an integer bit pattern X into a subset
#If X & 2^k == 1, then k is in the subset
function _cool_lex_visit(X::Integer)
subset = Int[]
n = 1
while X != 0
X & 1 == 1 && push!(subset, n)
X >>= 1
n += 1
end
subset
end
Base.length(C::CoolLexCombinations) = max(0, binomial(C.n, C.t))
struct MultiSetCombinations{T}
m::T
f::Vector{Int}
t::Int
ref::Vector{Int}
end
Base.eltype(::Type{MultiSetCombinations{T}}) where {T} = Vector{eltype(T)}
function Base.length(c::MultiSetCombinations)
t = c.t
if t > length(c.ref)
return 0
end
p = [1; zeros(Int, t)]
for i in 1:length(c.f)
f = c.f[i]
if i == 1
for j in 1:min(f, t)
p[j+1] = 1
end
else
for j in t:-1:1
p[j+1] = sum(p[max(1,j+1-f):(j+1)])
end
end
end
return p[t+1]
end
function multiset_combinations(m, f::Vector{<:Integer}, t::Integer)
length(m) == length(f) || error("Lengths of m and f are not the same.")
ref = length(f) > 0 ? vcat([[i for j in 1:f[i] ] for i in 1:length(f)]...) : Int[]
if t < 0
t = length(ref) + 1
end
MultiSetCombinations(m, f, t, ref)
end
"""
multiset_combinations(a, t)
Generate all combinations of size `t` from an array `a` with possibly duplicated elements.
"""
function multiset_combinations(a, t::Integer)
m = unique(collect(a))
f = Int[sum([c == x for c in a]) for x in m]
multiset_combinations(m, f, t)
end
function Base.iterate(c::MultiSetCombinations, s = c.ref)
((!isempty(s) && max(s[1], c.t) > length(c.ref)) || (isempty(s) && c.t > 0)) && return
ref = c.ref
n = length(ref)
t = c.t
changed = false
comb = [c.m[s[i]] for i in 1:t]
if t > 0
s = copy(s)
for i in t:-1:1
if s[i] < ref[i + (n - t)]
j = 1
while ref[j] <= s[i]
j += 1
end
s[i] = ref[j]
for l in (i+1):t
s[l] = ref[j+=1]
end
changed = true
break
end
end
!changed && (s[1] = n+1)
else
s = [n+1]
end
(comb, s)
end
struct WithReplacementCombinations{T}
a::T
t::Int
end
Base.eltype(::Type{WithReplacementCombinations{T}}) where {T} = Vector{eltype(T)}
Base.length(c::WithReplacementCombinations) = binomial(length(c.a) + c.t - 1, c.t)
"""
with_replacement_combinations(a, t)
Generate all combinations with replacement of size `t` from an array `a`.
"""
with_replacement_combinations(a, t::Integer) = WithReplacementCombinations(a, t)
function Base.iterate(c::WithReplacementCombinations, s = [1 for i in 1:c.t])
(!isempty(s) && s[1] > length(c.a) || c.t < 0) && return
n = length(c.a)
t = c.t
comb = [c.a[si] for si in s]
if t > 0
s = copy(s)
changed = false
for i in t:-1:1
if s[i] < n
s[i] += 1
for j in (i+1):t
s[j] = s[i]
end
changed = true
break
end
end
!changed && (s[1] = n+1)
else
s = [n+1]
end
(comb, s)
end
## Power set
"""
powerset(a, min=0, max=length(a))
Generate all subsets of an indexable object `a` including the empty set, with cardinality
bounded by `min` and `max`. Because the number of subsets can be very large, this function
returns an iterator object. Use `collect(powerset(a, min, max))` to get an array of all
subsets.
"""
function powerset(a, min::Integer=0, max::Integer=length(a))
itrs = [combinations(a, k) for k = min:max]
min < 1 && append!(itrs, eltype(a)[])
Iterators.flatten(itrs)
end