I have a function that is somewhat similar to the traveling salesman problem that I am trying to make recursive. I think it is working but I am having some trouble getting the appending to work correctly on large datasets. Here is what I have...main should be written to be recursive so I can call a variable number of recursions...https://play.golang.org/p/Lz8arHybFr
package main
type Foo struct {
StartPoint string
EndPoint string
distance int
}
func (f *Foo) Connects(endFoo *Foo) bool {
return f.EndPoint == endFoo.StartPoint
}
func (f *Foo) Completes(endFoo *Foo) bool {
return f.StartPoint == endFoo.EndPoint
}
var fl = []*Foo{
// many foo in here
}
func main() {
completeList := [][]*Foo{}
for _, first := range fl {
for _, second := range fl {
if second.Connects(first) {
if second.Completes(first) {
l := []*Foo{
first, second,
}
completeList = append(completeList, l)
}
//if the connection is made, but not complete, keep going
for _, third := range fl {
if third.Connects(second) {
if first.Completes(third) {
l := []*Foo{
first, second, third,
}
completeList = append(completeList, l)
}
}
}
}
}
}
}
I came up with 2 recursions, please, check it on your data: (I suppose you're going to make this parallel, so this will be easier with 2 recursions, just as a go rec2(...)
)
func main() {
rec(0,0)
}
func rec(a, b int) {
if a >= len(fl) || b >= len(fl) || a == b {
return
}
aa, bb := fl[a], fl[b]
if !aa.Connects(bb) {
rec(a, b+1)
return
}
l := []*Foo{
aa, bb,
}
completeList = append(completeList, l)
rec2(0, a, b)
}
func rec2(c, a, b int) {
if c >= len(fl) || a == c || b == c {
return
}
cc, aa, bb := fl[c], fl[a], fl[b]
if cc.Connects(bb) && aa.Completes(cc) {
l := []*Foo{
aa, bb, cc,
}
completeList = append(completeList, l)
}
rec2(c+1, a, b)
}