I am trying to implement BFS algorithm for finding all paths (from src and dest) in a graph. I am using a slice to simulate a queue, but the slice gets corrupted (the append didn't work as expected) when I append to it more than one elements in the for cycle. I have no idea why. I am new in GoLand
// GetPathsFromCache retrieve information from loaded jsons in the cache
func (cache *ModelsDataCache) GetPathsFromCache(modelUrn, selectedElement, targetType, authToken string, modelJSONs *ModelJSONs) []systemdetection.GraphPath {
result := make([]systemdetection.GraphPath, 0)
//create the queue which stores the paths
q := make([]Path, 0)
//Path to store the current path
var currentPath Path
currentPath.path = append(currentPath.path, selectedElement)
q = append(q, currentPath)
for len(q) > 0 {
currentPath = q[0] //get top
q = q[1:]
lastInThePath := currentPath.path[len(currentPath.path)-1]
connectedToTheCurrent := cache.GetConnectionsFromCache(modelUrn, lastInThePath, authToken, modelJSONs)
lastInPathType := modelJSONs.Elements[lastInThePath].Type
if lastInPathType == targetType {
//cache.savePath(currentPath, &result)
var newPath systemdetection.GraphPath
newPath.Target = lastInThePath
newPath.Path = currentPath.path[:]
newPath.Distance = 666
result = append(result, newPath)
}
for _, connected := range connectedToTheCurrent {
if cache.isNotVisited(connected, currentPath.path) {
var newPathN Path
newPathN.path = currentPath.path
newPathN.path = append(newPathN.path, connected)
q = append(q, newPathN)
}
}
}
return result
}