The SP query is defined as follows: The SP query uses a bidirectional breadth-first search algorithm that finds the shortest path between a target node and Substance nodes. Notice the shortestPath() call in the second, blue colored part of the query. The time complexity of bi-directional BFS is of O(b d/2 ), which is much faster than typically BFS .