Bidirectional Search
Prerequisites
- BFS — Understanding of frontier-based level-by-level expansion
- State Space — Node representation, frontier, explored set
- UCS — Priority-queue expansion for variable step costs
🎯 Learning Objectives
- Understand the meet-in-the-middle idea and why it dramatically reduces search space
- Implement bidirectional BFS using two simultaneous frontiers
- Analyse time and space complexity: O(bd/2)
- Identify prerequisites for applying bidirectional search (reversible operators, known goal state)
- Recognise limitations and GATE-style questions involving bidirectional search
1. Core Intuition: Meet in the Middle
Standard BFS from the start state S explores all nodes up to depth d, generating up to O(bd) nodes. Bidirectional search runs two simultaneous BFS instances:
- Forward search — expands from the start state S.
- Backward search — expands from the goal state G, following edges in reverse.
Each search only needs to reach depth d/2 before the two frontiers intersect. Since each half generates O(bd/2) nodes, the total work is:
2 × O(bd/2) = O(bd/2)
💡 Why the Savings Are Enormous
Consider b = 10 and d = 6. Standard BFS explores up to 106 = 1,000,000 nodes. Bidirectional search explores 2 × 103 = 2,000 nodes — a 500× reduction.
Visually, imagine two circles expanding from S and G respectively. The area of each circle is proportional to bd/2. Instead of one giant circle of area bd, we have two small circles whose combined area is far less.
2. Requirements for Bidirectional Search
Bidirectional search is not always applicable. It requires:
| Requirement | Explanation |
|---|---|
| Explicit goal state | The goal must be a specific state (e.g., a city), not just a goal test function (e.g., “any state with checkmate”). If the goal is described only by a predicate, we cannot start a backward search from it. |
| Reversible operators | We must be able to generate predecessors of a state, i.e., reverse the actions. In a road map this is trivial (drive both ways). In some problems (e.g., irreversible chemical reactions) predecessors may be hard to compute. |
| Efficient intersection check | At every expansion, we must check whether a newly generated node already appears in the other search’s frontier or explored set. Using a hash set, this is O(1) per check. |
3. Bidirectional BFS Algorithm
The most common variant uses BFS in both directions. We maintain:
frontier_Fandexplored_Ffor the forward search.frontier_Bandexplored_Bfor the backward search.
Pseudocode: Bidirectional-BFS
function BIDIRECTIONAL-BFS(problem):
node_s ← MAKE-NODE(problem.initial_state)
node_g ← MAKE-NODE(problem.goal_state)
if problem.initial_state = problem.goal_state:
return SOLUTION(node_s)
frontier_F ← FIFO-QUEUE(node_s)
frontier_B ← FIFO-QUEUE(node_g)
explored_F ← {problem.initial_state}
explored_B ← {problem.goal_state}
while frontier_F is not empty AND frontier_B is not empty:
// ---- Forward step ----
node ← POP(frontier_F)
for each child in EXPAND(node, problem):
state ← child.state
if state in explored_B OR state in frontier_B:
return JOIN-SOLUTIONS(child, backward_node(state))
if state not in explored_F:
explored_F ← explored_F ∪ {state}
ADD(frontier_F, child)
// ---- Backward step ----
node ← POP(frontier_B)
for each child in EXPAND-BACKWARD(node, problem):
state ← child.state
if state in explored_F OR state in frontier_F:
return JOIN-SOLUTIONS(forward_node(state), child)
if state not in explored_B:
explored_B ← explored_B ∪ {state}
ADD(frontier_B, child)
return FAILURE
Key Implementation Details
- EXPAND-BACKWARD: generates predecessors by applying inverse operators. For a graph with an adjacency list, this means iterating over incoming edges instead of outgoing edges.
- JOIN-SOLUTIONS: concatenates the path from S to the meeting node (from the forward search) with the reversed path from G to the meeting node (from the backward search).
- The intersection check —
state in explored_B— must be O(1); use a hash table (Pythonset, JavaHashSet). - We alternate one expansion step forward, one backward. Some implementations expand the smaller frontier first to balance memory.
4. Step-by-Step Trace
Consider the following undirected graph with unit-cost edges:
Example Graph
A --- B --- C
| |
D --- E --- F
Start: A, Goal: F. Solution depth d = 3 (A→B→C→F or A→D→E→F).
| Step | Action | Frontier_F | Frontier_B | Explored_F | Explored_B | Intersection? |
|---|---|---|---|---|---|---|
| Init | — | [A] | [F] | {A} | {F} | No |
| 1 (Fwd) | Expand A → B, D | [B, D] | [F] | {A, B, D} | {F} | No |
| 2 (Bwd) | Expand F → C, E | [B, D] | [C, E] | {A, B, D} | {F, C, E} | No |
| 3 (Fwd) | Expand B → A(skip), C | [D, C] | [C, E] | {A, B, D, C} | {F, C, E} | Yes! C ∈ explored_B |
Meeting point: C. Forward path: A→B→C. Backward path: F→C (reversed: C→F). Joined solution: A → B → C → F (cost = 3).
💡 Nodes Explored
Bidirectional BFS explored {A, B, D} forward and {F, C, E} backward = 6 nodes total. Standard BFS from A would explore A, B, D, C, E, F — also 6 in this small example, but the asymptotic savings are massive for larger graphs.
5. Properties of Bidirectional Search
| Property | Value | Notes |
|---|---|---|
| Complete? | Yes | If both directions use BFS (b is finite) and graph search to avoid revisits. |
| Optimal? | Yes (with BFS + unit costs) | Both frontiers expand level by level; the meeting point lies on a shortest path. |
| Time | O(bd/2) | Each direction explores at most depth d/2. |
| Space | O(bd/2) | Must store both frontiers and explored sets in memory. |
| Data Structure | Two FIFO queues + two hash sets | Hash sets for O(1) intersection checking. |
5.1 Completeness Proof Sketch
If a solution of depth d exists, the forward BFS will reach depth ⌈d/2⌉ and the backward BFS will reach depth ⌊d/2⌋ (or vice versa). At that point, some node on the optimal path must be in both explored sets, triggering an intersection. Since BFS is complete when b is finite, each direction is guaranteed to reach its half of the path.
5.2 Optimality Analysis
When both directions use BFS with unit step costs, each direction expands nodes in order of their distance from the respective origin. The first intersection found is at the midpoint of a shortest path, so the total path length is optimal.
⚠ Non-Unit Costs
With variable step costs, bidirectional BFS is not guaranteed to be optimal. The first meeting point might not lie on the cheapest path. You would need Bidirectional UCS with a more careful termination condition (checking that no shorter path can still be discovered).
6. Complexity Deep Dive
6.1 Why O(bd/2) Instead of O(bd)?
BFS generates all nodes up to depth k, totalling approximately bk nodes (dominant term). In bidirectional search, each direction goes to depth d/2:
Total nodes ≈ bd/2 (forward) + bd/2 (backward) = 2 · bd/2 = O(bd/2)
6.2 Numerical Comparison
| b | d | BFS: bd | Bidirectional: 2·bd/2 | Speedup |
|---|---|---|---|---|
| 2 | 20 | 1,048,576 | 2,048 | 512× |
| 10 | 6 | 1,000,000 | 2,000 | 500× |
| 10 | 10 | 10,000,000,000 | 200,000 | 50,000× |
| 4 | 12 | 16,777,216 | 8,192 | 2,048× |
The savings grow exponentially with d. This is the primary motivation for bidirectional search.
6.3 Space Requirement
Both directions must keep their frontiers and explored sets in memory simultaneously. The total space is O(bd/2) — much better than BFS’s O(bd), but still exponential. For extremely large state spaces, even bidirectional search may exhaust memory.
7. Checking Frontier Intersection
The critical operation is determining when the two searches have overlapping states. There are two strategies:
7.1 Check Against Explored Set
After generating a child node in one direction, check if its state appears in the other direction’s explored set. This is O(1) with a hash set. This is the most common approach.
7.2 Check Against Frontier
Also check the other direction’s frontier. This catches intersections one step earlier but requires the frontier to support O(1) membership testing (e.g., augment the queue with a hash set).
💡 Best Practice
Maintain a hash set shadow copy of each frontier for O(1) lookups. When a node is removed from the frontier queue, also remove it from the shadow set. This avoids O(n) scans of the queue.
7.3 Handling Multiple Meeting Points
In some graphs, multiple nodes may appear in both explored sets simultaneously (especially if we expand an entire BFS level at once). We must select the meeting point that yields the shortest total path. With unit costs and level-by-level expansion, any first meeting point is optimal.
8. Limitations
- Predecessor generation: In some domains, computing which states lead to a given state is expensive or impossible (e.g., a puzzle where moves are irreversible).
- Multiple goal states: If there are many possible goal states (e.g., “any state where the board is clean”), backward search must start from all of them, potentially negating the savings.
- Variable costs: As noted earlier, bidirectional BFS is only optimal with unit costs. Bidirectional UCS exists but is more complex.
- Implementation complexity: Managing two simultaneous searches, intersection detection, and path reconstruction is significantly more complex than a single BFS.
- Memory: Still exponential (O(bd/2)), so it does not solve the fundamental memory problem of BFS-based approaches.
9. Worked Examples (GATE-Style)
Example 1: Node Count Calculation
Q: A search problem has branching factor b = 10 and solution depth d = 8. How many nodes does bidirectional BFS generate (approximately)?
A: Each direction searches to depth 4. Nodes per direction ≈ b4 = 10,000. Total ≈ 2 × 10,000 = 20,000 nodes.
Compare with standard BFS: 108 = 100,000,000 nodes. Speedup: 5,000×.
Example 2: When Bidirectional Search Fails
Q: Can bidirectional search be used to solve the 8-puzzle?
A: Yes. The 8-puzzle has reversible operators (sliding tiles in any direction) and a specific goal state. Predecessors are easy to compute (just apply the same move operators from the goal).
Example 3: Applicable or Not?
Q: A robot must navigate a grid to reach any cell marked as an “exit.” There are 50 exit cells. Is bidirectional search a good choice?
A: It can be applied by starting the backward search from all 50 exit cells simultaneously (add all to the initial backward frontier). However, this increases the backward frontier size and may reduce the efficiency advantage depending on d and b. For small numbers of goals, it is still beneficial.
Example 4: Detailed Trace — Romania Map
Q: Start: Arad, Goal: Bucharest. Selected edges (unit cost for simplicity): Arad–Sibiu, Arad–Timisoara, Sibiu–Fagaras, Sibiu–Rimnicu Vilcea, Fagaras–Bucharest, Rimnicu Vilcea–Pitesti, Pitesti–Bucharest, Timisoara–Lugoj.
- Init: F_fwd=[Arad], F_bwd=[Bucharest], E_fwd={Arad}, E_bwd={Bucharest}.
- Fwd: Expand Arad → Sibiu, Timisoara. E_fwd={Arad, Sibiu, Timisoara}. Check: neither in E_bwd.
- Bwd: Expand Bucharest → Fagaras, Pitesti. E_bwd={Bucharest, Fagaras, Pitesti}. Check: neither in E_fwd.
- Fwd: Expand Sibiu → Arad(skip), Fagaras, Rimnicu Vilcea. Fagaras ∈ E_bwd! → Intersection found!
Forward path: Arad→Sibiu→Fagaras. Backward path: Bucharest→Fagaras (reversed: Fagaras→Bucharest). Solution: Arad→Sibiu→Fagaras→Bucharest, length 3.
10. Variants and Extensions
- Bidirectional UCS: Uses priority queues ordered by g(n) in both directions. Requires a more careful stopping criterion to ensure optimality.
- Bidirectional A*: Combines bidirectional search with heuristics. The backward heuristic estimates cost from a node to the start. Requires consistent heuristics.
- Front-to-Front vs Front-to-End: “Front-to-Front” checks if a new node is in the other’s frontier; “Front-to-End” checks the explored set. Front-to-Front can be more efficient but harder to implement.
- MM (Meet in the Middle): A modern bidirectional algorithm that guarantees optimality with consistent heuristics and expands the side with the smaller f-value frontier.
Summary
- Bidirectional search runs two simultaneous BFS instances from start and goal.
- Time and space: O(bd/2) — an exponential improvement over standard BFS.
- Requires: explicit goal state, reversible operators, efficient intersection checking.
- Complete with BFS in both directions (finite b). Optimal with unit step costs.
- Limitations: predecessor generation, multiple goals, variable edge costs, implementation complexity.
- For GATE: know the complexity formula, when it applies, and its advantages over BFS.
References
- Russell & Norvig, AIMA, Chapter 3.4.6.
- Pohl, I. (1971). “Bi-directional Search.” Machine Intelligence, 6, 127–140.