Return to Snippet

Revision: 16427
at August 3, 2009 23:25 by bingjian


Initial Code
def generate_k_subsets(n,k):
    """
    Generate all k-subsets of an n-set sequentially.
    Reference:
        http://www.math.upenn.edu/~wilf/website/CombinatorialAlgorithms.pdf
        Chapter 3
    """
    m, h  = 0, k
    a = range(k)
    yield a
    while True:
        if m < n-h: h = 1
        else: h += 1
        m = a[k-h] + 1
        for j in range(h): a[k+j-h] = m + j
        yield a
        if a[0] == n - k:  break

Initial URL


Initial Description


Initial Title
Generate all k-subsets of an n-set sequentially

Initial Tags


Initial Language
Python