First knowledge of greedy algorithm (Graham algorithm)

Source: Internet
Author: User
Tags cmath

I have done a lot of greedy algorithms before, and they can all find the optimal solution, which is also the reason for using greedy algorithms. Compared with other algorithms, greedy algorithms have the greatest advantage in low time complexity and low space complexity. For questions about using greedy algorithms,

There are two important features: greedy strategy and optimal sub-structure. Greedy strategy is the basis for each step to adopt a strategy. The optimal sub-structure means that the problem can be transformed into the optimal solution for solving the sub-problem. This is a bit like dynamic planning, but the latter has to solve the problem of enumeration.

Resource consumption is high.

Greedy algorithms do not necessarily guarantee the optimal solution, but they are often ineffective in other methods (some do not have a solution, and some are unacceptable in complexity). In this case, we can try to use an approximate algorithm, based on a certain effective greedy policy,

Even if the optimal solution is not obtained, the trade-off is acceptable.

For example, if a number of items are specified, they must be divided into two heaps of similar quality as much as possible. For example, if the number of items is 5 and the weight is 3, 3, 2, 2, and 2 respectively, it is easy to divide them into 3 + 3 and 2 + 2 + 2 based on experience. However, this is a 2 ^ n level problem, with a large amount of data coming out.

Current combination explosion. There is no effective solution to this problem. The enumeration method can obtain the optimal solution, but the time complexity is O (2 ^ n), which is unacceptable. The following is the enumeration method at n <= 15, which simplifies the computation with bit operations.

# Include
 
  
# Include
  
   
Using namespace std; const int MAXN = 20; int w [MAXN]; int used [MAXN]; const int INF = 1 <30; int n, id, sum; int Solve () {int min, cnt = 1 <
   
    
N; memset (used, 0, sizeof (used); for (int I = 0; I
    
     
> W [I]; int ans = Solve (); for (int I = 0; I
     
      
Running result: 2 + 2 + 2 = 6 3 + 3 = 6
      


Graham proposed an approximate algorithm to solve the problem. That is, each time you select a maximum of w [I] items from the items that have not been split, and then add it to the two divided heaps (a1, b1) respectively, if | a1 + w [I]-b1 |> | a1-w [I]-b1 |, ze to b1; otherwise add

A1. The error between the final result and the optimal solution cannot exceed 16%. The following is the implementation of the Graham algorithm.

# Include
       
        
# Include
        
         
Using namespace std; const int MAXN = 20; int w [MAXN]; int used [MAXN]; int n, a, B; void Solve () {sort (w, w + n); a = 0, B = 0; for (int I = n-1; I> = 0; I --) {if (abs (a + w [I]-B) <= abs (a-w [I]-B) {a + = w [I]; used [I] = true;} else B + = w [I] ;}} int main () {cin> n; memset (used, 0, sizeof (used); for (int I = 0; I
         
          
> W [I]; Solve (); printf ("\ n first heap:"); for (int I = 0; I
          
           
Running result: 2 + 2 + 3 = 7 2 + 3 = 7
           

In some cases, the approximate algorithm is acceptable.

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.