TeachMeBitcoin

The Perfect Fit: The Branch and Bound (BnB) Algorithm Explained

From TeachMeBitcoin, the free encyclopedia Reading time: 4 min

The Perfect Fit: The Branch and Bound (BnB) Algorithm Explained

The "Gold Standard" of coin selection is the Branch and Bound (BnB) algorithm. Developed by the legendary Bitcoin researcher Mark Erhardt (Murch), BnB is designed with one primary goal: Avoid Change. In a traditional transaction, you spend some coins and receive "Change" back to a new address. This change output takes up space in the blockchain (costing you fees) and "Links" your addresses together (hurting your privacy). BnB searches your inventory for a combination of UTXOs that exactly matches the amount you want to send plus the fee. If it finds such a combination, you receive NO change. It is the "Perfect Fit" of the forge.

BnB is a "Search Algorithm." It treats your UTXOs like a "Binary Tree." At each step, it asks: "Should I include this coin or not?" It explores these "Branches" until it finds a set that hits the "Target Range." If a branch becomes "Too Heavy," it "Bounds" (stops) that search and tries another path. It is the "Mathematical Precision" of the internal bank.

Analyzing the Search Engine: SelectCoinsBnB

In the source code (src/wallet/coinselection.cpp), we can see the "Inner Workings" of this search. It is a "Depth-First Search" that is limited by a "Try Count" to ensure it doesn't slow down your computer.

/**
 * This is the core logic of the Branch and Bound search.
 */
util::Result<SelectionResult> SelectCoinsBnB(std::vector<OutputGroup>& utxo_pool, ...)
{
 // 1. Sort the coins from "Largest" to "Smallest."
 std::sort(utxo_pool.begin(), utxo_pool.end(), descending);

 // 2. Start the search. We allow up to 100,000 "Guesses."
 for (size_t curr_try = 0; curr_try < 100000; ++curr_try) {

 // 3. If the current total is within the "Target Range"... SUCCESS!
 if (curr_value >= selection_target && curr_value <= selection_target + cost_of_change) {
 // We found a perfect match. No change needed.
 return result;
 }

 // 4. "Branching": Should we add the next coin?
 if (curr_value + next_coin <= target_upper_bound) {
 // Add the coin and keep moving "Forward."
 curr_value += next_coin;
 } else {
 // "Bounding": This path is too heavy. Move "Backwards" and try another branch.
 backtrack = true;
 }
 }
}

Explaining the Search: The Suitcase Puzzle

The "Changeless" Advantage

A changeless transaction is a "Privacy Masterpiece." To an outside observer looking at the blockchain, a transaction with two outputs (Recipient and Change) clearly shows that money is moving from one person to another. But a transaction with only ONE output is "Ambiguous." Did you send the whole amount to someone else? Or did you move the whole amount to another wallet you own? By avoiding change, you "Muddy the Water" of the ledger. You are the "Master of the Fit," protecting your wealth through the power of "Mathematical Perfection."


☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!