1 Star 0 Fork 0

妥協 / fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
choose.go 1.58 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package graph
import "math/big"
type orderedSet struct {
elements []interface{}
}
func (s *orderedSet) add(o interface{}) {
s.elements = append(s.elements, o)
}
type indiceSet struct {
indices []int
}
type indiceSets []*indiceSet
// CombinationsExceed computes the number of combinations
// of choosing K elements from N elements, and returns
// whether the number of combinations exceeds a given threshold.
// If n < k then it returns false.
func CombinationsExceed(n, k, threshold int) bool {
if n < k {
return false
}
combinations := &big.Int{}
combinations = combinations.Binomial(int64(n), int64(k))
t := &big.Int{}
t.SetInt64(int64(threshold))
return combinations.Cmp(t) > 0
}
func chooseKoutOfN(n, k int) indiceSets {
var res indiceSets
subGroups := &orderedSet{}
choose(n, k, 0, nil, subGroups)
for _, el := range subGroups.elements {
res = append(res, el.(*indiceSet))
}
return res
}
func choose(n int, targetAmount int, i int, currentSubGroup []int, subGroups *orderedSet) {
// Check if we have enough elements in our current subgroup
if len(currentSubGroup) == targetAmount {
subGroups.add(&indiceSet{indices: currentSubGroup})
return
}
// Return early if not enough remaining candidates to pick from
itemsLeftToPick := n - i
if targetAmount-len(currentSubGroup) > itemsLeftToPick {
return
}
// We either pick the current element
choose(n, targetAmount, i+1, append(currentSubGroup, i), subGroups)
// Or don't pick it
choose(n, targetAmount, i+1, currentSubGroup, subGroups)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/liurenhao/fabric.git
git@gitee.com:liurenhao/fabric.git
liurenhao
fabric
fabric
v2.0.0-beta

搜索帮助

344bd9b3 5694891 D2dac590 5694891