Backpack DP: Backpack Nine learning record

Source: Internet
Author: User

01 Backpack:
{
V C[i] W[i]
Two-dimensional:

F[i,j]= the maximum value of the first I item in a backpack with a capacity of V
~~~~~~
Do not select Article I item f[i,j]:=f[i-1,j];
Select item I f[i,j]:=f[i-1,j-c[i]]+w[i];
F[I,J]:=MAX{F[I-1,J],
F[i-1,j-c[i]]+w[i]}


One-dimensional:

Maximum value of f[v]= in a backpack with a capacity of V
F[v]:=max{f[v],f[v-c[i]]+w[i]}

For I:=1 to N do
For j:=v Downto 0 do
F[j]:=max{f[j],f[j-c[i]]+w[i]}

Or

For I:=1 to N do
For j:=v Downto 0 do
F[i,j]:=max{f[i-1,j],f[i-1,j-c[i]]+w[i]}

————————————————
Misunderstanding:
Note that f[i][v] is meaningful when and only if there is a subset of the first I item,
The sum of its costs is v. So after the recursive completion of this equation,
The final answer is not necessarily f[n] [V],
But the maximum value of f[n][0..v].
If you remove the word "just" from the definition of the state,
In the transfer equation, one more F[i][v-1] will be added,
This will ensure that f[n] [V] is the final answer.
————————————————

}


Full backpack
{

There are n items and a backpack with a capacity of V
The cost of article I items is c[i]
Value is W[i]
Each piece is an infinite number of pieces

F[i,j]= the maximum value of the first I item in a backpack with a capacity of V
~~~~~~
F[i,j]:=max{f[i-1,j-k*c[i]]+k*w[i] | 0<=k*c[i]<=v}

Optimization:
1: If Item I and item J C[i]<c[j] and W[i]>w[j], remove J
2: Convert to 01 backpack
As the item I is the most selected v/c[i] pieces, then the article I item to be converted into
(V/c[i]) pieces I items
Basic idea: To split an item into multiple items

O (N*V)
For I:=1 to N do
For j:=0 to V do
F[i,j]:=max{f[i-1,j],f[i-1,j-c[i]]+w[i]}

Or

For I:=1 to N do
For j:=0 to V do
F[j]:=max{f[j],f[j-c[i]]+w[i]}

Misunderstanding:
O (N*V) algorithm principle is not understood

}


Multiple backpack
{

There are n items and a backpack with a capacity of V,
The cost of item I is c[i]
Value is W[i]
Up to n[i] pieces

The first way of thinking:
For the first item there is (N[I]+1) a decision
...... Do not choose, choose one, choose two pieces 、... 、 n[i] Pieces
So, the transfer equation can be obtained
F[i,j]:=max{f[i-1,j-k*c[i]]+w[i]*k | 0<=k<=n[i]}
Complexity O (V*siga (n[i))

A simple second way of thinking
Converted to 01 backpacks. Convert item I to n[i] pieces I items
And then there's a 01 backpack.

Misunderstand:
Something about binary and monotone queues

}

Mixed backpack
{
The above three kinds of backpack mix, some items can only take one,
Some items are preferable to an unlimited number of times, and some have an upper limit


It feels like it's not too hard.

For I:=1 to N do
Begin if it's a 01 backpack.
For j:=v Downto 0 do
F[i,j]:=max{f[i-1,j],f[i-1,j-c[i]]+w[i]};
If it's a full backpack,
For j:=0 to V do
F[i,j]:=max{f[i-1,j],f[i-1,j-c[i]]+w[i]};
If it's a multi-pack
For j:=v Downto 0 do
Begin for K:=0 to V/c[i] do
If f[i,j]<f[i-1,j-k*c[i]]+ (w[i]*k)
Then f[i,j]:=f[i-1,j-k*c[i]]+ (w[i]*k)
End
End
}

Two-dimensional backpack for cost
{
Question: For each item, there are two costs,
If you choose this kind of item, you have to pay for these two costs

A[i],b[i] Price
V[i] Benefits
F[I,J,K] The maximum benefit of the first I-item payment j,k

F[i,j,k]:=max{f[i-1,j,k],
F[i-1,j-a[i],k-a[i]]+v[i]}

For I:=1 to N do
For j:=v Downto 0 do
F[i,j,k]:=max{f[i-1,j,k],
F[i-1,j-a[i],k-a[i]]+v[i]}

Often with implicit conditions

01 Limit the number of selected bags
b[i]=1//of each item

}

**
Group Backpack
{
There are n items, and a backpack with a capacity of V
Divided into k groups, each group of objects conflicting {select only one}

f[i,j]{the cost of items in the first I group is the greatest benefit of J.}

F[i,j]:=max{f[i-1,j],f[i-1,j-v[i][k]] | 0<=k<= number of items in Group I}

F[J]:=MAX{F[J],F[J-V[I][K] | 0<=k<= number of items in Group I}

For I:=1 to N do
For j:=v Downto 0 do
Number of items for k:=1 to Group I

}

***
A backpack that depends on
{
Test instructions
dependencies exist between items
Simplified:
1. There is no item dependent on more than one piece of goods
2, does not exist one item and depends on other items, has been other items lock dependent
Items that are not dependent on other items are called main pieces
Dependent on other items called attachments

All items consist of {One main piece and multiple attachments} collection

Suppose there are N sets {have c[i] attachments}
Option: 1, do not select the main parts
2. Select Main parts only
3. Select the main part and an attachment {C[i] type}
4. Select the main part and two attachments {}
5. Select the main part and three attachments {}
.
.
.
.
.
C[i]+2, selection of main parts and C[i] attachments {1}
Total is 2^c[i]+1

Extend an item to a 2^c[i]+1 piece

A simple optimization: For a combination of the same cost in a set, only one of the most profitable

}

***
Generalization backpack
{
Test instructions
In a backpack problem with a backpack capacity of V, the V and W of generalized items have a strict relationship
When the cost assigned to him is w[i], the interest is v[i]

Feeling it means that every knapsack problem can be seen as a generalization backpack
01 Backpack, when the cost is w[i], the interest is v[i]; The rest of the time v[i]=0
Full backpack, when the cost W=w[i]*k{0<=k<=v/w[i]} interest is v[i]*k;
The rest of the time v[i]=0;
Multiple backpacks, when the cost W=w[i]*k{0<=k<=n[i]} interest is V[i]*k
The rest of the time v[i]=0;

When only two items of generalization are q,p
F[i]=q[i]+p[v-i];
Maximum benefit is max{f[i]} {0<=i<=v}

}

Backpack DP: Backpack IX Learning record

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.