function KNAP (V, C, M)
if isTrivial (V, C, M) return trivialKNAP (V, C, M);
(X0, C0, m0) = KNAP (V-{vn}, C-{cn},
M);
(X1, C1, m1) = KNAP (V-{vn}, C-{cn},
M-
vn
);
if C1+
cn > C0
return (X1.1,
C1+
cn, m1+
vn
);
return (X0.0, C0, m0);
function isTrivial (V, C, M)
return (isEmpty (V) or M=0 or M<0);
trivialKNAP
přenecháme laskavému čtenáři jako cvičení. |
výsledné řešení |
|
netriviální podúloha k
řešení |
|
triviální podúloha k
řešení |
|
triviální podúloha,
jejíž řešení nepotřebujeme |
|
rekonstrukce X |
10 |
|
|
|
|
|
|
9 |
|
|||||
8 |
|
|||||
7 |
|
|
|
|
||
6 |
|
|
|
|
|
|
5 |
|
|
|
|||
4 |
|
|
||||
3 |
|
|
|
|
||
2 |
|
|
|
|||
1 |
|
|
|
|||
0 |
|
|
|
|
|
|
-1 |
|
|
|
|
|
|
-2 |
|
|
|
|
|
|
-3 |
|
|
|
|
|
|
-4 |
|
|
|
|
|
|
n |
0 |
1 |
2 |
3 |
4 |
5 |
V |
2 |
6 |
5 |
3 |
4 |
|
C |
5 |
9 |
20 |
12 |
18 |
10 |
0, 0 |
2, 5 |
8, 14 |
7, 25 |
10, 37 |
9, 38 |
9 |
0, 0 |
|||||
8 |
0, 0 |
|||||
7 |
0, 0 |
2, 5 |
6, 9 |
7, 25 |
||
6 |
0, 0 |
2, 5 |
6, 9 |
5, 20 |
5, 20 |
|
5 |
0, 0 |
2, 5 |
2, 5 |
|||
4 |
0, 0 |
2, 5 |
||||
3 |
0, 0 |
2, 5 |
2, 5 |
2, 5 |
||
2 |
0, 0 |
2, 5 |
2, 5 |
|||
1 |
0, 0 |
0, 0 |
0, 0 |
|||
0 |
0, 0 |
0, 0 |
|
|
|
|
-1 |
0, -inf |
0, -inf |
|
|
|
|
-2 |
|
|
0, -inf |
|
|
|
-3 |
|
0, -inf |
|
|
|
|
-4 |
|
0, -inf |
|
|
|
|
-5 |
|
0, -inf |
|
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