0019 algorithm notes -- Dynamic Programming for solving 0-1 knapsack problems

Source: Internet
Author: User

1. Problem description: n items and a backpack are given. The Weight of item I is wi, its value is vi, and the size of the backpack is C. Q: How should I select the items that are loaded into the backpack to maximize the total value of the items in the backpack? Formal Description: given c> 0, wi> 0, vi> 0, 1 ≤ I ≤ n. A n element vector (x1, x2 ,..., Xn,), xi ε {0, 1}, ∋ Σ wi xi ≤ c, and Σ vi xi reaches the maximum, that is, a special integer programming problem. 2. Optimum principle: Set (y1, y2 ,..., Yn) is an optimal solution of (3.4.1). Then (y2 ,..., Yn) is an optimal solution for the following sub-problems: Proof: Use the reverse certificate method. Otherwise, set (z2, z3 ,..., Zn) is an optimal solution for the above sub-problem, and (y2, y3 ,..., Yn) is not its optimal solution. Obviously there are Σ vizi> Σ viyi (I = 2 ,..., N) and w1y1 + Σ wizi <= c, so v1y1 + Σ vizi (I = 2 ,..., N)> Σ viyi, (I = 1 ,..., N) Description (y1, z2, z3 ,..., Zn) is a better solution to the (3.4.1) 0-1 backpack problem. Export (y1, y2 ,..., Yn) It's not the optimal solution to the backpack problem. It's a conflict. 3. Recursive relationship: Set the optimal value of the sub-problem for the given 0-1 knapsack problem to m (I, j), that is, m (I, j) indicates that the backpack capacity is j, you can select an item as I, I + 1 ,..., The optimal value of the 0-1 backpack problem at n. Based on the optimal sub-structure of the 0-1 backpack problem, a recursive formula for m (I, j) calculation can be established: Note: (3.4.3) The size of the backpack is j, and the item can be selected as I. After making a decision on xi, the problem is in one of two states: (1) the remaining backpack capacity is j, without any benefits; (2) the remaining capacity is j-wi, the benefit value increases by vi. The specific code of the algorithm is as follows: [cpp] // 3d10-1 dynamic planning backpack problem # include "stdafx. h "# include <iostream> using namespace std; const int N = 4; void Knapsack (int v [], int w [], int c, int n, int m [] [10]); void Traceback (int m [] [10], int w [], int c, int n, int x []); int main () {int c = 8; int v [] = {, 3}, w [] = {, 3 }; // The subscript starts from 1: int x [N + 1]; int m [10] [10]; cout <"Weight of the item to be loaded <Endl; for (int I = 1; I <= N; I ++) {cout <w [I] <"";} cout <endl; cout <"values of items to be loaded are:" <endl; for (int I = 1; I <= N; I ++) {cout <v [I] <"" ;}cout <endl; Knapsack (v, w, c, N, m ); cout <"the greatest value of a backpack is:" <m [1] [c] <endl; Traceback (m, w, c, N, x ); cout <"the ID of the backpack is:" <endl; for (int I = 1; I <= N; I ++) {if (x [I] = 1) {cout <I <";}} cout <endl; return 0;} void Knapsack (int v [], int w [], int c, int n, int m [] [10]) {in T jMax = min (w [n]-1, c); // The maximum available backpack capacity ranges from 0 ~ W [n]-1] for (int j = 0; j <= jMax; j ++) {m [n] [j] = 0 ;} for (int j = w [n]; j <= c; j ++) // restriction range [w [n] ~ C] {m [n] [j] = v [n] ;}for (int I = n-1; I> 1; I --) {jMax = min (w [I]-1, c); for (int j = 0; j <= jMax; j ++) // backpack with different residual capacity j <= jMax <c {m [I] [j] = m [I + 1] [j]; // no benefits} for (int j = w [I]; j <= c; j ++) // different backpack capacities j-wi> c {m [I] [j] = max (m [I + 1] [j], m [I + 1] [j-w [I] + v [I]); // benefit value growth vi} m [1] [c] = m [2] [c]; if (c> = w [1]) {m [1] [c] = max (m [1] [c], m [2] [c-w [1] + v [1]);} // x [] array stores the 0-1 vector of the corresponding item, 0 does not load the backpack, 1 means to load the backpack void Traceback (int m [] [10], int W [], int c, int n, int x []) {for (int I = 1; I <n; I ++) {if (m [I] [c] = m [I + 1] [c]) {x [I] = 0;} else {x [I] = 1; c-= w [I] ;}x [n] = (m [n] [c])? 1-0;} Algorithm Execution Process: Fill in m [] [] and trace back process of Traceback: it is easy to see that the recursive m (I, j) algorithm Knapsack requires O (nc) computing time; Traceback requires O (n) computing time; algorithm generally requires O (nc) computing time. When the size of a backpack is large, the algorithm requires a large amount of computing time. For example, when c> 2 ^ n, the algorithm requires Ω (n2 ^ n) to calculate the time. 4. algorithm improvement: the recursive formula m (I, j) is easy to prove. In general, for each definite I (1 ≤ I ≤ n ), the m (I, j) function is a tiered Monotonic function of variable j. The hop point is the descriptive feature of this type of function. In general, the m (I, j) function is uniquely identified by all its skip points .. For each identified I (1 ≤ I ≤ n), use a table p [I] to store all the Skip points of the function m (I, j. Table p [I] can be computed recursively and recursively by table p [I + 1] Based on m (I, j, initial p [n + 1] = {(0, 0 )}. Example: n = 3, c = 6, w = {4, 3, 2}, v = {5, 2, 1 }. Function m (I, j) is calculated by max operations on function m (I + 1, j) and function m (I + 1, j-wi) + vi. Therefore, all the hop points of function m (I, j) are included in function m (I + 1, j) p [I + 1] and q [I + 1] of the function m (I + 1, j-wi) + vi. Yi Zhi, (s, t) ε q [I + 1] When and only when wi <= s <= c and (s-wi, t-vi) ε p [I + 1]. Therefore, it is easy for p [I + 1] to determine the Skip Point Set q [I + 1] as follows: q [I + 1] = p [I + 1] percentile (wi, vi) = {(j + wi, m (I, j) + vi) | (j, m (I, j) ε p [I + 1]} on the other hand, set (a, B) and (c, d) to two hops in p [I + 1] ∪ q [I + 1, when c> = a and d <B, (c, d) is controlled by (a, B), SO (c, d) is not the hop point in p [I. Except for the controlled hop point, all the hop points in p [I + 1] Qaq [I + 1] are the hop points in p [I. It can be seen that, recursively calculated from the p [I + 1] Table p [I], q [I + 1] can be calculated from p [I + 1] first. merge the tables p [I + 1] and q [I + 1], and clear the controlled hop points to obtain the table p [I]. Example: n = 5, c = 10, w = {2, 6, 5, 4}, v = {6, 3, 5, 4, 6 }. The calculation process of the hop point is as follows: initial p [6] = {()}, (w5, v5) = ). Therefore, q [6] = p [6] partition (w5, v5) = )}. P [5] = {(0, 0), (4, 6 )}. Q [5] = p [5] partition (w4, v4) = {(5, 4), (9, 10 )}. From the Union of p [5] and q [5] of the Skip Point Set p [5] ∪ q [5] = ), ()}, the hop point () is controlled by the hop point ). After the controlled hop point () is cleared, p [4] = {(), ()} is obtained )} q [4] = p [4] Random (6, 5) = {(6, 5), (10, 11)} p [3] = ), (), ()} q [3] = p [3] Random (2, 3) = {(2, 3), (6, 9 )} p [2] = )} q [2] = p [2] records () = )} p [1] = {(), ()} The last hop point of p [1) the optimal value is m (1, c) = 15. The specific code is implemented as follows: [cpp] // 3d10-2 dynamically plan the optimization of the knapsack problem jump point # include "stdafx. h "# include <iostream> using namespace std; const int N = 4; template <class Type> int Knapsack (int n, Type c, Type v [], type w [], int ** p, int x []); template <class Type> void Traceback (int n, Type w [], Type v [], type ** p, int * head, int x []); int main () {int c = 8; int v [] = {, 3 }, w [] = {, 3}; // subscript starting from 1 int x [N + 1]; int ** p = new int * [50]; for (in T I = 0; I <50; I ++) {p [I] = new int [2];} cout <"the weight of the item to be loaded is: "<endl; for (int I = 1; I <= N; I ++) {cout <w [I] <" ;}cout <endl; cout <"values of items to be loaded:" <endl; for (int I = 1; I <= N; I ++) {cout <v [I] <"" ;}cout <endl; cout <"maximum value of a backpack:" <Knapsack (N, c, v, w, p, x) <endl; cout <"ID of the backpack:" <endl; for (int I = 1; I <= N; I ++) {if (x [I] = 1) {cout <I <"" ;}} cout <endl; for (int I = 0; I <50; I ++) {delete p [I];} delete [] P; return 0;} template <class Type> int Knapsack (int n, Type c, Type v [], Type w [], int ** p, int x []) {int * head = new int [n + 2]; head [n + 1] = 0; p [0] [0] = 0; // p [] [0] storage item weight p [0] [1] = 0; // p [] [1] storage item value, the hop point of item n (0, 0) // left points to the first hop point of p [I + 1], and right points to the last hop point. // take the example in the book as an example, if p [3] = 0, left points to the first hop point of p [4] (0 0) right points to (9, 10) int left = 0, right = 0, next = 1; // next refers to the location where the next hop point is to be stored head [n] = 1; // head [n] is used to point to the position of the first hop point of the nth item for (int I = n; I> = 1; I --) {int k = left; // k points to the hop point in p [] and moves k to determine p [] and p [] + (w v) for (int j = left; j <= right; j ++) {if (p [j] [0] + w [I]> c) break; // The remaining space cannot be loaded with I, and the for loop is exited. Type y = p [j] [0] + w [I], m = p [j] [1] + v [I]; // calculate p [] + (w v) // If p [k] [0] is small, (p [k] [0] p [k] [1]) is definitely not controlled, store it as a hop point for p [I] while (k <= right & p [k] [0] <y) {p [next] [0] = p [k] [0]; p [next ++] [1] = p [k ++] [1];} // If p [k] [0] = y and m <p [k] [1], then (y m) if (k <= right & p [k] [0] = y) {I F (m <p [k] [1]) // pair (p [k] [0] p [k] [1]) judge {m = p [k] [1];} k ++ ;} // If p [k] [0]> = y and m> = p [k] [1], determine if it is the controlled point of the last hop point of the current I. // if not, store if (m> p [next-1] [1]) for the hop point of the I. {p [next] [0] = y; p [next ++] [1] = m;} // If so, judge the next element. While (k <= right & p [k] [1] <= p [next-1] [1]) {k ++ ;}} while (k <= right) {p [next] [0] = p [k] [0]; p [next ++] [1] = p [k ++] [1]; // store the rest of I + 1 as the I hop point} left = right + 1; right = next-1; // position of the first hop point of the item in the I-1 head [0] refers to the position of the first hop point of The 0th item in the head [I-1] = next;} Traceback (n, w, v, p, head, x); return p [next-1] [1];} // x [] array stores the 0-1 vector of the corresponding item, 0 is not loaded into the backpack, 1 indicates loading the backpack template <class Type> void Traceback (int n, Type w [], Type v [], Type ** p, int * head, Int x []) {// initialize j. m is the 0th column and 1st column corresponding to the last hop point. // The last hop point obtained in the preceding example is (8 15) j = 8, m = 15 Type j = p [head [0]-1] [0], m = p [head [0]-1] [1]; for (int I = 1; I <= n; I ++) {x [I] = 0; // initialize the array; for (int k = head [I + 1]; k <= head [I]-1; k ++) // The initial k points to the first hop point of p [2] (0 0) {// determines whether the item I is loaded. For example, the above and the hop point (6 9) sum equal to (8 15) therefore, if (p [k] [0] + w [I] = j & p [k] [1] + v [I] = m) {x [I] = 1; // If item I is loaded, set x [I] to 1 j = p [k] [0]; // set the j and m values to the values of m = p [k] [1] corresponding to the jump point that meets the if condition. // in the preceding example, j = 6, m = 9 break; // then judge An item }}} the main calculation workload of the above algorithm is to calculate the Skip Point Set p [I] (1 ≤ I ≤ n ). Since q [I + 1] = p [I + 1] percentile (wi, vi ), therefore, calculating q [I + 1] requires O (| p [I + 1] |) computing time. Merging p [I + 1] and q [I + 1] and clearing controlled jump points also requires O (| p [I + 1] |) to calculate the time. From the definition of the Skip Point Set p [I], we can see that the jump point in p [I] corresponds to xi ,..., Assign a value to 0/1 of xn. Therefore, the number of hop points in p [I] cannot exceed 2 ^ (n-I + 1 ). It can be seen that the computing time taken by the algorithm to calculate the Skip Point Set p [I] is so, and the complexity of the improved algorithm's computing time is O (2 ^ n ). When the given item's weight wi (1 ≤ I ≤ n) is an integer, | p [I] | ≤ c + 1, (1 ≤ I ≤ n ). In this case, the time complexity of the improved algorithm is O (min {nc, 2 ^ n }). Running result

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.