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

Source: Internet
Author: User

It is recommended that first look at the preface: http://www.wutianqi.com /? P = 2298

Total Directory: http://www.wutianqi.com /? P = 2403

GreedyAlgorithmTo 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? Take a closer look at 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 take my 001 backpack.ProgramVerify the result.

The following is a small program for some backpacks:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
 # 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 <  "Value and weight of items :"   < 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   <   "The greatest value is :"   < Value < Endl ;  Return   0  ; } 

Result

Tanky Woo Tag:   Tanky Woo, greedy algorithm, Introduction to Algorithms

In my independent blog: http://www.wutianqi.com /? P = 2571

You are welcome to discuss this.

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.