-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchoose.go
65 lines (55 loc) · 1.58 KB
/
choose.go
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
/*
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)
}