I have a graph, where nodes are words (type ** Word **) and edges are replace rules, that make one word from another. For example betwen 123
and333
nodes there is a 12-> 33
edge.
To find all patches from one word to another I have a function FindPath:
func (this *Graph) FindPath(from Word, to Word, visited Dict, current Path) {
if from.Eq(to) {
if len(this.Pathes) == 0 {
fmt.Println(current)
}
this.Pathes = append(this.Pathes, current)
return
}
if visited.Index(from) != -1 { // если уже были в этом узле
return
}
index := this.nodes.Index(from)
if index == -1 {
return
}
for r := 0; r < len(this.rules); r++ {
index := from.Index(this.rules[r].Pat)
if index == -1 {
continue
}
this.FindPath(from.ApplyRule(this.rules[r]), to, append(visited, from), append(current, this.rules[r]))
}
return
}
After FindPath is called, all pathes should be stored in this.Pathes, but appart from true pathes there are some false ones.
To see that you can add these lines between lines 2 and 3
if len(this.Pathes) == 0 {
fmt.Println(current)
}
and also this line in the main function. FindPath is called:
g.FindPath(Word {1, 0, 1}, Word {1, 1}, nil, nil)
fmt.Println(g.Pathes [0]) // this one
And if you run it, you will get this output:
[{[1 0 1] [0]} {[0] [1]} {[1] [1 0]} {[1 0] [1 1]}] // .FindPath, right one
[{[1 0 1] [0]} {[0] [1]} {[1] [1 0]} {[1 0] [1 1 1]}] // main, false
What am I doing wrong? Why can one and the same object change it`s value? How shoud I fix it?
I can post more code (all of it), but i belive, that the problem is here.
Thank you in advance.