// Query find shortest path between source and target
// on the BFS searched graph.
// return depth and path if succeed.
Query find shortest path between source and target
on the BFS searched graph.
return depth and path if succeed.
// Query find shortest path between source and target // on the BFS searched graph . // return depth and path if succeed .
func (b *Bfs) Query(target graph.NodeID) (int, graph.Path, error) {
	currNodeAttr, targetInNodes := b.nodesAttr[target]
	if !targetInNodes {
		panic(fmt.Errorf("target node %v not in the graph", target))
	}
	if target != b.Source && currNodeAttr.parent == graph.InvalidNodeID {
		return 0, nil, fmt.Errorf("no valid path from %v to %v", b.Source, target)
	}
	depth := currNodeAttr.depth
	shortestPath := []graph.NodeID{}
	shortestPath = append(shortestPath, target)
	for currNodeAttr.parent != graph.InvalidNodeID {
		currNode := currNodeAttr.parent
		currNodeAttr = b.nodesAttr[currNode]
		shortestPath = append(shortestPath, currNode)
	}
	for i, j := 0, len(shortestPath)-1; i < j; i, j = i+1, j-1 {
		shortestPath[i], shortestPath[j] = shortestPath[j], shortestPath[i]
	}
	return depth, shortestPath, nil
}
func ( b * Bfs ) Query ( target graph . NodeID ) ( int , graph . Path , error ) { currNodeAttr , targetInNodes := b . nodesAttr [ target ] if ! targetInNodes { panic ( fmt . Errorf ( " " , target ) ) } if target != b . Source && currNodeAttr . parent == graph . InvalidNodeID { return 0 , nil , fmt . Errorf ( " " , b . Source , target ) } depth := currNodeAttr . depth shortestPath := [ ] graph . NodeID { } shortestPath = append ( shortestPath , target ) for currNodeAttr . parent != graph . InvalidNodeID { currNode := currNodeAttr . parent currNodeAttr = b . nodesAttr [ currNode ] shortestPath = append ( shortestPath , currNode ) } for i , j := 0 , len ( shortestPath ) - 1 ; i < j ; i , j = i + 1 , j - 1 { shortestPath [ i ] , shortestPath [ j ] = shortestPath [ j ] , shortestPath [ i ] } return depth , shortestPath , nil }
Query find shortest path between source and target
on the BFS searched graph.
Query find shortest path between source and target on the BFS searched graph .