# Monotone submodular maximization

> Use a heap to make greedy selections with fewer gain evaluations, then compare the results with a full scan.

Pick a fixed number of items from a large pool to maximize a set function `f`. If `f` is monotone — adding an item never hurts — and submodular — the marginal gain of an item can only shrink as the set grows — then greedy achieves at least `1 − 1/e` (about 63.2%) of the optimal value for a normalized objective (Nemhauser, Wolsey, and Fisher, 1978).

When evaluating a gain is expensive, a heap of cached upper bounds can save most of those evaluations while keeping greedy’s guarantee. On the advertising benchmark below, lazy greedy needed 576 evaluations where a full scan needed 22,500.

## The three methods

| Method                     | Gain evaluations                                           | Guarantee                    | Structure it needs                |
| -------------------------- | ---------------------------------------------------------- | ---------------------------- | --------------------------------- |
| Full-scan greedy           | up to `k · n`, scanning every remaining candidate per pick | `1 − 1/e`                    | none                              |
| Lazy greedy (Minoux, 1978) | `n +` re-evaluations of stale roots                        | `1 − 1/e` (unchanged)        | a max-heap of cached upper bounds |
| Stochastic greedy          | `(n/k) · log(1/ε)` per step                                | `1 − 1/e − ε` in expectation | an injected random source         |

Lazy greedy works because of submodularity itself: a cached marginal gain is an **upper bound** on the true gain now, so the heap’s root is an upper bound on the best remaining choice. Re-evaluate the root. If the fresh value still beats every other cached value, it is provably the maximum, so take it. If it dropped, put it back with its smaller value and repeat. A candidate with a fresh gain below another candidate’s cached bound may be revisited before the next pick.

time O(nc + r(c + log n)), where r counts heap iterations after initialization · space O(n), one cached gain per candidate · c is the cost of one marginal-gain evaluation

When the heap pays off

Lazy greedy adds heap maintenance. It helps when bounds stay tight enough to skip many gain evaluations, especially when each evaluation is expensive.

## What it looks like in Knotkit

Use `maxBinaryHeap` from `knotkit/heap` to keep the candidate with the highest upper bound at the root.

```ts
import { mapInputOrder, maxBinaryHeap, numberOrder } from 'knotkit/heap'

// Each candidate reaches some users; pick the two that reach the most users together.
const audiences = [
  ['ada', 'grace', 'alan'],
  ['grace', 'alan'],
  ['edsger', 'barbara'],
  ['ada', 'edsger'],
]

const reached = new Set<string>()
const gainOf = (index: number): number => (audiences[index] ?? []).filter((user) => !reached.has(user)).length

interface Candidate {
  readonly index: number
  /** The gain when last computed: an upper bound, because gains only shrink as the set grows. */
  readonly gain: number
}

const byGain = mapInputOrder(numberOrder, (candidate: Candidate) => candidate.gain)
const frontier = maxBinaryHeap(
  byGain,
  audiences.map((_, index) => ({ index, gain: gainOf(index) })),
)

const picks: number[] = []

while (picks.length < 2) {
  const top = frontier.peek()
  const fresh = gainOf(top.index)

  if (fresh < top.gain) {
    frontier.replaceRoot({ ...top, gain: fresh }) // stale bound: lower it and look again
    continue
  }

  frontier.pop()
  picks.push(top.index)

  for (const user of audiences[top.index] ?? []) {
    reached.add(user)
  }
}

picks // [0, 2]
reached.size // 5
```

The same snippet runs, with every stated result checked, on the guide [Pick the k items that cover the most](/guides/budgeted-coverage/).

`replaceRoot` installs the updated candidate with a single sift down. `mapInputOrder` tells the heap to compare candidates by their cached gain.

Stochastic greedy instead draws a uniform sample of candidates per step, which is where `knotkit/sampling` and an injected `knotkit/prng` source enter: the sample is reproducible from the seed, so a reported result is replayable.

## The measured workload

[`problems/budget-allocation`](https://github.com/jkomyno/knotkit/blob/main/problems/budget-allocation/README.md) measures all three against the exact optimum on a DR-submodular lattice objective — spreading `r` units of budget across advertising channels to maximize expected reach:

* At budget 75 on the `low-overlap` instance, **lazy greedy needs 576 gain evaluations against full-scan greedy’s 22,500**, and takes **0.267 ms against 4.274 ms**.
* At 16 units, lazy greedy matches the certified optimum at every budget, and takes **27.2 µs** where branch and bound takes **733 ms** to certify that optimum.

Those are the repository’s numbers for one fixture on one machine. The address is the problem README’s [Benchmarks](https://github.com/jkomyno/knotkit/blob/main/problems/budget-allocation/README.md#benchmarks) section; the figure is [`reach-and-time.png`](https://github.com/jkomyno/knotkit/blob/main/problems/budget-allocation/figures/reach-and-time.png).

The same workload found the limit of the structure: the heap holds one entry per channel, so **heap arity and graph layout barely matter** there. A lazy heap pays off when the pool is large — one cached bound per candidate — not when the number of candidates is small enough that a scan is free.

![Left: reach below the certified best at each even budget. Lazy greedy is 0% short at every budget; stochastic greedy is at most 3.69% short, at 6 units. Right: at 16 units, lazy greedy takes 27.2 microseconds, while branch and bound takes 733 milliseconds to certify the optimum.](/figures/problems/budget-allocation/reach-and-time.png)

Quality on the left, cost on the right. Lazy greedy pays nothing for skipping evaluations; stochastic greedy trades at most 3.69% of reach for a bounded sample per step.
