Dynamic Programming Method: Knapsack problem

Source: Internet
Author: User

A few concepts:

Optimization problem: There are n inputs, whose solution consists of a subset of these n inputs, which must satisfy certain predetermined conditions, which are called constraint conditions, which satisfies the solution of the constraint condition called the problem. There may be more than one feasible solution to meet the constraint conditions, in order to measure the pros and cons of these feasible solutions, some criteria are given in advance, these standards are usually given in the form of functions, these standard functions are called objective functions, so that the feasible solution of the objective function to obtain extremum is the optimal solution, which is called the optimization problem.

The principle of two optimality:

For an optimization problem with n input, the process of solving it can be divided into several stages, the decision of each stage depends only on the state of the previous stage, the action taken by the decision makes the state shift, and becomes the basis of the next stage. Thus, a decision sequence is generated in a constantly changing state. The process of this decision sequence is called the multistage decision process.

In each stage of decision-making there is a strategy or goal that depends on the decision, which is determined by the nature and characteristics of the problem, usually expressed in the form of a function and has a recursive relationship, called dynamic programming function.

The multi-stage decision process satisfies the optimality principle: Regardless of the initial state of the decision-making process and the initial decision, the remaining decision must be relative to the current state of the initial decision and constitute an optimal decision sequence.

If a problem satisfies the optimality principle, the problem is usually called the optimal substructure property.

Three characteristics

We know that dynamic programming is an effective way to solve the global optimal solution, and the problem that can be solved by dynamic programming algorithm has the following two notable features:

It has the best substructure property, that is to say, the optimal solution can be defined recursively.

Can be decomposed into several self problems that overlap each other.

The optimal solution also contains the optimal solution of the sub problem.

Four design scheme

Design scheme of dynamic programming method

Segmentation: The original problem is decomposed into several overlapping sub problems;

Analysis: To analyze whether the problem satisfies the optimal substructure property and find the recursive formula of dynamic programming function;

Solution: The dynamic programming process is realized by using the recursive bottom-up calculation.

501 knapsack problem

0/1 knapsack problem, the item I either is put into the backpack, or is not put into the backpack, set Xi to express the case of the goods I loaded into the backpack, then when xi=0, the expression goods I are not loaded into the backpack, xi=1, the expression goods I is loaded into the backpack. According to the requirements of the problem, there are the following constraints and objective functions:

Fig. 1 Constraint conditions

Figure 2 Objective function

01 The state transition equation of the backpack f[i,j]= max{F[i-1,j-wi]+pi (J >= Wi), F[i-1,j]}

F[I,J] represents the maximum value that can be achieved by selecting a number of pieces in the first I item to be placed in a backpack bearing J.

PI represents the value of article I.

Decision: In order to maximize the total value of the items in the backpack, should I put the item in the backpack?

Six dynamic programming approach solutions

There are five items numbered respectively a,b,c,d,e, their weight is 2,2,6,5,4, their value is 6,3,5,4,6, now give you a backpack bearing 10, how to let the backpack loaded items have the greatest value combined

Table 10/1 Dynamic Programming method for knapsack problem

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.