Bitcoin Coin Selection Algorithms: Branch and Bound, Knapsack, and Waste
A Bitcoin wallet does not spend part of an existing UTXO. Each selected input spends an entire unspent transaction output, and the transaction creates new outputs for the recipient and, when needed, change. Coin selection is therefore a constrained optimization problem rather than simple balance subtraction.
Inputs, outputs, and the selection target
For a transaction with selected input values I, recipient outputs R, change C, and miner fee F, the accounting identity is I = R + C + F. A wallet must select confirmed or otherwise spendable UTXOs under its own policy, estimate the signed transaction's virtual size, and choose a fee rate suitable for its confirmation objective.
Input types matter: spending a legacy input, a native SegWit input, and a Taproot key-path input can require different virtual sizes. An algorithm that optimizes only face value may select an unnecessarily expensive set of inputs.
Branch and Bound (BnB)
Bitcoin Core's coin-selection research popularized Branch and Bound for searching subsets whose effective values can fund a payment without creating a change output. Effective value is approximately the UTXO value minus the estimated fee to spend that input at the current feerate. The search considers subsets and prunes branches that cannot satisfy the target.
A no-change solution can save the immediate cost of a change output and its future spend. But an exact-looking match is not automatically optimal: the wallet must compare excess value, input costs, search limits, and the value of retaining particular coins. Branch and Bound is not guaranteed to find a suitable subset within a bounded search.
Knapsack and randomized selection
Knapsack-style approaches search for a collection that covers the target, sometimes using stochastic trials. They can find useful solutions when a change-free subset is unavailable, but may produce change and can have variable results. Modern wallet implementations may combine several candidate-selection strategies and score their outcomes instead of relying on one universal algorithm.
Waste and long-term costs
A useful selection metric considers immediate input fees, the future cost of spending a change output, and the difference between current and expected long-term feerates. Spending a large number of small UTXOs during high congestion may be expensive; consolidating them during lower feerates can reduce later input counts, but consolidation also reveals common ownership and may reduce privacy.
Privacy and operational constraints
Wallets may avoid mixing coins associated with different identities, prefer certain confirmation depths, exclude frozen UTXOs, or enforce spending limits. These constraints can override a purely fee-minimizing selection. A wallet should also prevent concurrent transactions from reserving the same UTXO unless it intentionally supports replacement or conflict handling.
Developer checklist
Estimate input weights using the actual script types and signing path. Recalculate the final fee after building and signing. Treat dust and change as policy decisions, not fixed global constants. Test exact-match payments, insufficient funds after fees, many tiny inputs, mixed script types, and concurrent spending attempts.
Primary references
Bitcoin Core coin selection documentation and implementation: https://github.com/bitcoin/bitcoin/tree/master/src/wallet
Bitcoin Core PR #17331, coin selection improvements: https://github.com/bitcoin/bitcoin/pull/17331
Bitcoin developer transaction guide: https://developer.bitcoin.org/devguide/transactions.html
