Dynamic programming with 0 1 backpack issues

Updated on educate 2024-03-18
6 answers
  1. Anonymous users2024-02-06

    01 Backpack 2 states.

    A backpack can only be taken or not taken.

    The first i backpack to fit the space of J.

    Consider 2 cases f[i,j]:=max ( f[i-1,j],f[i-1,j-v[i]]+w[i])

    f[i-1,j] means that the ith is not taken, then f[i,j] is the same as the first i-1 j.

    f[i-1,j-v[i]]+w[i] indicates that the ith is taken and the first i-1 is loaded with j-v[i] (means that the former i-1 is loaded with j-v[i]).

    And then add the value of the ith one.

    Take the largest of the two.

    fillchar(a,sizeof(a),0);

    for i:=1 to n do

    for j:=1 to m do

    if (j-v[i]>=0)and( [i-1,j-v[i]]+w[i]> f[i-1,j]) then f[i,j]:=[i-1,j-v[i]]+w[i]

    else f[i,j]:=f[i-1,j];

    writeln(f[n,m]);

    Initialize the full assignment to 0 and the array starts at 0.

    You can go and see the backpack 9 lectures that the library has.

    Just go to RQ or Tyvj and do some questions, you have to do them.

  2. Anonymous users2024-02-05

    What is the basic idea of dynamic programming? For the 0-1 backpack problem, w=(1,2,3,4,5),v=(15, 20, 25,30, 35, 18), and the backpack weight is c=6.

    The basic idea of dynamic programming is to divide a complex problem into multiple sub-problems, using solutions to smaller sub-problems to solve the larger problems. For the 0-1 backpack problem, the dynamic programming algorithm can be used to find the optimal solution, and the following recursive formula is established: f(i,j)=max, where f(i,j) represents the optimal solution for the first i items to be put into the backpack with capacity j; w(i) and v(i) indicate the weight and value of the ith item.

    According to this formula, it was found that the maximum value of an item loaded in a backpack with a capacity of 6 was 38.

  3. Anonymous users2024-02-04

    What is the idea of the basis of dynamic programming? For the 0-1 backpack problem, w=(1,2,3,4,5),v=(15, 20, 25,30, 35, 18), the backpack bearing weight is c=6, and the dynamic programming is used to find the 0-1 backpack to ask the high question.

    Hello, glad to answer for you. The basic idea of dynamic programming is to decompose a complex problem into a series of small problems that are related to each other and try to solve the optimal solution. 0-1 Backpack Issue:

    The array m[i][j] can be used to represent the array m[i][j], i represents the first i items, j represents the weight, and m[i][j] indicates the maximum value that can be loaded when there are i items and the weight does not exceed j. m[5][6]= max(m[4][5], m[4][6], m[4][5]-w[5]+v[5]), to obtain the maximum value of the cluster is 38, and the optional commodity is modulo row 1, 2, 4

  4. Anonymous users2024-02-03

    Summary. The 0-1 backpack problem is described as follows: N items and a backpack are given.

    The weight of item i is wi, its value is vi, and the capacity of the backpack is c. How do you choose what to pack in your backpack so that you can maximize the total value of the items in your backpack? When choosing what to pack in a backpack, there are only 2 choices for each item, what is the idea of dynamic programming?

    For the 0-1 backpack problem, w=(1,2,3,4,5),v=(15, 20, 25,30, 35, 18), the backpack bearing weight is c=6, and the dynamic programming is used to find the 0-1 backpack to ask the high question.

    The 0-1 backpack problem is described as follows: Given n items, Mintan and a backpack. The weight of item i is wi, its value is vi, and the capacity of the backpack is c.

    How do you choose what to pack in your backpack so that you can maximize the total value of the items in your backpack? When it comes to choosing what to put in your backpack, you only have 2 options for each item, which is awesome! Can you elaborate on that?

    It doesn't matter if x1 is 0 or 1, [x2 ,.]xn ] must be an optimal solution after the filial piety code of the first decision, if not, there will be a better solution [y2,.].,yn ], thus [x1,y2,pishen bureau.

    yn] is a better case for Fang Ran. Suppose n=3, w=[100,14,10], p=[20,18

  5. Anonymous users2024-02-02

    * A traveler has a backpack that can use up to m kg and now has n items, and their weights are W1, W2 ,..wn, their values are p1, p2 ,..pn.

    If there is only one item of each item, the traveler will receive the maximum total value.

    Input format: m, nw1, p1w2, p2

    Output Format: x *

    Because the maximum capacity of the backpack m is unknown. So, our program has to be tried one by one from 1 to m. For example, start by selecting one of the n items.

    If you can put it in the backpack corresponding to M, and if you can put it in and there is more space, then the maximum value of the N-1 item can be placed in the extra space. How can you be sure that the total choice is the best value? Take a look at the table below. Test data:

    The 5,6c[i][j] array holds the maximum value of items 1, 2, and 3 after being selected sequentially.

    How did you get this greatest value? Starting from the backpack capacity of 0, item 1 is tried first, and the capacity of 0, 1, and 2 cannot be placed. So set 0, the backpack capacity is 3, then put 4 in it

    In this way, this row of backpacks has a capacity of 4,5,6 ,..At 10, the best solution is to put 4Suppose item 1 is placed in a backpack.

    Then let's look at item 2. When the backpack capacity is 3, the best plan is still the most expensive plan c in the previous row is 4When the backpack capacity is 5, the best solution is your own weight 5

    When the backpack capacity is 7, it is obvious that 5 is added to a value. Add whom? Obviously 7-4 = 3.

    The best solution for the previous row of C3 is 4So. The overall optimal scenario is 5+4 for 9

    Such. Row by row push down. The data that is decentralized to the far right is the most valuable.

    Note that when the backpack capacity of the 3rd row is 7, the best solution is not the 6 itselfIt's the 9 in the previous rowIndicates that item 3 was not selected at this time.

    Items 1 and 2 were chosen. So get 9)

    This can be seen from the construction process of the greatest value above.

    f(n,m)=maxThis is the dynamic programming equation written in the book. Is it clear this time?

    Here's the actual procedure:

    #include

    int c[10][100];*Corresponding to the maximum value of each case* int knapsack(int m,int n)else c[i][j]=c[i-1][j];

    return(c[n][m]);

    int main()

    system("pause");}

  6. Anonymous users2024-02-01

    Dynamic programming mainly solves the problem of multi-stage decision-making.

    01 In the backpack, the status is the remaining capacity of the backpack, the stage is each item, and the decision is whether to choose the current item.

    Therefore, it is very appropriate to use dynamic programming to solve the problem.

    Let's let f[v] denote the maximum value that can be obtained if the used capacity is v, w[i] denotes the quality of the i-item, and c[i] denotes the value of the i-item.

    for(int i=1;i<=n;i++)

    for(int j=v;j>=w[i];j--)

    f[j]=max(f[j],f[j-w[i]]+c[i]);

    This is known as a state transfer equation.

    f[j] indicates the maximum value when the used capacity is j, and f[j-w[i]] indicates the maximum value when the used capacity is j-w[i].

    f[j] can be transferred from the state f[j-w[i]] to the point of attainment, which means that the item w[i] is selected and thus the value is c[i].

    Each time, f[j] will decide whether to choose or not to select the optimal solution.

    From the local optimal of each item, that is, of each stage, the final global optimal is deduced. This solves the 01 backpack problem.

Related questions
6 answers2024-03-18

There are n items and a backpack with a volume of m. The volume of the ith article is w[i] and the value is d[i]. Solve which items to pack in your backpack to maximize the sum of your values. >>>More

9 answers2024-03-18

The first step is to open [Settings] - [Wallpaper] on your iPhone; >>>More

6 answers2024-03-18

The content, method, and method of network planning

Network planning overview. >>>More

21 answers2024-03-18

We compare the engine displacement, maximum power, torque, and comprehensive fuel consumption of the Ministry of Industry and Information Technology of the Lynk & Co 01 and Qashqai to provide a reference for your car selection. >>>More

4 answers2024-03-18

(1) Formulate and implement urban economic and social development strategies and plans; (3) Continuously adjust and optimize the economic structure of the city; (4) effective control; (5) Strengthen the indirect management and service of the economic activities of enterprises;