Skip to the workspace
Pebellslearn by building
Shortest Paths at Scale  ›  Lesson

Searching From Both Ends

Bidirectional Search 28 minTwo small circles are cheaper than one big one

dsa / Searching From Both Ends

Explore each scene at your own pace.

01 · Why two halves beat one whole

Why two halves beat one whole

Searching From Both Ends

Definition
Bidirectional search runs one search forward from the start and another backward from the goal, and stops when they meet. Each search only has to go half as deep, so far fewer nodes are explored, though with weighted edges the first meeting point is not always on the cheapest path.
In plain words
Two people tunnelling towards each other under a mountain meet in the middle after each digging only half the distance. That is far less rock than one person digging the whole way.

In short: running two searches at once — one forward from the start, one backward from the goal — and stopping when they meet cuts the explored region from about b^d junctions to about 2·b^(d/2), a square root, not a halving. With equal edge weights the first meeting point is already the answer. With different weights, run two Dijkstras and record every crossing between the two settled regions, stopping only when the two heaps' cheapest remaining entries add up to at least the best crossing found — the first touch is not always the cheapest route.

Two overlapping circles, forward (green, from depot) and backward (red, from harbour), with a grey dot labelled market: first touch at cost 6, and a green dot lower down labelled canal-ridge: cheapest at cost 5

The slowest queries Meridian answers are the cross-city ones: a pickup on the east ring road, a drop-off on the far west. A search from the pickup spreads out in rings, and by the time a ring reaches the drop-off it has swept up most of the city.

Here's a better idea. Start a second search from the drop-off at the same time, and stop when the two meet in the middle. Two small circles instead of one big one. On a network where each junction branches out, that is not a small saving, and this lesson shows how big it is. It also shows the one detail that makes the weighted version subtle: the moment the searches first touch is not always the moment you have the answer.

Why two halves beat one whole

Suppose every junction leads to about b new junctions, and the route is d roads long. A search from one end explores roughly b to the power d junctions before it arrives. Two searches that each go halfway explore about 2 × b to the power d/2:

bdversus2 bd/2b^{d} \quad\text{versus}\quad 2\,b^{d/2}

That's a square root, not a halving:

What does this print?
b, d = 3, 8
print(b ** d, 2 * b ** (d // 2))

Here's the difference as the route gets longer, when each junction leads to 3 new ones:

route length d one search (3^d) two searches (2 × 3^(d/2)) ratio
4 81 18 4.5
8 6,561 162 40.5
14 4,782,969 4,374 1,093.5
20 3,486,784,401 118,098 29,524.5

The ratio is 3 to the d/2, over 2, so every two extra roads triple it, and at 14 roads it passes a thousand. City routes of 30 or 40 roads make the one-sided search millions of times more expensive, at least until the network's edges stop the frontier growing.

Scene 1 of 9
Project context and further resources
You're building a piece ofMeridian — the router
This piece — meet_in_middle(): Searches from the pickup and the drop-off at once and proves when they've met on the cheapest route.
Scenario Cross-city queries are the slow ones: the pickup and the drop-off are far apart, and a search from one end sweeps up half the city before it arrives. Meridian searches from both ends at once and stops the moment it can prove the two halves have met on the cheapest route.
meet_in_middle.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
meet_in_middle(roads, start, goal) → intThe cheapest cost between two junctions, found by searching from both ends; -1 if unreachable.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.