Hello! There is a graph in which the nodes are words (type Word ), and the edges are substitutions that turn one word into another.
For example: between nodes 123 and 333 there is an edge 12->33 .

There is a method that searches all the paths from one word to another and stores them in this.Path :

 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 this method finishes, this.Path will have both the right and the wrong ways. Add this line to main :

 fmt.Println(g.Pathes[0]) 

and compare with the output of 4 lines of the FindPath method. We get:

 [{[1 0 1] [0]} {[0] [1]} {[1] [1 0]} {[1 0] [1 1]}] // FindPath, правильный путь [{[1 0 1] [0]} {[0] [1]} {[1] [1 0]} {[1 0] [1 1 1]}] // main 

Could you help me? I can post more code, but it seems to me that the error is here.

    1 answer 1

     this.FindPath(from.ApplyRule(this.rules[r]), to, append(visited, from), append(current, this.rules[r])) 

    Here the append is executed in the loop. Suppose that, at first, there was something in one slice: (1) [a, b, c] . Then they added something to it: (2) [a, b, c, d] . Then they tried to add something else: (3) [a, b, c, e] . However, the slice in the second step and the third should be stored in the same place, so the first is replaced with the second.

    Need to do so:

     new := make(Type, len(old)) copy(new, old) append(new, element) 

    At the same time, I was not able to compose an example that would clearly illustrate this. I would be grateful if someone offers a program of ten lines from which it will definitely follow.
    Transfer