ArticleDirectory
- Iv. 0-1 backpack Problems
- 5. Travel Salesman Problems
Iv. 0-1 backpack Problems
Problem description: N items and a backpack. The Weight of item I is wi, the value is pi, and the size of the backpack is C. Q: How should I select the items to be loaded into the backpack to maximize the total value of the items in the backpack?
The 0-1 knapsack problem is a special integer programming problem..
Solution space:
Feasibility constraint function:
Upper Bound functions:
When considering a right subtree, Set
R: indicates the total value of unused items (remainder)
CP: current price)
Bestp: the best price currently obtained)
Then, meet the following requirements:
However, the upper bound R is too loose.
A more accelerated upper bound:
Sort the remaining items by unit weight value, and load the items in sequence until the items cannot be loaded. Then, place part of the remaining items in the backpack. (R_N <= r)
Implementation
/* Subject: 0-1 backpack problem * Author: chinazhangjie * mailbox: chinajiezhang@gmail.com * development language: C ++ * Development Environment: Mircosoft virsual studio 2008 * Time: 2010.10.25 */# include <iostream> # include <vector> # include <algorithm> # include <functional> using namespace STD; class goods {public: int weight; // weight int price; // price goods (): weight (0), price (0) {}}; bool goods_greater (const Goods & LHS, const Goods & RHs) {return (LHS. price/LHS. weight)> (RHS. price/RHS. weight);} class knapsack {public: knapsack (int c, const vector <goods> & GL): Capacity (C), curr_price (0), best_price (0 ), curr_weight (0) {goods_list = gl; total_layers = gl. size (); curr_path.resize (total_layers); best_path.resize (total_layers);} void backtrack () {__ backtrack (0); cout <"Path:"; copy (best_path.begin (), best_path.end (), ostream_iterator <int> (cout, ""); cout <Endl; cout <"best_price:" <best_price <Endl;} PRIVATE: // calculate the upper bound int _ bound (INT layers) {int cleft = capacity-curr_weight; int result = curr_price; // sort the items after layer by unit price in descending order. Vector <goods> TMP = goods_list; sort (TMP. begin () + layers, TMP. end (), goods_greater); // load the while (layers <total_layers & TMP [layers]. weight <= cleft) {cleft-= TMP [layers]. weight; Result + = TMP [layers]. price; ++ layers;} // If (layers <total_layers) {result + = (TMP [layers]. price/tmp [layers]. weight) * cleft;} return result;} void _ backtrack (INT layers) {// reach the leaf node and update the optimal value if (layers> = total_layers) {If (curr_price> best_price | best_price = 0) {best_price = curr_price; copy (curr_path.begin (), curr_path.end (), best_path.begin ();} return ;} // left pruning (can be placed below) if (curr_weight + goods_list [layers]. weight <= capacity) {curr_path [layers] = 1; curr_weight + = goods_list [layers]. weight; curr_price + = goods_list [layers]. price ;__ backtrack (layers + 1); curr_weight-= goods_list [layers]. weight; curr_price-= goods_list [layers]. price;} // right-side pruning if (_ bound (layers + 1)> best_price | best_price = 0) {curr_path [layers] = 0; __backtrack (layers + 1);}/* curr_path [layers] = 0 ;__ backtrack (layers + 1); */} PRIVATE: vector <goods> goods_list; // goods information list int capacity; // backpack carrying capacity int curr_price; // current price int curr_weight; // current weight int best_price; // The current obtained optimal value int total_layers; // total number of layers vector <int> curr_path; // current path vector <int> best_path; // path under optimal value}; int main () {const int size = 3; vector <goods> GL (size); GL [0]. weight = 10; GL [0]. price = 1; GL [1]. weight = 8; GL [1]. price = 4; GL [2]. weight = 5; GL [2]. price = 5; knapsack KS (16, GL); Ks. backtrack (); Return 0 ;}5. Travel Salesman Problems
Problem description: Find a travel route with the smallest weight in the figure.
Solution space:Arrangement tree
Pruning policy:
The weight of the current path + the weight of the next path <current minimum weight, search for this path
Implementation:
/* Subject: Travel Salesman Problem * Author: chinazhangjie * mailbox: chinajiezhang@gmail.com * development language: C ++ * Development Environment: Mircosoft virsual studio 2008 * Time: 2010.10.26 */# include <iostream> # include <vector> # include <iterator> # include <algorithm> using namespace STD; class traveling {public: static const int noedge =-1; public: traveling (const vector <int> & ug): curr_cost (0), best_cost (-1) {node_count = ug. size (); undigraph = ug; curr_so Lution. resize (node_count); For (INT I = 0; I <node_count; ++ I) {curr_solution [I] = I;} best_solution.resize (node_count);} void backtrack () {__ backtrack (1); cout <best_cost <Endl;} PRIVATE: void _ backtrack (INT layers) {If (layers> = node_count) {If (undigraph [curr_solution [node_count-1] [curr_solution [0] = noedge) {return;} int total_cost = curr_cost + undigraph [curr_solution [node_count-1] [Curr_solution [0]; If (total_cost <best_cost | best_cost =-1) {// update the optimal cost and the Optimal Path best_cost = total_cost; copy (curr_solution.begin (), curr_solution.end (), best_solution.begin () ;}return ;}for (INT I = layers; I <node_count; ++ I) {// trim if (undigraph [curr_solution [layers-1] [curr_solution [I]! = Noedge & (curr_cost + undigraph [curr_solution [layers-1] [curr_solution [I] <best_cost | best_cost =-1 )) {// search for the subtree swap (curr_solution [layers], curr_solution [I]); curr_cost + = undigraph [curr_solution [layers-1] [curr_solution [layers]; __backtrack (layers + 1); curr_cost-= undigraph [curr_solution [layers-1] [curr_solution [layers]; swap (curr_solution [layers], curr_solution [I]) ;}} intnode_count; // number of nodes intcurr_cost; // the current fee intbest_cost; // The current vector <int> curr_solution; // current solution vector <int> best_solution; // optimal solution vector <int> undigraph; // undirected graph (using matrix storage)}; int main () {int size = 4; vector <int> UG (size); For (INT I = 0; I <size; ++ I) {ug [I]. resize (size);} ug [0] [0] =-1; ug [0] [1] = 30; ug [0] [2] = 6; ug [0] [3] = 4; ug [1] [0] = 30; ug [1] [1] =-1; ug [1] [2] = 5; ug [1] [3] = 10; ug [2] [0] = 6; ug [2] [1] = 5; ug [2] [2] =-1; ug [2] [3] = 20; ug [3] [0] = 4; ug [3] [1] = 10; ug [3] [2] = 20; ug [3] [3] =-1; traveling T (UG); T. backtrack (); Return 0 ;}
Reference books 《AlgorithmDesign and Analysis (second edition) edited by Wang Xiaodong
Professor Zhang Yang