---
title: "Coin Selection Algorithm — definition | Nuri Glossary"
description: "The wallet logic that decides which coins to combine, trading off fees, privacy and future coin fragmentation."
lang: "en"
type: "glossary-term"
canonical: "https://nuri.com/glossary/coin-selection-algorithm"
---

# Coin Selection Algorithm

The wallet logic that decides which coins to combine, trading off fees, privacy and future coin fragmentation.

## Basically

Coin selection is how your wallet chooses which coins to spend. It aims to keep fees low, protect your privacy and keep your coins easy to use.

## Key Takeaways

- A coin selection algorithm chooses which UTXOs your wallet spends as transaction inputs. It balances fees, privacy and long-term UTXO set health.
- Bitcoin Core runs Branch and Bound, Knapsack, CoinGrinder and Single Random Draw. It picks the result with the lowest waste score and favours changeless transactions when possible.
- Layer 2 systems such as Spark avoid coin selection entirely. Transfers happen off-chain, so there are no inputs to select or change outputs to manage.

## What Is a Coin Selection Algorithm?

A coin selection algorithm decides which unspent transaction outputs your Bitcoin wallet uses in a new transaction. Bitcoin tracks balances as separate coins, called UTXOs, rather than one account balance. Your wallet must therefore combine inputs that cover the payment and fees.
The coins you choose affect costs and privacy. Too many inputs increase transaction weight and fees, while too few may require a change output that costs more now and later. Combining coins from different sources also links them on-chain through the common-input-ownership heuristic, so a good algorithm balances these trade-offs automatically.
Coin selection is a variant of the subset sum problem, which is NP-hard. Wallets use heuristic algorithms to find good solutions within practical time limits. These solutions are not always optimal.

## How It Works

Your wallet lists eligible UTXOs, runs one or more selection algorithms, compares the results with a scoring function and builds the transaction with the winning inputs. Bitcoin Core, as of v27.0+, runs four algorithms. It selects the result with the lowest waste score.

## Branch and Bound (BnB)

Branch and Bound entered Bitcoin Core in v0.17.0 in October 2018 through PR #10637. It searches for an exact-match input set that removes the need for a change output. Mark Erhardt, also known as Murch, based the algorithm on his master's thesis at the Karlsruhe Institute of Technology.
BnB builds a binary decision tree, with each level including or excluding one UTXO. It sorts UTXOs by effective value, meaning face value minus the fee cost of spending that input, in descending order and then performs a depth-first search. It prunes branches that exceed the target range or cannot reach it with the remaining UTXOs, and explores up to 100,000 combinations before falling back to other methods.
The target range includes a tolerance window. BnB accepts a slight overpayment when inputs exceed the payment plus fees by less than the cost of creating and later spending a change output. This approach makes changeless transactions more common.

## Knapsack Solver

The Knapsack solver is Bitcoin Core's legacy algorithm and has been present since the earliest versions. It uses stochastic approximation across over 1,000 randomised iterations, including each UTXO with a 50% probability on a first pass and filling the gap with remaining UTXOs on a second pass. The iteration with the smallest excess over the target wins.
Knapsack almost always produces a change output. Its randomisation offers a privacy benefit because the same wallet builds different input sets for similar transactions. This variation makes fingerprinting harder.

## CoinGrinder

CoinGrinder entered Bitcoin Core in v27.0 in April 2024 through PR #27877 and searches for the input set with the minimum transaction weight. It only activates at elevated feerates, by default when the current feerate is at least three times the long-term consolidation feerate, typically 30+ sat/vB. Minimising input count saves money at high fees, but would grind the UTXO pool into smaller fragments at normal fees, so it remains dormant during low-fee periods.

## Single Random Draw (SRD)

Single Random Draw shipped in Bitcoin Core v23.0. It randomly shuffles all eligible UTXOs and greedily adds them until the target amount is reached. Simulations showed that SRD won roughly one-third of the time and outperformed the Knapsack solver in many scenarios, despite its simplicity.

## The Waste Metric

Bitcoin Core scores each candidate with the waste metric after all algorithms produce their results. The metric was introduced in PR #22009. The formula:
The timing cost penalises spending many inputs when fees are high, giving a positive value, and rewards consolidation when fees are low, giving a negative value. The long-term feerate defaults to 10 sat/vB and can be configured with the -consolidatefeerate setting. Bitcoin Core prefers the result that spends more inputs when waste scores are tied, consolidating the UTXO set opportunistically.

