<p>No. this is not knapsack problem and no one need to find optimal solution.</p>

<p>Sort your transactions in mempool by two rules: </p>

<ul>
<li>subsequent transactions can not be to the left of parent ones</li>
<li>most profitable transactions (fee/size) gravitate to left</li>
</ul>

<p>and take leftmost transactions from mempool until 1 mb limit</p>

CC BY-SA 3.0
2017-11-02T12:22:24.010Z
Great, thanks.
Where can I find the code that implements this behavior?
Itay
CC BY-SA 3.0
2017-11-02T12:43:14.217Z
somewhere in https://github.com/bitcoin/bitcoin (cy, Captain Obvious)
amaclin
CC BY-SA 3.0
2017-11-03T07:22:41.167Z
this isnt the full truth, and miners likely differ in their implementations. one way to prove this isnt the full truth is to look at the existence of transactiln accelerators, another to look at blocks with 0 transactions and a third to look at blocks with some transaction types excluded.
Jonathan Silverblood
CC BY-SA 3.0
2017-11-02T08:10:54.103Z
2017-11-02T08:10:54.103Z
amaclin
<p>I understand it's in the miner's best interest to receive as many fees as possible for his hopefully-accepted block, hence it should include the most rewarding (fee / size) txs - up to the 1MB limit.</p>

<p>However, what algorithm do the miners use? It seems like a knapsack problem, which is NP-Hard, so I doubt miners actually go for the optimal solution.</p>

<p>Thanks!</p>

<transactions><block><mining-profitability><mining-reward><algorithms>
What algorithm do bitcoin miners use to choose which tx to include in their block?
Itay
# What algorithm do bitcoin miners use to choose which tx to include in their block?

(Asked by: username_0 on 2017-11-02)

I understand it's in the miner's best interest to receive as many fees as possible for his hopefully-accepted block, hence it should include the most rewarding (fee / size) txs - up to the 1MB limit.

However, what algorithm do the miners use? It seems like a knapsack problem, which is NP-Hard, so I doubt miners actually go for the optimal solution.

Thanks!

**Tags:** transactions, block, mining-profitability, mining-reward, algorithms


---

## 1 answers

### Answer by username_1 on 2017-11-02. Score: 1

No. this is not knapsack problem and no one need to find optimal solution.

Sort your transactions in mempool by two rules:

* subsequent transactions can not be to the left of parent ones
* most profitable transactions (fee/size) gravitate to left

and take leftmost transactions from mempool until 1 mb limit

> **Comments:**
> **username_0**: Great, thanks. Where can I find the code that implements this behavior?  
> **username_1**: somewhere in https://github.com/bitcoin/bitcoin (cy, Captain Obvious)  
> **username_2**: this isnt the full truth, and miners likely differ in their implementations. one way to prove this isnt the full truth is to look at the existence of transactiln accelerators, another to look at blocks with 0 transactions and a third to look at blocks with some transaction types excluded.  

---

