Knapsack:
größe[element_] := element[[1]]
wert[element_] := element[[2]]
total[knapsack_, wg_] := Plus @@ wert /@ wg[[knapsack]] (*Wert*)
inhalt[knapsack_, wg_] := Plus @@ größe /@ wg[[knapsack]] (*Gewicht*)
Knapsack[{}, g_Integer?NonNegative] := {}
Knapsack[wg_List, g_Integer?NonNegative] :=
Module[{gn = größe[Last[wg]], wn = wert[Last[wg]], n = Length[wg],
rest = Drop[wg, -1], k1, k2},
k1 = Knapsack[rest, g];(*ohne letzten Gegenstand*)
If[g >= gn, k2 = Knapsack[wg, g - gn];(*mit letztem Gegenstand*)
k2 = Append[k2, n];
If[total[k1, wg] >= total[k2, wg], k1, k2], k1]]
KnapsackGraph[{},
inhalt_Integer?NonNegative] := {{}, {knoten[{inhalt, 0}, 0]}}
KnapsackGraph[wg_List, inhalt_Integer?NonNegative] :=
Module[{gn = größe[Last[wg]], wn = wert[Last[wg]], n = Length[wg],
rest = Drop[wg, -1], k1, k2, g, k, g1, g2}, {k1, g1} =
KnapsackGraph[rest, inhalt];
If[inhalt >= gn, {k2, g2} = KnapsackGraph[wg, inhalt - gn];
k2 = Append[k2, n];
If[total[k1, wg] >= total[k2, wg], k = k1;
g = {arrow[{inhalt, n}, {inhalt, n - 1}, True],
arrow[{inhalt, n}, {inhalt - gn, n}]}, k = k2;
g = {arrow[{inhalt, n}, {inhalt, n - 1}],
arrow[{inhalt, n}, {inhalt - gn, n}, True]}], k = k1;
g = {arrow[{inhalt, n}, {inhalt, n - 1}, True]};
g2 = {};];
{k, Join[g, g1, g2, {knoten[{inhalt, n}, total[k, wg]]}]}]
diskr = 0.4; (*Disk Radius*)
sf = 2; (*vertical scaling*)
scale[{x_, y_}] := {x, sf y}
knoten[xy_List, text_] := {{GrayLevel[1], Disk[scale[xy], diskr]},
Circle[scale[xy], diskr], Text[text, scale[xy]]}
t0 = 0.4; t1 = 1.1;(*Thickness[] in Punkten*)
arrow[from_, to_,
fett_ : False] := {AbsoluteThickness[If[fett, t1, t0]],
Line[{scale[from], scale[to]}]}
xticks[min_, max_] := Range[Round[min + 1], Round[max - 1], 2]
yticks[min_, max_] :=
Table[{i, i/2}, {i, Round[sf (min + 1)], Round[sf (max - 1)], sf}]
KnapsackPlot[wg_List, inhalt_Integer?NonNegative, opts___] :=
Module[{gr}, gr = KnapsackGraph[wg, inhalt][[2]];
Show[Graphics[gr], opts, Frame -> True, AspectRatio -> Automatic,
FrameLabel -> {"g", "n"},
PlotRange -> {{-1, inhalt + 1}, {-1, sf Length[wg] + 1}},
FrameTicks -> {xticks, yticks, xticks, yticks}]]
Iterative Version:
KnapsackIterative[wg_List, inhalt_Integer?NonNegative] :=
Module[{ks}, ks[0, in_] = {};
ks[n_, in_] :=
ks[n, in] =
Module[{gn = größe[wg[[n]]], wn = wert[wg[[n]]], k1, k2},
k1 = ks[n - 1, in];
If[in >= gn, k2 = ks[n, in - gn]; k2 = Append[k2, n];
If[total[k1, wg] >= total[k2, wg], k1, k2], k1]];
ks[Length[wg], inhalt]]
Examples:
sizes = {3, 4, 7, 8, 9};
values = {4, 5, 10, 11, 13};
things = {sizes, values}\[Transpose];
KnapsackPlot[things, 17]