Input : X = {x1, x2, . . ., xn}
Solution(s) (S) are contained in X (some combination)
Feasible solution (FS): FS
S We need to find a feasible solution that either maximizes or minimizes a given objective function. A feasible solution that does this is called an optimal solution
Optimal solution:
S
Method of solution:
Problem:
Ex: n = 3 W = (18, 15, 10) P = (25, 24, 15), M = 20
We could order according to decreasing profit (pick the highest profit first):
But chosing the highest value first does not always give us optimal.
Order according to increasing weight:
But chosing greedily here does not always give us optimal either.
Selection based on profit alone or weight alone will not lead to optimal solution.
Solution:
Proof: by contradiction and transformation
X (vector of xi's): Solution obtained by algorithm
Suppose X not optimal
Let Y be optimal See also page 201 (read)
If xi = 1, 1 < i < n, then X is optimal.
If X not optimal, let Y be optimal
w.l.o.g.
Let k be the least index such that xk != yk (must be at least one)
Claim: yk < xk
Transform Y to Z by increasing yk to xk and decreasing yk+1, . . ., yk to avoid overflowing knapsack
Y --> Z
increase in weight = wk (zk - yk) weight must stay the same so,
If increase > decrease
Repeat: Need to show increase > decrease which is:
(zk - yk) pk >?
OR (take each side and divide and multipy by the same term - will need this form)
(zk - yk) pk/wk * wk >?
We know the following (and it will be useful):
OK, we now need to show
(zk - yk) pk/wk * wk >?
(zk - yk)wk * pk/wk >?
pk/wk divide both sides by pk/wk note that what is left is the relation about weights that had to stay the same
Therefore profit increase > decrease. hint on what just happened there
Transition Y --> Z does not decrease profit
If profit increases, Y not optimal
Repeat transformations until Y --> Z --> . . . --> X without change in profit.
Therefore X is optimal.
S
2|x| -possible subsets of X
Knapsack Problem
n objects wi weight of object pi profit of object M capacity of knapsack xi fraction of object i included in knapsack, 0<xi<1
Find {x1, x2, . . ., xn} such that
X 1/2, 1/3, 1/4 16.5 24.25 0, 2/3, 1 20 31 0, 1, 1/2 20 31.5
P: 10 5 1 M = 10 W: 100 10 1 X: 1/10 0 0 xipi = 1 but X: 0 1 0 xipi = 5
W: 1 10 100 M = 1 P: 1 100 1000 X: 1 0 0 xipi = 1 but X: 0 1/10 0 xipi = 10
Consider a ratio of profit to weight.
If for
This means we could put everything in the sack - boring.
Otherwise, assume any optimal solution must fill knapsack to capacity according to the following algorithm
Algorithm sack (P, W, M, X, n)
{
sort pi/wi in decreasing order
// ie. p1/w1 > p2/w2 > . . . > pn/wn
for i = 1 to n while M > 0 do
}
if wi < M then xi = 1
M = M - xi wi
else xi = M/wi
Transform Y to X (transform Y to some Z, and show that that Z must be X)
without changing value of optimizing function, thus showing that X is optimal as well.
Let j be least index such that xj < 1.
ie. xi = 1, 1 < i < j, xj < 1
x1 x2 ... xj-1 xj xj+1 ... xn 1 1 ... 1 < 1 0 ... 0
Remember, j is the first index that is not a value of 1 for X.
prove claim by looking at the only three cases possible:
This proves claim that yk < xk.
Then xk = 1 ; But xk != yk so 0 < yk < 1
==> yk < xk = 1
Since
If yk > xk then
Therefore yk < xk
If yk > xk then
This is not possible, therefore yk < xk
Note that Z looks more like X than Y.
Need to show that Z is optimal as well.
Y y1 ... yk-1 yk yk+1 ... yn ... ... z1 ... zk-1 zk zk+1 ... zn ... ... X x1 ... xk-1 xk xk+1 ... xn
decrease in weight =
increase in profit = (zk - yk) pk
decrease in profit =
then Z is optimal as well.
pk/wk > pi/wi , k < i < n, (ordered that way)
multiply both sides by the same terms
Therefore Z is optimal.
The graph stuff: