Power Grid
Problem
A grid of light bulbs is formed. Then, each light bulb is powered on with probability . Find the probability that no two adjacent (grid cells that share a common side) light bulbs are powered on.
Original Problem Link: https://www.quantguide.io/questions/power-grid
Solution
Brute-forcing over all on/off configurations of the grid is the naive plan, and it does technically work, but there's a much smarter way in once we notice something: whether a configuration is valid only ever depends on pairs of adjacent rows, never on rows further apart. That locality is exactly what makes a row-by-row DP click.
Step 1: Treat a row as a "state"
Each row has bulbs, each on or off, so a row can be described by one of on/off patterns. Call each pattern a state. Write a state like to mean bulb and are off, bulb is on.
But not all states can ever legally appear as a row by themselves: the "no two adjacent bulbs" rule also applies within a row (bulbs – and – are side-by-side). That rules out any state with two consecutive bulbs both on: , , and are all immediately invalid, leaving exactly legal single-row states:
Step 2: When can two rows sit on top of each other?
Two vertically stacked rows are compatible exactly when no column has both rows' bulbs on, i.e. their on-positions don't overlap at all. Take the state : whatever sits directly above or below it must have bulb off (else that shared column has two adjacent bulbs both lit). Checking it against all legal states, the compatible ones are
three options, all with bulb off (the two remaining legal states, itself and , both keep bulb on, so they're excluded).
Running this same check against the other legal states builds the full compatibility map. A few more of them, to see the pattern:
- : all legal states, since an all-off row conflicts with nothing.
- : anything with bulb off, options.
- : anything with bulb off, options.
- : needs both bulb and bulb off, only options.
That last one is the most restrictive, which makes sense: has the most bulbs lit ( of the ), so it leaves the least room for a neighboring row.
Step 3: Roll the rows up via DP
Now define number of valid ways to fill the first rows, given that row is in state . The first row has no row below it to conflict with, so
Every subsequent row only needs to look at the row directly beneath it (that's the whole point of building the compatibility map in Step 2), so
Rolling this forward for rows (using the grid's top-bottom symmetry: it doesn't matter which side we call "row 1") and summing over all legal gives the total count of valid grids:
Step 4: Convert to a probability
All on/off assignments to the bulbs are equally likely (each bulb is on independently with probability ), and of them are valid. So
Why this generalizes
Nothing above was really specific to a grid. For an grid, encode each row as one of raw states (of which some smaller number are internally legal), build the compatibility map exactly as in Step 2, and run the same DP for rows. Each new row only ever needs the row directly below it, and that's the entire mechanism, so the whole computation costs about : rows, and up to compatibility checks summed per row. A DP like this is really nothing more than a rule for mapping states to states; once that mapping is written down, the rest is bookkeeping.
There's a small optimization worth naming, too. Nothing in the setup actually forces rows to be the "line" and columns to be the "steps." A grid graph has no preferred orientation, so we could just as easily encode each column as a state (of bulbs, so legal states) and step across the columns instead, giving cost , by the exact same argument with rows and columns swapped, WLOG.
Since (the count of legal single-line states, no two adjacent bulbs on) grows exponentially in the line length (it's a Fibonacci-type count, and in fact is exactly what Step 1 found), that squared term dominates. So it's always worth picking whichever orientation keeps the state dimension smaller, even if that means taking more steps along the other one. The true best-case complexity is
and for a long, narrow grid (say , ), that difference is enormous: sweeping across the long direction with only states per step is vastly cheaper than the reverse.