You are correct, it is infact equivalent to knapsack problem. To the best of my knowledge most miners simply sort by fee and this gives a good approximation for optimal solution.
A couple of things of things to highlight how difficult the transaction slection is:
1) Knapsack problem as you pointed out.
2) But even if the constraint in knapsack were removed(i.e infinite blocksize), still calculating optimal fees is NP hard problem.
Transactions also have dependencies and conflicts. We’ll take
conflicts first, which come from the prohibition on double-spending.
We’ll also make the problem considerably easier by ignoring the block
size limit and assuming all transactions have a constant fee. If
transactions B and B’ both claim funds from transaction A, then only
one of B and B’ can be published and we say they conflict. If this is
the only constraint, we can simply draw a graph of all transactions
with edges between transactions which conflict. The miner’s task is to
find the largest set of vertices (transactions) with no edge between
then (no conflict). Solving this is exactly the maximum independent
set problem. (Source)
I presume the double-spend instances are rare and so are dependant transactions(child pays for parent known as CPFP). Therefore, my best guess is that simple greedy algorithm(taking CPFP into account) is close to optimal. Furthermore, there is also the tradeoff of time complexity which is very crutial for miners when we attempt alogrithms which are not simple.
I don't see how this is not Knapsack. How the uniqueness of txns change things? – SpiderRico – 2017-12-05T16:20:32.253
1The Knapsack problem assumes you have an unlimited number of items with the same v/w ratio. With Bitcoin this is not the case and the solution to maximize v is to order the list by v/w and do a greedy selection from the top. (This is default algorithm, some miners might have custom ones) – Emil R – 2017-12-05T16:27:35.670
I don't remember learning such assumption on Knapsack problem. Can you provide me a source? – SpiderRico – 2017-12-05T16:29:16.493
1
The knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible. https://en.wikipedia.org/wiki/Knapsack_problem
– Emil R – 2017-12-05T16:31:08.627This is wrong. It is possible to construct 2 blocks where fee/size is not the best algorithm. I still think it is not possible to opitmal block in linear time. The greedy algorithm is just a good approximation – sanket1729 – 2017-12-06T07:12:30.197