예제 #1
0
func getCandidates(c *models.City, origin, dest *models.Node, visited []int) []models.Path {
	if c.LastError != nil {
		return nil
	}
	vlen := len(visited)
	var paths = make([]models.Path, 0)
	for i := 0; i < len(origin.Outputs); i++ {
		if origin.Outputs[i].DestinyID == dest.ID {
			paths = append(paths, models.Path{Reached: true, Links: []models.Link{origin.Outputs[i]}})
			continue
		}
		if alreadyVisited(origin.Outputs[i].DestinyID, visited) {
			continue
		}
		visited = append(visited, origin.Outputs[i].DestinyID)
		subPaths := getCandidates(c, c.GetNode(origin.Outputs[i].DestinyID), dest, visited)
		for j := 0; j < len(subPaths); j++ {
			lnks := subPaths[j].Links
			lnks = append(lnks, origin.Outputs[i])
			paths = append(paths, models.Path{Links: lnks, Reached: subPaths[j].Reached})
		}
	}
	if vlen == 0 && len(paths) == 0 {
		c.LastError = fmt.Errorf("There's no way to the requested address")
	}
	return paths
}
예제 #2
0
func GetPaths(c *models.City, origin, destiny int) ([]models.Path, error) {
	if origin == destiny {
		return nil, fmt.Errorf("Already at destiny")
	}
	org := c.GetNode(origin)
	dest := c.GetNode(destiny)
	candidates := getCandidates(c, org, dest, nil)
	return sortLinks(candidates), c.LastError
}