How the northwest-corner rule fills a supply and demand grid

Commerce

The page takes two lists of integer units. When the totals match, it fills a grid from the northwest corner: each step assigns the smaller of the remaining supply and the remaining demand, then steps down or across. When the totals differ, it reports the integer gap and assigns nothing.

Each list has 2 to 4 integers, from 0 to 40. A zero line is allowed. The lists may have different lengths. No money is typed, and no place name is accepted.

The result appears here when the page script runs.

How it is computed

Method

Start at the first supply and the first demand. Assign the minimum of what remains on that row and what remains on that column. If the row is then empty, move to the next row. If the column is then empty, move to the next column. If both are empty, move both. Stop when every remaining line is exhausted. Each row then sums to its supply, and each column sums to its demand.

Assumptions

One commodity, in integer units, with no loss and no extra source. The order of lines is the order typed. A balanced total is the only permission to fill the grid. The corner rule is a feasible filling, not a cheapest filling, because no cost is defined.

Limits

No cost, route, or real volume is involved. An unequal total abstains, and no allocation is defined. The bounds are 2 to 4 lines and units from 0 to 40.

Reproducibility

The inputs are kept in the address bar, so the same link reruns the same calculation. The same integers produce the same grid in this engine.