Searching a graph is quite famous problem and have a lot of practical use. We have already discussed here how to search for a goal vertex starting from a source vertex using BFS. In normal graph search using BFS/DFS we begin our search in one direction usually from source vertex toward the goal vertex, but what if we start search form both direction simultaneously.
Bidirectional search is a graph search algorithm which find smallest path form source to goal vertex. It runs two simultaneous search –
- Forward search form source/initial vertex toward goal vertex
- Backward search form goal/target vertex toward source vertex
Bidirectional search replaces single search graph(which is likely to grow exponentially) with two smaller sub graphs – one starting from initial vertex and other starting from goal vertex. The search terminates when two graphs intersect.
Consider following simple example-
Suppose we want to find if there exists a path from vertex 0 to vertex 14. Here we can execute two searches, one from vertex 0 and other from vertex 14. When both forward and backward search meet at vertex 7, we know that we have found a path from node 0 to 14 and search can be terminated now. We can clearly see that we have successfully avoided unnecessary exploration.
Why bidirectional approach?
Because in many cases it is faster, it dramatically reduce the amount of required exploration.
Suppose if branching factor of tree is b and distance of goal vertex from source is d, then the normal BFS/DFS searching complexity would be . On the other hand, if we execute two search operation then the complexity would be for each search and total complexity would be which is far less than .
When to use bidirectional approach?
We can consider bidirectional approach when-
- Both initial and goal states are unique and completely defined.
- The branching factor is exactly the same in both directions.
- Completeness : Bidirectional search is complete if BFS is used in both searches.
- Optimality : It is optimal if BFS is used for search and paths have uniform cost.
- Time and Space Complexity : Time and space complexity is
Below is very simple implementation representing the concept of bidirectional search using BFS. This implementation considers undirected paths without any weight.
Path exist between 0 and 14 Intersection at: 7 *****Path***** 0 4 6 7 8 10 14
This article is contributed by Atul Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Best First Search (Informed Search)
- Iterative Deepening Search(IDS) or Iterative Deepening Depth First Search(IDDFS)
- Applications of Depth First Search
- Breadth First Search without using Queue
- Breadth First Search or BFS for a Graph
- Depth First Search or DFS for a Graph
- Top 10 Interview Questions on Depth First Search (DFS)
- Implementing Water Supply Problem using Breadth First Search
- Uniform-Cost Search (Dijkstra for large Graphs)
- Finding minimum vertex cover size of a graph using binary search
- Find if there is a path between two vertices in a directed graph | Set 2
- Print all possible paths in a DAG from vertex whose indegree is 0
- Check if a graph is Strongly, Unilaterally or Weakly connected
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row