Documentation
¶
Overview ¶
Package provides algorithm to generates all permutations and combinations.
Index ¶
- func Combinations[Slice ~[]E, E any](n int, l Slice) iter.Seq[Slice]
- func HeapPermutations[Slice ~[]E, E any](l Slice) iter.Seq2[T, Slice]
- func LexCombinations[Slice ~[]E, E any](n int, list Slice) iter.Seq[Slice]
- func LexNext(p []int) bool
- func LexPermutations[Slice ~[]E, E any](l Slice) iter.Seq[Slice]
- func Permutations[Slice ~[]E, E any](list Slice) iter.Seq2[T, Slice]
- func Permute[Slice ~[]E, E any](p P, val Slice)
- func RevCombinations[Slice ~[]E, E any](n int, list Slice) iter.Seq[Slice]
- func SJTEPermutations[Slice ~[]E, E any](l Slice) iter.Seq2[T, Slice]
- func SJTPermutations[Slice ~[]E, E any](l Slice) iter.Seq2[T, Slice]
- func Subset[Slice ~[]E, E any](p S, val Slice) Slice
- func Transpose[Slice ~[]E, E any](t T, p Slice)
- type P
- type S
- type T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Combinations ¶
Combinations returns an iterator over all n-combinations of 'list'.
Example ¶
// For a given set 'x'. x := []string{"a", "b", "c"} // One can simply loop over all 2-combinations. for v := range Combinations(2, x) { fmt.Println(v) }
Output: [a b] [b c] [a c]
func HeapPermutations ¶
HeapPermutations returns an interator over all permutations of 'list' using the Heap algorithm
Each permutation is computed from the previous one + one Transposition. The iterator returns both values.
Example ¶
A := []string{"A", "B", "C", "D"} for t, v := range HeapPermutations(A) { fmt.Println(t, v) }
Output: [0 0] [A B C D] [0 1] [B A C D] [0 2] [C A B D] [0 1] [A C B D] [0 2] [B C A D] [0 1] [C B A D] [0 3] [D B A C] [0 1] [B D A C] [0 2] [A D B C] [0 1] [D A B C] [0 2] [B A D C] [0 1] [A B D C] [1 3] [A C D B] [0 1] [C A D B] [0 2] [D A C B] [0 1] [A D C B] [0 2] [C D A B] [0 1] [D C A B] [2 3] [D C B A] [0 1] [C D B A] [0 2] [B D C A] [0 1] [D B C A] [0 2] [C B D A] [0 1] [B C D A]
func LexCombinations ¶
LexCombinations returns an iterator over all n-combinations of 'list'.
func LexNext ¶
LexNext finds the next permutation in lexicographical order.
return false if it has gone back to the identity permutation.
inspired from Narayana Pandita in https://en.wikipedia.org/wiki/Permutation
func LexPermutations ¶
LexPermutations returns an interator over all permutations of 'list' in lexicographical order.
func Permutations ¶
Permutations returns an interator over all permutations of 'list'.
Each permutation is computed from the previous one + one Transposition. The iterator returns both values.
Example ¶
// For a given set 'x'. x := []string{"a", "b", "c"} // One can simply loop over all permutations. for _, p := range Permutations(x) { fmt.Println(p) }
Output: [a b c] [b a c] [c a b] [a c b] [b c a] [c b a]
func Permute ¶
Permute permutation p to 'val'.
Example ¶
// For a given set 'x'. x := []string{"a", "b", "c"} // The permutation 'p' p := []int{2, 1, 0} // transform 'x' into: `{x[2], x[1], x[0]}` Permute(p, x) fmt.Printf("%#v", x)
Output: []string{"c", "b", "a"}
func RevCombinations ¶
RevCombinations returns an iterator over all n-combinations of 'list' according to the Revolving Door algorithm.
ref Knuth, Donald Ervin. The Art of Computer Programming, volume 4, fascicle 3; generating all combinations and partitions, sec. 7.2.1.3, algorithm R, Revolving-door combinations, p. 9.
func SJTEPermutations ¶
SJTEPermutations returns an interator over all permutations of 'list' using the Steinhaus-Johnson-Trotter algorithm with the Even speed up.
func SJTPermutations ¶
SJTPermutations returns an interator over all permutations of 'list' using the Steinhaus-Johnson-Trotter algorithm.