// Query retrieve path from source to target based on a dfs tree/forest
Query retrieve path from source to target based on a dfs tree/forest
// Query retrieve path from source to target based on a dfs tree / forest
func (d *Dfs) Query(source, target graph.NodeID) (graph.Path, error) {
	path := graph.Path{}
	curr := target
	for {
		path = append(path, curr)
		parent := d.nodesAttr[curr].parent
		if parent == source {
			path = append(path, source)
			break
		} else if parent == graph.InvalidNodeID {
			return nil, fmt.Errorf("no valid path from %v to %v", source, target)
		}
		curr = parent
	}
	for i, j := 0, len(path)-1; i < j; i, j = i+1, j-1 {
		path[i], path[j] = path[j], path[i]
	}
	return path, nil
}
func ( d * Dfs ) Query ( source , target graph . NodeID ) ( graph . Path , error ) { path := graph . Path { } curr := target for { path = append ( path , curr ) parent := d . nodesAttr [ curr ] . parent if parent == source { path = append ( path , source ) break } else if parent == graph . InvalidNodeID { return nil , fmt . Errorf ( " " , source , target ) } curr = parent } for i , j := 0 , len ( path ) - 1 ; i < j ; i , j = i + 1 , j - 1 { path [ i ] , path [ j ] = path [ j ] , path [ i ] } return path , nil }
Query retrieve path from source to target based on a dfs tree/forest
Query retrieve path from source to target based on a dfs tree / forest