feat(go4k): Algorithm to construct small delay times tables by abusing overlapping of different delay times.

The problem of finding a string that contains all particular substrings is the "shortest superstring problem"; it is NP-hard and analogous to traveling salesman problem. We use simple greedy search instead of trying to find true optimum. But even with these algorithm, units that use exactly the same delay times will always appear only once in the delay times table.
This commit is contained in:
Veikko Sariola
2020-11-09 22:29:10 +02:00
parent c153239710
commit e36aea59a5
3 changed files with 174 additions and 6 deletions

36
go4k/algorithms_test.go Normal file
View File

@ -0,0 +1,36 @@
package go4k_test
import (
"fmt"
"reflect"
"testing"
"github.com/vsariola/sointu/go4k"
)
func TestFindSuperIntArray(t *testing.T) {
var tests = []struct {
input [][]int
wantSuper []int
wantIndices []int
}{
{[][]int{}, []int{}, []int{}},
{[][]int{nil, nil}, []int{}, []int{0, 0}},
{[][]int{{3, 4, 5}, {1, 2, 3}}, []int{1, 2, 3, 4, 5}, []int{2, 0}},
{[][]int{{3, 4, 5}, {1, 2, 3}, nil}, []int{1, 2, 3, 4, 5}, []int{2, 0, 0}},
{[][]int{{3, 4, 5}, {1, 2, 3}, {}}, []int{1, 2, 3, 4, 5}, []int{2, 0, 0}},
{[][]int{{3, 4, 5}, {1, 2, 3}, {2, 3}}, []int{1, 2, 3, 4, 5}, []int{2, 0, 1}},
{[][]int{{1, 2, 3, 4, 5}, {1, 2, 3}}, []int{1, 2, 3, 4, 5}, []int{0, 0}},
{[][]int{{1, 2, 3, 4, 5}, {2, 3}}, []int{1, 2, 3, 4, 5}, []int{0, 1}},
{[][]int{{1, 2, 3, 4, 5}, {2, 3}, {5, 6, 7}}, []int{1, 2, 3, 4, 5, 6, 7}, []int{0, 1, 4}},
{[][]int{{1, 2, 3, 4}, {3, 4, 1}, {2, 3, 4, 5}}, []int{3, 4, 1, 2, 3, 4, 5}, []int{2, 0, 3}},
}
for i, tt := range tests {
t.Run(fmt.Sprintf("TestFindSuperIntArray %d", i), func(t *testing.T) {
super, indices := go4k.FindSuperIntArray(tt.input)
if !reflect.DeepEqual(super, tt.wantSuper) || !reflect.DeepEqual(indices, tt.wantIndices) {
t.Errorf("FindSuperIntArray(%v) got (%v,%v), want (%v,%v)", tt.input, super, indices, tt.wantSuper, tt.wantIndices)
}
})
}
}