0-1 backpack and backpack problems (implemented in C Language) -- greedy algorithm application (3)

Source: Internet
Author: User

Problem description:
N items and a backpack are given. The Weight of item I is w [I], the value is v [I], and the size of the backpack is c. How should we select the items to be loaded into the backpack to maximize the total value of the items in the backpack. Each item can be loaded at most once.


0-1 backpack problem: there are only two options for items to be loaded into the backpack: all or not.
Backpack problem: for items to be loaded into the backpack, you can choose to load part of the package, not all of them into the backpack.


Algorithm analysis:
When using greedy policies to solve such problems, we must first select the optimal measurement standards.
There are three available measurement standards: value, capacity, and unit value (v/w, value/weight ).
Obviously, the size of high-value items may be too large, and the value of large items may be very low. The Optimal measurement criterion is the unit value.


Knapsack Problem algorithm ideas:
1. Sort items by unit value from high to low;
2. Put the person with the highest value into the backpack;
3. Calculate the remaining space of the backpack;
4. Repeat 2-3 steps until the remaining size of the backpack is 0 or all items are loaded into the backpack (for 0-1 backpacks, the termination condition is that the remaining capacity of the backpack cannot be loaded into any item or all items into the backpack ).


The following is the c language implementation (DEV c ++ 4.9.2 runs through)


[Cpp]
# Include <stdio. h>
 
Void package (int n, float c, float v [], float w [], float x []);
Void package0_1 (int n, float c, float v [], float w [], float x []);
 
Int main (void)
{
Int n = 3;
Float c = 20;
Float v [] = {24, 15, 25 };
Float w [] = {15, 10, 18}; // sorted by unit value in descending order
Float * x;
X = (float *) malloc (sizeof (float) * n );
Printf ("******** backpack ******** \ n ");
Package (n, c, v, w, x );
Printf ("********* 0-1 backpack ****** \ n ");
Package0_1 (n, c, v, w, x );
System ("PAUSE ");
 
}
 
/*
* Backpack Problems
* N: number of items
* C: backpack capacity
* V []: The value of each item
* W []: the weight of each item (listed in descending order of unit value)
* X []: whether to put an item into a backpack (0 indicates not to put it, 1 indicates to put it all, and 0-1 indicates to put a part)
*/
Void package (int n, float c, float v [], float w [], float x [])
{
Int I;
For (I = 0; I <n; I ++)
{
X [I] = 0; // initial status. All items are not placed in the backpack.
}

For (I = 0; I <n; I ++)
{
If (w [I]> c)
{
Break;
}

X [I] = 1;
C = c-w [I];
Printf ("put % d items, remaining backpack capacity % f. \ n", (I + 1), c );
}

If (I <= n) // you can add a part of an item.
{
X [I] = c/w [I];

Printf ("add % f of the % d item. the remaining backpack capacity is 0. \ n ", (I + 1), w [I] * x [I]);
}
}
 
/*
* 0-1 backpack Problems
* N: number of items
* C: backpack capacity
* V []: The value of each item
* W []: the weight of each item (listed in descending order of unit value)
* X []: whether to put the item into a backpack (0 indicates not to put it, 1 indicates to put it all)
*/
Void package0_1 (int n, float c, float v [], float w [], float x [])
{
Int I;
For (I = 0; I <n; I ++)
{
X [I] = 0; // initial status. All items are not placed in the backpack.
}

For (I = 0; I <n; I ++)
{
If (w [I]> c)
{
Break;
}

X [I] = 1;
C = c-w [I];
Printf ("put % d items, remaining backpack capacity % f. \ n", (I + 1), c );
}
}
 

# Include <stdio. h>

Void package (int n, float c, float v [], float w [], float x []);
Void package0_1 (int n, float c, float v [], float w [], float x []);

Int main (void)
{
Int n = 3;
Float c = 20;
Float v [] = {24, 15, 25 };
Float w [] = {15, 10, 18}; // sorted by unit value in descending order
Float * x;
X = (float *) malloc (sizeof (float) * n );
Printf ("******** backpack ******** \ n ");
Package (n, c, v, w, x );
Printf ("********* 0-1 backpack ****** \ n ");
Package0_1 (n, c, v, w, x );
System ("PAUSE ");

}

/*
* Backpack Problems
* N: number of items
* C: backpack capacity
* V []: The value of each item
* W []: the weight of each item (listed in descending order of unit value)
* X []: whether to put an item into a backpack (0 indicates not to put it, 1 indicates to put it all, and 0-1 indicates to put a part)
*/
Void package (int n, float c, float v [], float w [], float x [])
{
Int I;
For (I = 0; I <n; I ++)
{
X [I] = 0; // initial status. All items are not placed in the backpack.
}

For (I = 0; I <n; I ++)
{
If (w [I]> c)
{
Break;
}

X [I] = 1;
C = c-w [I];
Printf ("put % d items, remaining backpack capacity % f. \ n", (I + 1), c );
}

If (I <= n) // you can add a part of an item.
{
X [I] = c/w [I];

Printf ("add % f of the % d item. the remaining backpack capacity is 0. \ n ", (I + 1), w [I] * x [I]);
}
}

/*
* 0-1 backpack Problems
* N: number of items
* C: backpack capacity
* V []: The value of each item
* W []: the weight of each item (listed in descending order of unit value)
* X []: whether to put the item into a backpack (0 indicates not to put it, 1 indicates to put it all)
*/
Void package0_1 (int n, float c, float v [], float w [], float x [])
{
Int I;
For (I = 0; I <n; I ++)
{
X [I] = 0; // initial status. All items are not placed in the backpack.
}

For (I = 0; I <n; I ++)
{
If (w [I]> c)
{
Break;
}

X [I] = 1;
C = c-w [I];
Printf ("put % d items, remaining backpack capacity % f. \ n", (I + 1), c );
}
}

 


Although the problem of a backpack and a 0-1 backpack have the optimal sub-structure, the problem of a backpack is solved by the greedy algorithm. The problem of a 0-1 backpack cannot be obtained through the greedy algorithm, because it cannot be ensured that the backpack can be fully filled at last, some idle backpack space will reduce the total value.

 

Related Article

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.