2) sort items with respect to descending values of h
i
,
set k = 1,
3) if the remaining capacity w
m,k
of the knapsack at
k-th stage of the algorithm exceeds the weight of
the i-th item, include this item x
i
= ⌊
w
m,k
w
i
⌋ times
(w
m,k
is the available capacity of the knapsack
when considering the k-th item); in the opposite
case, proceed to the next item and put k := k + 1,
4) repeat Step 3 until all items are considered.
Other types of greedy algorithm are not consid-
ered here, as e.g. (Ekel and Neto, 2006), sice the paper
is focused on giving background to the DP approach.
On the opposite to the greedy algorithm, there is a
brute force algorithm which generates all possible so-
lutions abiding constraints, and selecting the one for
which the objective function is maximized. It is easy
to implement, but at the same time is computationally
expensive, and can be formulated as follows:
1) generate matrix M ∈ R
m×n
of all (m) possible so-
lutions to the knapsack problem (put in rows),
2) generate vector r
comprising objective function
values for all rows of M,
3) find the maximum entry of r
, and set the corre-
sponding solution as optimal.
Dynamic programming (DP) is the method based
on successive division of the task into subproblems,
subproblems into subsubproblems, etc, proposed by
Richard Bellman, who was granted an IEEE medal in
1979 for this algorithm (Arora, 2004; Nocedal and
Wright, 1999; Randvidran et al., 2006). The next
stage is based on merging solutions to obtain the so-
lution of the initial problem. Every subproblem is
solved once only, what shortens the computation time,
and partial solutions are placed in the tableau (Moura,
2012; Kellerer et al., 2004). However, it may suffer
from the so-called curse of dimensionality for highly-
dimensioned problems (not discussed here).
In the case of the knapsack problem, the DP
tableau contains solutions for increasing capacity of
the knapsack in its last columns, and in subsequent
rows the number of available items is increased.
In the tableau (see Tab. 1), e.g. the entry P(1, 1)
is the largest objective function value for knapsack of
capacity equal to 1, with a one-element set of avail-
able items, P(n,W) is the largest objective function
for the knapsack with capacity of W and n avail-
able items. The optimal objective function value is
in P(n,W). In order to restore the values of decision
variables, the Bottom-Up algorithm must be used,
what will be given below.
The DP algorithm for the KP is summarized as
follows:
1) set initial values: P(0, 0) = 0, put P(0, j) = 0 ∀ j =
1, . . . , W and P(i, 0) = 0 ∀i = 1, . . . , n;
2) for consecutive capacities j = 1, . . . , W proceed
to Step 3;
3) for consecutive items i = 1, . . . , n proceed to Step
4;
4) if it holds that j ≥ w
i
, then put P(i, j) =
max(P(i− 1, j), P(i, j − w
i
) + c
i
), and, in the op-
posite case, put P(i, j) = P(i− 1, j);
5) repeat Step 2 until i = n ∩ j = W.
The Bottom-Up algorithm of restoring decision
variables in the optimal solution is as follows:
1) assume the initial entry to be P(n,W) and put i :=
n, j := W;
2) for P(i, j) = P(i− 1, j) put i := i− 1; in the oppo-
site case, put j := j − w
i
and update x
by substi-
tuting x
i
:= x
i
+ 1;
3) repeat Step 2 until P(i, j) 6= 0.
3 EXAMPLES
3.1 Greedy Algorithm
As an example of the greedy algorithm one can give
the following KP problem:
max
x
2x
1
+ x
2
+ x
3
+ 3x
4
+ x
5
+ x
6
+ 4x
7
+ 2x
2
+ x
9
s.t. 5x
1
+ 3x
2
+ 2x
3
+ 3x
4
+ 4x
5
+ 3x
6
+ 3x
7
+
+3x
8
+ 5x
9
≤ 14,
x
∈ Z
9
+
.
The following tableau presents the data of the problem:
i
1 2 3 4 5 6 7 8 9
w
i
5 3 2 3 4 3 3 3 5
c
i
2 1 1 3 1 1 4 2 1
h
i
0.40 0.33 0.50 1.00 0.25 0.33 1.33 0.67 0.20
.
By sorting the items with descending h
i
one gets (k =
1):
i
7 4 8 3 1 2 6 5 9
h
i
1.33 1.00 0.67 0.50 0.40 0.33 0.33 0.25 0.20
⌊
14
w
i
⌋
4 4 4 7 2 4 4 3 2
.
The remaining capacity W
m,1
= 14 > w
7
= 3, thus
x
7
= 4. The current capacity W
m,2
= 14− 12 = 2 (k =
2).
i
4 8 3 1 2 6 5 9
h
i
1.00 0.67 0.50 0.40 0.33 0.33 0.25 0.20
⌊
2
w
i
⌋ 0 0 1 0 0 0 0 0
.
As the result, x
3
= 1. The remaining capacity W
m,3
=
0 – the algorithm terminates. The optimal solution
becomes:
x
∗
= [0, 0, 1, 0, 0, 0, 4, 0, 0]
T
,
f(x
∗
) = 17.