How to iteratively generate k elements subsets from a set of size n in java?

后端 未结 7 1803
忘掉有多难
忘掉有多难 2020-11-29 09:14

I\'m working on a puzzle that involves analyzing all size k subsets and figuring out which one is optimal. I wrote a solution that works when the number of subsets is small,

7条回答
  •  无人及你
    2020-11-29 09:57

    Swift implementation:

    Below are two variants on the answer provided by afsantos.

    The first implementation of the combinations function mirrors the functionality of the original Java implementation.

    The second implementation is a general case for finding all combinations of k values from the set [0, setSize). If this is really all you need, this implementation will be a bit more efficient.

    In addition, they include a few minor optimizations and a smidgin logic simplification.

    /// Calculate the binomial for a set with a subset size
    func binomial(setSize: Int, subsetSize: Int) -> Int
    {
        if (subsetSize <= 0 || subsetSize > setSize) { return 0 }
    
        // Take advantage of symmetry
        var subsetSizeDelta = subsetSize
        if (subsetSizeDelta > setSize - subsetSizeDelta)
        {
            subsetSizeDelta = setSize - subsetSizeDelta
        }
    
        // Early-out
        if subsetSizeDelta == 0 { return 1 }
    
        var c = 1
        for i in 1...subsetSizeDelta
        {
            c = c * (setSize - (subsetSizeDelta - i))
            c = c / i
        }
        return c
    }
    
    /// Calculates all possible combinations of subsets of `subsetSize` values within `set`
    func combinations(subsetSize: Int, set: [Int]) -> [[Int]]?
    {
        // Validate inputs
        if subsetSize <= 0 || subsetSize > set.count { return nil }
    
        // Use a binomial to calculate total possible combinations
        let comboCount = binomial(setSize: set.count, subsetSize: subsetSize)
        if comboCount == 0 { return nil }
    
        // Our set of combinations
        var combos = [[Int]]()
        combos.reserveCapacity(comboCount)
    
        // Initialize the combination to the first group of set indices
        var subsetIndices = [Int](0.. set.count - (subsetSize - x))
                {
                    x -= 1
                    if x >= 0 { continue }
                }
                else
                {
                    for x1 in x+1.. [[Int]]?
    {
        // Validate inputs
        if subsetSize <= 0 || subsetSize > setSize { return nil }
    
        // Use a binomial to calculate total possible combinations
        let comboCount = binomial(setSize: setSize, subsetSize: subsetSize)
        if comboCount == 0 { return nil }
    
        // Our set of combinations
        var combos = [[Int]]()
        combos.reserveCapacity(comboCount)
    
        // Initialize the combination to the first group of elements
        var subsetValues = [Int](0.. setSize - (subsetSize - x))
                {
                    x -= 1
                    if x >= 0 { continue }
                }
                else
                {
                    for x1 in x+1..

提交回复
热议问题