Skip to content

Commit 0eb6ade

Browse files
Heap's Algorithm
1 parent 8bc4e86 commit 0eb6ade

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@
123123
* Sieve
124124
* [Sieve](https://github.com/TheAlgorithms/Go/blob/master/math/sieve/Sieve.go)
125125
* [Sieve Test](https://github.com/TheAlgorithms/Go/blob/master/math/sieve/sieve_test.go)
126+
* Permutation
127+
* [Heap's Algorithm](https://github.com/TheAlgorithms/Go/blob/master/math/permutation/heaps.go)
128+
* [Heap's Algorithm Test](https://github.com/TheAlgorithms/Go/blob/master/math/permutation/heaps_test.go)
126129

127130
## Other
128131
* Maxsubarraysum

math/permutation/heaps.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package permutation
2+
3+
import (
4+
"strings"
5+
)
6+
7+
// Heap's Algorithm for generating all permutations of n objects
8+
func Heaps(out chan []string, n int) {
9+
elementSetCh := make(chan []string)
10+
go GenerateElementSet(elementSetCh, n)
11+
elementSet := <-elementSetCh
12+
13+
var recursiveGenerate func([]string, int, []string)
14+
var permutations []string
15+
recursiveGenerate = func(previousIteration []string, n int, elements []string) {
16+
if n == 1 {
17+
permutations = append(permutations, strings.Join(elements, ""))
18+
} else {
19+
for i := 0; i < n; i++ {
20+
recursiveGenerate(previousIteration, n - 1, elements)
21+
if n % 2 == 1 {
22+
tmp := elements[i]
23+
elements[i] = elements[n - 1]
24+
elements[n - 1] = tmp
25+
} else {
26+
tmp := elements[0]
27+
elements[0] = elements[n - 1]
28+
elements[n - 1] = tmp
29+
}
30+
}
31+
}
32+
}
33+
recursiveGenerate(permutations, n, elementSet)
34+
out <- permutations
35+
}
36+
37+
func GenerateElementSet(out chan []string, n int) {
38+
elementSet := make([]string, n)
39+
for i, _ := range elementSet {
40+
elementSet[i] = string(i + 49) // Adjust this if you want to change your charset
41+
}
42+
out <- elementSet
43+
}

math/permutation/heaps_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package permutation
2+
3+
import (
4+
"reflect"
5+
"sort"
6+
"testing"
7+
)
8+
9+
func TestHeaps(t *testing.T) {
10+
t.Run("Should generate permutations for various size sets", func (t *testing.T) {
11+
expectedValues := [][]string {
12+
{"1"},
13+
{"12", "21"},
14+
{"123", "213", "321", "231", "312", "132"},
15+
{"1234", "1243", "1324", "1342", "1423", "1432", "2134", "2143", "2314", "2341", "2413", "2431", "3124", "3142", "3214", "3241", "3412", "3421", "4123", "4132", "4213", "4231", "4312", "4321"},
16+
{"12345", "12354", "12435", "12453", "12534", "12543", "13245", "13254", "13425", "13452", "13524", "13542", "14235", "14253", "14325", "14352", "14523", "14532", "15234", "15243", "15324", "15342", "15423", "15432", "21345", "21354", "21435", "21453", "21534", "21543", "23145", "23154", "23415", "23451", "23514", "23541", "24135", "24153", "24315", "24351", "24513", "24531", "25134", "25143", "25314", "25341", "25413", "25431", "31245", "31254", "31425", "31452", "31524", "31542", "32145", "32154", "32415", "32451", "32514", "32541", "34125", "34152", "34215", "34251", "34512", "34521", "35124", "35142", "35214", "35241", "35412", "35421", "41235", "41253", "41325", "41352", "41523", "41532", "42135", "42153", "42315", "42351", "42513", "42531", "43125", "43152", "43215", "43251", "43512", "43521", "45123", "45132", "45213", "45231", "45312", "45321", "51234", "51243", "51324", "51342", "51423", "51432", "52134", "52143", "52314", "52341", "52413", "52431", "53124", "53142", "53214", "53241", "53412", "53421", "54123", "54132", "54213", "54231", "54312", "54321"},
17+
}
18+
permutationsCh := make(chan []string)
19+
var value []string
20+
21+
for i := 1; i <= 5; i++ {
22+
go Heaps(permutationsCh, i)
23+
value = <- permutationsCh
24+
sort.Strings(value)
25+
sort.Strings(expectedValues[i - 1])
26+
if !reflect.DeepEqual(value, expectedValues[i - 1]) {
27+
t.Errorf("Permutation set is incorrect for element size of %v. Expected (%v) and received (%v)", i, expectedValues[i - 1], value)
28+
}
29+
}
30+
})
31+
}

0 commit comments

Comments
 (0)