01 Dynamic Programming Method for Solving knapsack problems

Source: Internet
Author: User

01 Dynamic Programming Method for Solving knapsack problems

 

I. Problem description:

There are n items, each of which has its own weight and value. There is a backpack with a total capacity of v. Now we have selected a number of items from these N types of products and placed them in a backpack. Each product is required to be placed at most once, so that the total value of the items in the backpack is the maximum and the total weight of the items does not exceed the total size of the backpack, how do I select products?

Ii. Solutions

Consider putting the I-th item into the state of the backpack, at this time, there are I-1 items in the backpack, the greatest value is f [v]. The greatest value of a backpack should be max {f [v], F [V-W [I] + C [I]}.

V is the size of the backpack at this time. V is variable, from 0 to v. W [I] is the weight of the I-th item, and C [I] is the value of the I-th item. That is to say, the greatest value of putting the I-th item into a backpack depends on the status of the backpack and the I-th item itself.

Three Dynamic Programming Method

# Include <iostream>

# Include <vector>

# Include <set>

# Include <cstdlib>

Using namespacestd;

 

Class commodity // product class

{

PRIVATE:

Int sequence; // product serial number

Int weight; // item weight

Int cost; // product value

Public:

Static const int N; // Total number of items

Commodity (int s = 0, int W = 0, int C = 0)

{

Sequence = s;

Weight = W;

Cost = C;

}

Commodity (const commodity & C)

{

Sequence = C. sequence;

Weight = C. weight;

Cost = C. cost;

}

Const commodity & operator = (constcommodity & C)

{

Sequence = C. sequence;

Weight = C. weight;

Cost = C. cost;

Return * this;

}

Inline int getsequence () const

{

Return sequence;

}

Inline int getweight () const

{

Return weight;

}

Inline int getcost () const

{

Return cost;

}

Inline void setsequence (int s = 0)

{

Sequence = s;

}

Inline void setweight (int w = 0)

{

Weight = W;

}

Inline void setcost (int c = 0)

{

Cost = C;

}

};

 

Const intcommodity: n = 6;

 

Class bag

{

PRIVATE:

Int volume; // total backpack capacity

Vector <commodity> commodityset; // commodity set

Typedef struct tag // struct involved in key algorithms

{

Int V; // v = Volume... 0

Int value; // value indicates the maximum value of the product in the backpack. For example, V = 10 and value = 8 indicate that the size of the backpack is 10, and the maximum value of the product is 8.

Set <int> sequence; // The product number that is loaded into the backpack when the maximum value is reached.

};

Vector <tag> tag;

Bool addtobag (int v, int sequence) // determines whether the sequence product can be added to the backpack.

// V indicates the backpack capacity V = volume...

{

If (V> = commodityset [sequence]. getweight () return true;

Else return false;

}

Bool alterbagvalue (intv, vector <tag>: reverse_iterator it, int sequence) // modify the maximum value of the backpack, provided that the addtobag function returns true

{

Intreminder = V-commodityset [sequence]. getweight (); // the weight of the backpack that is not placed in front of the I-th item

If (IT-> value <tag [Reminder]. Value + commodityset [sequence]. getcost () // put the I-th item into the backpack.

{

It-> value = tag [Reminder]. Value + commodityset [sequence]. getcost ();

Return true; // The backpack is modified, and the function returns true.

}

Else return false; // The backpack is not modified. The function returns false.

}

Void addcommoditytobag (intv, vector <tag>: reverse_iterator it, int sequence) // Add a commodity to the backpack

{

Intreminder = V-commodityset [sequence]. getweight ();

Set <int>: iterator it1;

It-> sequence. Clear (); // It is very important to clear the current product number

For (it1 = tag [Reminder]. sequence. Begin (); it1! = Tag [Reminder]. sequence. End (); it1 ++)

{

It-> sequence. insert (* it1 );

}

It-> sequence. insert (sequence );

}

Void showcommodity (INT sequence) // display the product information of Sequence

{

Ints = commodityset [sequence]. getsequence ();

Intw = commodityset [sequence]. getweight ();

Intc = commodityset [sequence]. getcost ();

Cout <"Product Information in the backpack :";

Cout <"No.:" <S <"";

Cout <"Weight:" <W <"";

Cout <"value:" <C <Endl;

}

Public:

Bag (int v): volume (V)

{

Int I;

Int W, C;

For (I = 0; I <commodity: N; I ++)

{

Cout <"Enter the" <I <"weight of a commodity :";

Cin> W;

If (! Cin. Good () Exit (1 );

Cout <"Enter the" <I <"commodity value :";

Cin> C;

If (! Cin. Good () Exit (1 );

Commodity Commodity (I, W, C );

Commodityset. push_back (commodity );

}

For (I = 0; I <= volume; I ++)

{

Tag T;

T. v = I;

T. value = 0;

T. sequence. Clear ();

Tag. push_back (t );

}

}

Void solution () // use the Dynamic Programming Method to Solve the Knapsack Problem

{

Int I;

Int V;

Vector <tag>: reverse_iteratorit; // reverse iterator

For (I = 0; I <commodity: N; I ++)

{

For (V = volume, it = tag. rbegin (); addtobag (v, I) & it! = Tag. rend (); It ++, V --)

{

// The product should be added only when the backpack is modified

If (alterbagvalue (V, it, I) addcommoditytobag (V, it, I );

}

}

}

Void showresult ()

{

Vector <tag >:: reverse_iteratorit;

Set <int >:: const_iterator it1;

Int V, value;

For (IT = tag. rbegin (); it! = Tag. rend (); It ++)

{

V = it-> V;

Value = it-> value;

Cout <"when the weight of the backpack is" <v <", the greatest value of the goods in the backpack is" <value <Endl;

For (it1 = it-> sequence. Begin (); it1! = It-> sequence. End (); it1 ++)

{

Showcommodity (* it1 );

}

}

}

};

 

Void main ()

{

Bag (40 );

Bag. solution ();

Bag. showresult ();

}

4. Test

The size of the backpack is 40. Select data for testing.

When the backpack capacity is 30

Select data for test

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.