Documentation ¶
Overview ¶
Package combin implements routines involving combinatorics (permutations, combinations, etc.).
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Binomial ¶
Binomial returns the binomial coefficient of (n,k), also commonly referred to as "n choose k".
The binomial coefficient, C(n,k), is the number of unordered combinations of k elements in a set that is n elements big, and is defined as
C(n,k) = n!/((n-k)!k!)
n and k must be non-negative with n >= k, otherwise Binomial will panic. No check is made for overflow.
func Combinations ¶
Combinations generates all of the combinations of k elements from a set of size n. The returned slice has length Binomial(n,k) and each inner slice has length k.
n and k must be non-negative with n >= k, otherwise Combinations will panic.
CombinationGenerator may alternatively be used to generate the combinations iteratively instead of collectively.
func GeneralizedBinomial ¶
GeneralizedBinomial returns the generalized binomial coefficient of (n, k), defined as
Γ(n+1) / (Γ(k+1) Γ(n-k+1))
where Γ is the Gamma function. GeneralizedBinomial is useful for continuous relaxations of the binomial coefficient, or when the binomial coefficient value may overflow int. In the latter case, one may use math/big for an exact computation.
n and k must be non-negative with n >= k, otherwise GeneralizedBinomial will panic.
func LogGeneralizedBinomial ¶
LogGeneralizedBinomial returns the log of the generalized binomial coefficient. See GeneralizedBinomial for more information.
Types ¶
type CombinationGenerator ¶
type CombinationGenerator struct {
// contains filtered or unexported fields
}
CombinationGenerator generates combinations iteratively. Combinations may be called to generate all combinations collectively.
func NewCombinationGenerator ¶
func NewCombinationGenerator(n, k int) *CombinationGenerator
NewCombinationGenerator returns a CombinationGenerator for generating the combinations of k elements from a set of size n.
n and k must be non-negative with n >= k, otherwise NewCombinationGenerator will panic.
func (*CombinationGenerator) Combination ¶
func (c *CombinationGenerator) Combination(combination []int) []int
Combination generates the next combination. If next is non-nil, it must have length k and the result will be stored in-place into combination. If combination is nil a new slice will be allocated and returned. If all of the combinations have already been constructed (Next() returns false), Combination will panic.
Next must be called to initialize the first value before calling Combination or Combination will panic. The value returned by Combination is only changed during calls to Next.
func (*CombinationGenerator) Next ¶
func (c *CombinationGenerator) Next() bool
Next advances the iterator if there are combinations remaining to be generated, and returns false if all combinations have been generated. Next must be called to initialize the first value before calling Combination or Combination will panic. The value returned by Combination is only changed during calls to Next.