3

There are two well-known knapsack problems:

1) Given n items, each has its weight and cost. We need to select items, that will fit in our knapsack and have maximal cost in sum. It can be easily solved using dynamic programming.

2) Fractional knapsack: same as the first, but we can take not the whole item only, but its part. This problem can be easily solved with greedy algorithm.

Imagine we are using greedy algorithm from second problem to solve the first one. How can I prove, that the solution we will get is no more than two times worse, than the optimal one?

Aleksandr Tukallo
  • 1,299
  • 17
  • 22
  • What do you mean by "worse"? – Juan Lopes Dec 13 '16 at 11:26
  • 1
    You probably have more restrictions, since general case (weight, cost are double values, not simply int values) is not so DP-friendly – Alexander Anikin Dec 13 '16 at 12:08
  • 1
    @Alexander Anikin: `int` is *not a restriction* - just *scale up* my answer - knapsack with `1e100` volume; item `A` with `weight = 1` and `cost = 1` and item `B` with `weight = 1e100` and cost `1e100-1` – Dmitry Bychenko Dec 13 '16 at 12:38

1 Answers1

4

As far I can see, the greedy solution can be as much as inefficient as you want. Imagine that you have a knapsack with capacity 1 and two (n = 2) items:

item weight cost density
------------------------
   A      ε    ε       1  <- greedy choice
   B      1  1-ε     1-ε  <- optimal choice

so the greedy algorithm takes A with ε cost when the optimal solution is to take B with 1-ε cost. The chosen (greedy) solution is

  (1-ε)/ε = 1/ε - 1 

times inefficient than optimal one. Make ε as little as you want (say, ε = 1e-100) and have a very inefficient greedy solution.

Edit: in case of integer values only, just scale the sample above: you have a knapsack with capacity X and two (n = 2) items

item weight cost density
------------------------
   A      1    1       1  <- greedy choice
   B      X  X-1   1-1/X  <- optimal choice

in this case the greedy solution is

   (X - 1) / 1 = X - 1

times inefficient than optimal one. Finally, make X to be large enough (say, X = 1e100)

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Yep, the real 2-competitive greedy algorithm needs to consider a solution consisting solely of the item with a fractional setting. – David Eisenstat Dec 13 '16 at 14:45
  • Yea, thank you! I understood, that it works with integers too. Thanks for nice contr-example! – Aleksandr Tukallo Dec 13 '16 at 16:06
  • @Dmitry Bychenko: Very concise answer! – Codor Dec 13 '16 at 16:13
  • 1
    @Aleksandr Tukallo: you are welcome! Even if just *one item* (that's `B`) can't be split you can create a counter example with arbitrary large inefficiency. Even if `B` can be split but not into *small enough chunks* (we can't split gold atom into gold protons and neutrons) you can create the counter example – Dmitry Bychenko Dec 13 '16 at 16:14
  • Possibly related, but with far less concise answers: http://stackoverflow.com/questions/35967159/continuous-knapsack-vs-0-1-knapsack – Codor Dec 13 '16 at 16:19