## Other Common Algorithms

Wallet implementations beyond Bitcoin Core use a variety of simpler strategies. These strategies vary between wallets.
The research article on Bitcoin fee market dynamics gives a deeper look at how input selection affects on-chain costs. It examines that relationship in more detail.

## Privacy Implications

Coin selection is one of the most significant factors in Bitcoin on-chain privacy. Chain analysis firms assume that all inputs in a transaction with multiple inputs belong to the same entity, using the common-input-ownership heuristic. This assumption forms the foundation of nearly all blockchain surveillance.
A UTXO received from a KYC exchange becomes permanently linked on-chain when combined with a UTXO from a non-KYC source. Consistent selection patterns, such as always choosing the largest coins first, can also fingerprint a wallet implementation. These patterns can reveal which software created the transaction.
Coin control gives you fine-grained control over which UTXOs are spent together. You can hand-pick inputs instead of relying on automatic selection. CoinJoin and PayJoin further break the common-input heuristic by mixing inputs from multiple participants in a single transaction.
The Bitcoin privacy research article gives a comprehensive overview of on-chain privacy techniques. It covers these methods in more detail.

## Fee Optimization

Each input adds weight to a transaction: roughly 68 vBytes for native SegWit inputs and up to 148 vBytes for legacy inputs. Each additional SegWit input costs approximately 3,400 sats at a feerate of 50 sat/vB. A smart coin selection algorithm minimises the number of inputs and avoids unnecessary change outputs to keep fees low, especially during high-fee periods driven by mempool congestion.

## UTXO Set Management

Small UTXOs accumulate in wallets over time as incoming payments arrive, and these fragments can become uneconomical to spend when fees rise. A well-designed coin selection algorithm performs opportunistic UTXO consolidation by including more inputs than strictly necessary when fees are low, merging small UTXOs into a single larger change output. The waste metric drives this behaviour automatically by making extra inputs "free" or even beneficial below the long-term feerate.

## Merchant and Exchange Wallets

High-volume wallets process hundreds or thousands of withdrawals per day, making coin selection critical at this scale. Poor selection can cost thousands of dollars in excess fees daily. Many exchanges use custom algorithms tuned for batched transactions, where a single transaction pays multiple recipients and changes the optimisation calculus significantly.

## Dust Accumulation

Naive algorithms such as largest-first progressively fragment a wallet into smaller UTXOs, which become stranded once their value drops below the cost of spending them, a condition sometimes exploited in dust attacks. The Bitcoin network enforces a dust limit of 546 satoshis for legacy outputs and 294 satoshis for SegWit outputs, below which transactions are rejected by default relay policy. UTXOs under roughly 10,000 sats may be uneconomical to spend during fee spikes in practice.

## Privacy Degradation

Automatic coin selection can unknowingly combine UTXOs from different privacy contexts. A wallet that mixes exchange-sourced coins with privately received coins creates a permanent, public link between those sources. You should review your coin control settings and consider labelling UTXOs by source if you value privacy.

## Computational Cost

Wallets with very large UTXO pools containing thousands of coins can experience noticeable delays during coin selection. Bitcoin Core caps its Branch and Bound and CoinGrinder searches at 100,000 combinations to keep selection time reasonable. The Knapsack solver's 1,000 iterations still add latency in extreme cases.

## How Spark Eliminates the Problem

The UTXO model makes coin selection necessary because every on-chain spend requires choosing inputs, paying per-input fees and managing change. Layer 2 protocols such as Spark remove this overhead entirely. These protocols avoid the coin selection problem rather than trying to solve it on-chain.
Spark transfers happen off-chain through key rotation rather than on-chain transactions. You have no inputs to select, no change outputs to create and no per-byte fees to optimise, so receiving a thousand small payments gives you the same transfer cost as receiving a single large deposit. The research article on the UTXO model vs. account model provides a detailed comparison.
The glossary entry is for informational purposes only and does not constitute financial or investment advice. You should always do your own research before using any protocol or technology.
Part of the [Nuri glossary](https://nuri.com/glossary), which defines 1876 Bitcoin, Lightning, wallet, stablecoin and payment terms.
