# Budget allocation

> Spread a budget of r units across advertising channels so that the expected number of customers reached is as large as possible. Each unit on channel `s` reaches customer `t` independently with probability `p_st`. The objective `f(x) = Σ_t 1 − Π_s (1 − p_st)^\{x_s\}` is monotone and DR-submodular on the integer lattice: a channel's next unit is worth less as more units are spent, but a second unit on one channel can still be the best move. This private workload compares full-scan greedy, lazy greedy over an injected `knotkit/heap` max-heap, and stochastic greedy over injected `knotkit/prng` randomness. It checks them against the exact optimum, enumerated on small instances, and measures how far they are from optimal with an upper bound from the objective's concave relaxation and a branch-and-bound search built on it.

Spread a budget of r units across advertising channels so that the expected number of customers reached is as large as possible. Each unit on channel `s` reaches customer `t` independently with probability `p_st`. The objective `f(x) = Σ_t 1 − Π_s (1 − p_st)^\{x_s\}` is monotone and DR-submodular on the integer lattice: a channel’s next unit is worth less as more units are spent, but a second unit on one channel can still be the best move. This private workload compares full-scan greedy, lazy greedy over an injected `knotkit/heap` max-heap, and stochastic greedy over injected `knotkit/prng` randomness. It checks them against the exact optimum, enumerated on small instances, and measures how far they are from optimal with an upper bound from the objective’s concave relaxation and a branch-and-bound search built on it.

## Result

> Lazy greedy needs 576 gain evaluations against full-scan greedy’s 22,500 at budget 75 on low-overlap (0.267 ms against 4.274 ms)

## Modules

* [`knotkit/heap`](/modules/heap/): Provides binary and k-ary min/max heaps plus indexed priority queues, ordered by caller-supplied comparators
* [`knotkit/prng`](/modules/prng/): Generates seeded pseudorandom numbers and unbiased integers for reproducible simulations and tests
* [`knotkit/graph`](/modules/graph/): Provides breadth-first search, Dijkstra, topological order, and connected components over integer-vertex graphs, stored as adjacency lists, compressed sparse rows, or an adjacency matrix
* [`knotkit/sampling`](/modules/sampling/): Draws unbiased subsets, pairs, shuffles, and weighted samples over injected randomness

## Figures

* Same best reach (59.37 expected customers), about 52,000× faster with lazy greedy.
* There are 1,384,623 ways to spend the full budget.
* Finding the best split is NP-hard. Branch and bound can take exponential time.

Hover or focus a chart for exact values. Drawn by [`demo.ts`](https://github.com/jkomyno/knotkit/blob/main/problems/budget-allocation/demo.ts) at build time, from the fixtures the benchmark uses; it reproduces the README figure.

## Reproduce

```bash
git clone https://github.com/jkomyno/knotkit.git && cd knotkit && bun install
bun run demo:problem:budget-allocation
```

[`problems/budget-allocation/README.md`](https://github.com/jkomyno/knotkit/blob/main/problems/budget-allocation/README.md) states the fixture, the seed, and the result table.
