Searching from the answer backward

Graphs

Searching from the answer backward

When asking the question from each cell is expensive, ask the opposite question from the edges.

Key idea

The expensive question

Some grid problems ask, for every cell, whether it can reach some destination. Running a search from each cell answers it and costs a full traversal per cell.

The trick is that reachability questions can usually be turned around. Instead of asking which cells can reach the edge, ask which cells the edge can reach, searching backward along the same connections.

One search from the destinations answers the question for every cell at once.

Why it works

Reverse the movement rule too

If moving forward requires going downhill, then searching backward requires going uphill. The rule inverts along with the direction.

Getting that inversion wrong produces a search that runs and returns confidently wrong answers, which is much harder to spot than a crash. Write the forward rule down, then write its reverse explicitly, before coding.

On a real cell the two rules pick out different neighbors, and seeing them side by side is the cheapest way to confirm you have inverted it rather than copied it.

The two lists are disjoint here, which is the point: forward and backward are not the same walk with the arrows renamed.

Key idea

Two destinations means two searches

When cells must reach both of two destinations, run one search per destination and intersect the results. Each search is linear, so two of them are still linear.

Trying to answer both in one traversal is possible with two flags per cell and is fiddlier for no gain. Two sets and an intersection is clearer and the same complexity.

Key idea

The same idea for marking what to keep

A related shape: some regions are protected because they touch the border and the rest are not. Rather than testing each region for border contact, search from the border and mark everything it reaches as protected.

Then a single sweep converts everything unmarked. Two passes, both linear, and no per-region bookkeeping at all. Recognizing that this is the same reversal as the reachability version is the point.

Gotcha

Do the conversion after the marking, not during

Converting cells while the search is still running changes the data the search is reading. Mark first, sweep second, and keep the two phases strictly separate.

Using a third temporary value to mean protected, then converting in the final sweep, is the usual way to keep them apart. Mixing them produces results that depend on traversal order, which is the worst kind of bug to reproduce.

Which rule does the backward search use?

Water flows from a cell to a neighbor whose height is equal or lower. You want every cell from which water can reach the border. Searching outward from the border, what should the neighbor condition be?

Pacific Atlantic Water Flow

Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.

Loading the workspace…

Surrounded Regions

Read the constraints first and let them tell you what complexity is expected. Derive the approach, implement it, run the tests, and submit when it passes.

Loading the workspace…
← Previous