DP backpack problem + Memory Recursion

Source: Internet
Author: User
Two questions:

1) Dynamic Planning solution for knapsack problems
2) Another Implementation Method of Dynamic Planning, memory recursion (mentioned in the first article of DP, also mentioned in professional). I will explain it here and add all the articles in detail, this can be regarded as a dynamic rule.

The last one is not detailed.

Certificate ---------------------------------------------------------------------------------------------------------------------------------------------------

Given a group of items:

The weight is W1, W2,... wn.

Value: V1, V2,... VN

And a bag weighing W.

A most valuable subset of these items can be packed into schoolbags.

Certificate --------------------------------------------------------------------------------------------------------------------------------------

1. Backpack Problems

Set V [I, j] to indicate the value of the most valuable subset of the first I item that can be placed into a backpack weighing J. The goal is to evaluate V [N, w].

The following recursive formula is obtained based on whether the best subset of V [I, j] contains item I:

This recursive formula means that if I has a larger weight than J, it obviously does not include I (J-wi <0). If I has a smaller weight than J, it may contain I (if it can go to the maximum value ).

This table can be filled by row or column:

Implementation:

Package section8;

/* Chapter 8 about Dynamic Planning of backpacks */

Public class backpack {

/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
// Todo auto-generated method stub
Int [] W = {2, 1, 3, 2}; // weight array
Int [] V = {12, 10, 20, 15}; // value Array
Int W = 5;

Int [] [] result = backpack (W, V, W );

For (INT I = 0; I <result. length; I ++)
{
For (Int J = 0; j <result [0]. length; j ++)
System. Out. Print (result [I] [J] + "");
System. Out. println ();
}
}

Public static int [] [] backpack (INT [] W, int [] V, int W ){
// W is the item weight array, V is the item value array, and W is the backpack weight.
// Return the matrix that expresses the Solution Process of the Knapsack Problem
Int n = W. length; // The length of W and V is the same
Int [] [] result = new int [n + 1] [W + 1]; // The first I items (I am from 0 to N), and W is from 0 to W

// Initial condition: result [0] [J] = 0; Result [I] [0] = 0;
For (INT I = 0; I <= W; I ++)
Result [0] [I] = 0;
For (INT I = 0; I <= N; I ++)
Result [I] [0] = 0;

// Fill in the table based on the state transition equation of the dynamic plan: This table can be filled in one row or one column. Here, you can fill in one row and one row.
// Note that filling in a table is very important in dynamic planning. When you fill in a position, it must be filled in for other locations.
// Therefore, the table filling method is related to the state transition equation. in depth, it is related to the generation process of the Dynamic Planning constructor solution.

For (INT I = 1; I <= N; I ++) // The number of rows ranges from 1 to n.
{
For (Int J = 1; j <= W; j ++) // The number of columns ranges from 1 to W.
{
// This determines an I. The position of J must be filled in: result [I] [J].
If (J-W [I-1] <0)
Result [I] [J] = Result [I-1] [J];
Else
Result [I] [J] = max (result [I-1] [J], V [I-1] + result [I-1] [J-W [I-1]);
}
}

Return result;
}

Public static int max (int m, int N ){
If (M> = N)
Return m;
Return N;
}

}

Result:

0 0 0 0 0 0
0 0 12 12 12 12
0 10 12 22 22 22
0 10 12 22 30 32
0 10 15 25 30 37

After writing these several dynamic plans, we can see that with the recursive formula, the programming is very easy, basically by operating the matrix according to the formula.

Therefore, we should focus on the idea of dynamic planning, how to describe and portray problems, find the most sub-structure, and obtain the state transition equation.

Certificate -------------------------------------------------------------------------------------------------------------------------------------------------

2. Memory recursion-another Implementation Method of Dynamic Planning

What is memory recursion, what is its starting point, and why it needs to be remembered recursion? I have mentioned this before. Here I will briefly describe it:

One of the core ideas of dynamic planning is to remember (or record) to avoid solving replay subproblems. However, it does not avoid solving unnecessary subproblems.

If recursion and dynamic planning are combined, a recursive implementation method of dynamic planning can be obtained, which avoids solving unnecessary subproblems.

In fact, in recursion, in the dynamic planning table, first check whether the item to be recursive has been obtained. If the item has been obtained, the answer is returned directly; otherwise, the recursive solution is used.

Recursive Implementation of the memory of the knapsack problem:

In fact, it is only when V [I, j] has not been obtained (once a V [I, j] has been obtained, it will not be changed. This is very important ), otherwise, the return value is returned directly.

Think about the Fibonacci series.

Certificate -------------------------------------------------------------------------------------------------------------------------------------------------

Summary:

1) the time complexity and space complexity of the knapsack problem are both NW and two cycles. For details, see the code.

2) one key point: Another Implementation Method of Dynamic Planning, memory Recursion

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.