Reputation: 151
As part of a large project in python, I need a fast generator function that produces all possible sets of non-negative integer numbers smaller than n
, such that each set has at most s
elements and the difference between the largest and smallest numbers in the set is smaller than w
.
The fastest implementation that I have achieved so far makes use of itertools
:
import itertools
def subsample(n, s, w):
nn = range(w)
for p in range(s):
o = list(itertools.combinations(nn, p+1))
for t in o:
yield t
for _ in range(0, n-w):
pt = o
o = [tuple([op + 1 for op in list(u)]) for u in pt]
for t in list((set(o) ^ set(pt)) & set(o)):
yield t
For instance:
In [1]: list(subsample(6,3,3))
Out [1]: [(0,), (1,), (2,), (3,), (4,), (5,), (0, 1), (0, 2), (1, 2), (1, 3), (2, 3), (3, 4), (2, 4), (4, 5), (3, 5), (0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5)]
But I am convinced that there must be more efficient ways to do this. Is there anything that would make this faster?
Upvotes: 1
Views: 297
Reputation: 52049
Here are some generators based on algorithms attributed to Knuth.
subsets4
generates all subsets of 1..n having k or less elementssubsets5
restricts the subsets generated by subsets4
to those having a max difference of wsubsets
generates all subsets of 1..n of length exactly ksubsets2
restricts the subsets of generated by subsets
to those having a max difference of wRun the megatest()
function to test the subsets5
generator for multiple values of n, k and w.
# all subsets of k or less elements of 1..n
def subsets4(n,k):
a = [ 0 ] * k
i = 0
while i >= 0:
a[i] += 1
yield a[0:i+1]
r = a[i]+1
i += 1
while i < k and r <= n:
a[i] = r
yield a[0:i+1]
i += 1
r += 1
i -= 1
if a[i] >= n:
i -= 1
# all subsets of k or less elements of 1..n with max difference <= w
def subsets5(n,k,w):
a = [ 0 ] * k
i = 0
while i >= 0:
a[i] += 1
yield a[0:i+1]
r = a[i]+1
i += 1
while i < k and r <= n and r-a[0] <= w:
a[i] = r
yield a[0:i+1]
i += 1
r += 1
i -= 1
if a[i] >= n or a[i]+1-a[0] > w:
i -= 1
# all subsets of 1..n having exactly k elements
def subsets(n,k):
a = range(1,k+1)
while a[0] <= n+1-k:
yield a
# find i
i = k-1
while i >= 0 and a[i]+k-i >= n+1: i -= 1
r = a[i]
a[i] += 1
j = 2
i += 1
while i < k:
a[i] = r + j
i += 1
j += 1
# all subsets of 1..n having exactly k elements and whose max
# difference is w
def subsets2(n,k,w):
if k > w: return
a = range(1,k+1)
while a[0] <= n+1-k:
yield a
i = k-1
while i >= 0 and (a[i]+k-i >= n+1 or a[i]+k-i-a[0] > w) : i -= 1
r = a[i]
a[i] += 1
j = 2
i += 1
while i < k:
a[i] = r + j
i += 1
j += 1
def test(n,k,w):
s1 = [ s for s in subsets4(n,k) if s[-1] - s[0] <= w ]
s2 = [ s for s in subsets5(n,k,w) ]
if s1 == s2:
print "OK", n,k,w
return 0
else:
print "NOT OK", n, k, w
return 1
# for s in subsets2(10,3,4): print s
# for s in subsets(10,3): print s
def megatest():
failed = 0
for n in xrange(10,20):
for k in xrange(1,n+1):
for w in xrange(k,n+1):
failed += test(n,k,w)
print "failed:", failed
Upvotes: 1