Introduction to algorithms-21. Chapter 1 greedy algorithms (1) Basics 1

Source: Internet
Author: User

Speaking of greedy algorithms, we cannot avoid comparison with DP, so we need to know about the previous DP.

Greedy algorithms make the selection seem to be the best at present, and we hope to generate a global optimal solution through the local optimal selection.

Like summary DP in the previous chapter, I will first give an example of the easiest way to get started, to see if Shenma is greedy? (This algorithm is human-friendly.

=. =)

A simple example:

Some backpack problems:

There are N items, I-th item value vi, heavy wi. Now you have a package that can hold up to W lbs. You can choose to take away all or part of each item, how can I choose to maximize the value of a backpack? (Is this similar to the 01 backpack mentioned in DP earlier? Carefully check the differences between the two !)

If there are three kinds of items, the backpack can hold 50 lbs of items, the item is 1 heavy 10 lbs, the value is 60 CNY; the item 2 heavy 20 lbs, the value is 100 CNY; the item 3 heavy 30 lbs, the value is 120 RMB. Therefore, since we can choose to install only one part, we can calculate the unit value of each item, item 1 is 6 yuan per pound, item 2 is meibang 5 yuan, item 3 is 4 RMB per pound. According to the greedy strategy, item 1 should be the current situation. If item 1 is packed and there is space in the backpack, then Item 2 ......

The final result is:

If you press 01, the result is:

If you are interested, you can check the result with the app of my 001 backpack.

The following is a small program for some backpacks:


# Include <iostream>
# Include <algorithm>
Using namespace std;
 
Typedef struct Thing {
Double v; // value
Double w; // weight
} Thing;
 
Thing arr [100];
Int n;
Double W;
 
 
Bool cmp (Thing a, Thing B)
{
Return a. v/a. w> B. v/B. w;
}
 
 
Int main ()
{
Cout <"Number of input items :";
Cin> n;
Cout <"input backpack carrying capacity :";
Cin> W;
Cout <"input" <n <"item value and weight:" <endl;
For (int I = 0; I <n; ++ I)
Cin> arr [I]. v> arr [I]. w;
Sort (arr, arr + n, cmp );
Int k = 0;
Double value = 0;
While (W)
{
If (W> = arr [k]. w)
{
W-= arr [k]. w;
Value + = arr [k]. v;
}
Else
{
Value + = W * arr [k]. v/arr [k]. w;
W = 0;
}
++ K;
}
Cout <"maximum value:" <value <endl;
Return 0;
}

Result

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.