(1) algorithm principle
When using the backtracking method to solve a problem, the space for solving the problem should be clearly defined. The space for solving the problem should include at least one (optimal) solution of the problem. After defining the space for resolving the problem, the space for resolving the problem should be well organized so that the whole space can be searched by backtracking. The solution space is usually organized into a tree or graph.
After determining the organizational structure of the space, the Backtracking Method starts from the Start Node (root node) and searches the entire solution space in depth first. This Start Node becomes a dynamic node and also the current expansion node. At the current extended node, search moves in depth to a new node. This new node becomes a new dynamic node and the current extended node. If you cannot move further at the current extension node, the current extension node becomes a dead end point. In this case, you should move back to the nearest dynamic knot point and make it the current expansion node. The Backtracking Method recursively searches in the solution space until the required solution or solution space has no dynamic knots.
(2) solving 0-1 knapsack algorithm analysis
The 0-1 knapsack problem is the subset selection problem. Generally, the 0-1 backpack problem is NP-hard. The solution space of the 0-1 knapsack problem can be represented by a subset tree. When searching for a spatial tree, as long as its left son node is a feasible node, the search enters the left subtree. If the right subtree contains an optimal solution, you can search for the right subtree. Otherwise, cut the right subtree. Set R to the total value of the remaining items, CP to the current value, and bestp to the current optimal value. When CP + r <= bestp, you can cut the right subtree. A better way to calculate the upper bound of the solution in the right subtree is to sort the remaining items by their unit weight value, and then load the items in sequence until they cannot be loaded, load a part of the item and fill it with a backpack. The resulting value is the upper bound of the solution in the right subtree. In order to facilitate the calculation of the upper bound, you can first sort items from large to small based on their unit weight values. Then, you only need to examine each item in order.
(3) Algorithm Implementation
Import javax. Swing .*;
Public class knapsack extends jframe {
Private int C; // the size of the backpack
Public int bestp; // optimal value
Public int CP; // Current Value
Public int CW; // current weight
Public static int [] flag; // Save the original location
Final jscrollpane scrollpane = new jscrollpane ();
Public static jtextarea resulttextarea;
Public jlabel L1 = new jlabel ("Optimal Solution ");
Public jlabel l3 = new jlabel ("time used ");
Public static jtextfield T1 = new jtextfield ();
Public static jtextfield t2 = new jtextfield ();
Final jlabel label = new jlabel ();
Final jlabel label_1 = new jlabel ();
// Private Static int X []; // records optional items
Private Static int [] cx;
// Constructor
Public knapsack (int pp [], int WW [], int CC ){
This. c = cc;
This. CP = 0;
This. CW = 0;
This. bestp = 0;
Cx = new int [pp. Length];
For (INT I = 0; I <pp. length; I ++ ){
CX [I] = 0;
}
}
Void knapsack (int v [], int W [], int C, int N ){
Double temp = 0;
Int temp1;
Int valuetemp = 0;
Int weighttemp = 0;
Int J;
Flag = new int [50];
Double [] T = new double [n + 1];
// Calculate the unit value of an item
For (INT I = 0; I <n; I ++ ){
Flag [I] = I;
T [I] = (double) V [I]/W [I];
}
// Sorts the value of an item and sorts the value and weight in the array.
For (INT I = 0; I <n; I ++ ){
For (j = n-1; j> I; j --){
If (T [J]> T [J-1]) {
// Exchange unit value
Temp = T [J];
T [J] = T [J-1];
T [J-1] = temp;
// The Position of the exchanged Value
Valuetemp = V [J];
V [J] = V [J-1];
V [J-1] = valuetemp;
// Position of the switching weight
Weighttemp = W [J];
W [J] = W [J-1];
W [J-1] = weighttemp;
Temp1 = Flag [J];
Flag [J] = Flag [J-1];
Flag [J-1] = temp1;
}
}
}
Backtrack (V, W, C, 0, N, CX );
This. setresizable (false );
This. settitle ("backtracing algorithm computing 0-1 backpack ");
This. getcontentpane (). setlayout (null );
This. setbounds (100,100,670,400 );
This. setdefaclocloseoperation (jframe. exit_on_close );
Scrollpane. setbounds (190, 25,454,293 );
Getcontentpane (). Add (scrollpane );
Resulttextarea = new jtextarea ();
Resulttextarea. seteditable (false );
Scrollpane. setviewportview (resulttextarea );
Label. sethorizontaltextposition (swingconstants. Right );
Label. sethorizontalalignment (swingconstants. Right );
Label. settext ("Optimal Solution :");
Label. setbounds (10, 42, 66, 18 );
Getcontentpane (). Add (Label );
T1.sethorizontalalignment (swingconstants. Right );
// T1.sethorizontaltextposition (swingconstants. Right );
T1.sethorizontalalignment (swingconstants. Right );
T1.setbounds (80, 42, 66, 18 );
Getcontentpane (). Add (T1 );
Label_1.sethorizontaltextposition (swingconstants. Right );
Label_1.sethorizontalalignment (swingconstants. Right );
Label_1.settext ("time used :");
Label_1.setbounds (10, 75, 66, 18 );
Getcontentpane (). Add (label_1 );
T2.sethorizontalalignment (swingconstants. Right );
// T1.sethorizontaltextposition (swingconstants. Right );
T2.sethorizontalalignment (swingconstants. Right );
T2.setbounds (80, 75, 66, 18 );
Getcontentpane (). Add (T2 );
}
// Calculate the upper bound value
Double bound (INT [] V, int [] W, int C, int I, int N ){
Int cleft = C-CW;
Int vleft = CP;
// Load the item in descending order of Unit Weight Value
While (I <= N & W [I] <= cleft ){
Cleft-= W [I];
Vleft + = V [I];
++ I;
}
// Full of backpacks
If (I <= N)
Vleft + = V [I] * cleft/W [I];
Return vleft;
}
Void backtrack (int v [], int W [], int C, int I, int N, int [] cx ){
// Determine whether the node reaches the leaf node
If (I> = N ){
If (bestp <CP ){
Bestp = CP;
}
Return;
}
If (CW + W [I] <= c ){
CW + = W [I];
CP + = V [I];
CX [I] = 1;
Backtrack (V, W, C, I + 1, n, CX );
CW-= W [I];
CP-= V [I];
}
If (bound (V, W, C, I, n)> bestp ){
CX [I] = 0;
Backtrack (V, W, C, I + 1, n, CX );
}
}
Void printresult () {// system. Out. println ("count" + count );
System. Out. println ("****** backtracking *****");
System. Out. println ("***** number of items: n = 50 ");
System. Out. println ("***** backpack capacity: c = 1000 ");
System. Out. println ("***** optimal value: =" + bestp );
T1.settext ("" + bestp );
System. Out. println ("***** the selected item is :");
}
Public static void main (string [] ARGs ){
Int P [] = {1, 220,208,198,192,180,180,165,162,160,158,155,
130,125,122,120,118,115,110,105,101,100,100, 98, 90, 88, 82, 80, 77
, 75, 73, 70, 69, 66, 65, 63, 60, 58, 56, 50, 30, 20, 15, 10, 8, 5, 3, 1, 1
};
Int W [] = {80, 82, 85, 70, 72, 70, 66, 50, 55, 25, 50, 55, 40, 48,
50, 32, 22, 60, 30, 32, 40, 38, 35, 32, 25, 28, 30, 22, 50, 30,
45, 30, 60, 50, 20, 65, 20, 25, 30, 10, 20, 25, 15, 10, 10, 4, 4, 1, 1 };
Int maxweight = 1000;
Double Start = system. currenttimemillis ();
Knapsack Ks = new knapsack (p, W, maxweight );
KS. knapsack (p, W, 50); // perform a retrospective search.
KS. printresult ();
KS. setvisible (true );
Resulttextarea. append ("item selection ('0' indicates no selection, and '1' indicates selection) as follows:/N ");
For (INT I = 0; I <Cx. length; I ++ ){
System. Out. Print (CX [I] + "");
Resulttextarea. append ("X [" + flag [I] + "] =" + cx [I] + "/N ");
}
Double end = system. currenttimemillis ();
T2.settext (end-Start) + "Ms ");
System. Out. println ("/ntime consuming:" + (end-Start ));
}
}