Bidirectional search
algorithm for finding shortest paths by simultaneously searching from the source and destination until both searches meet From Wikipedia, the free encyclopedia
Remove ads
In computer science, bidirectional search is a method used for finding the shortest path between two items in a graph. It runs two simultaneous searches starting from the two items.[1]
Implementation
The example below uses two simultaneous breadth-first searches.
void bidirectionalSearch(Item a, Item b) {
Queue qA = new Queue();
Queue qB = new Queue();
qA.enqueue(a);
qB.enqueue(b);
while (!qA.isEmpty() && !qB.isEmpty()) {
if (!qA.isEmpty()) {
Item v = qA.dequeue();
if (aItem.found) return true;
for (Item neighbor : v.neighbors()) {
if (!neighbor.found) {
neighbor.found = true;
qA.enqueue(neighbor);
}
}
}
if (!qB.isEmpty()) {
Item v = qB.dequeue();
if (v.found) return true;
for (Item neighbor : v.neighbors()) {
if (!neighbor.found) {
neighbor.found = true;
qB.enqueue(neighbor);
}
}
}
}
return false;
}
Remove ads
Related pages
References
Wikiwand - on
Seamless Wikipedia browsing. On steroids.
Remove ads