java-Greedy algorithm

Source: Internet
Author: User

1. What is a greedy algorithm?
greedy algorithm, also known as greedy algorithm (greedy algorithm), refers to when solving problems, always make the best choice in the present. In other words, not considering the overall optimal solution, it makes only the local optimal solution in a certain sense.
The greedy algorithm is a phased work, at each stage, it can be thought that the decision is the best, regardless of future consequences. This "Get it right now" strategy is the source of the name of this type of algorithm.
greedy algorithm has no fixed algorithm framework, the key of algorithm design is the choice of greedy strategy. It must be noted that the greedy algorithm does not have the overall optimal solution to all problems, the greedy strategy chosen must have no effect, that is, the process after a state does not affect the previous state, only related to the current state. Therefore, the greedy strategy used must carefully analyze whether it satisfies the no-effect.

second, the basic idea of greedy algorithm:
1. Build a mathematical model to describe the problem.
2. Divide the problem of solving into several sub-problems.
3. To solve each sub-problem, the local optimal solution of sub-problem is obtained.
4. The solution of the problem is solved by the local optimal solution to the original solution problem.

three, the greedy algorithm applies the question
the premise of greedy strategy is that local optimal strategy can lead to global optimal solution. That is, when the algorithm terminates, the local optimal equals the global optimal.

Four, greedy algorithm implementation Framework
Starting from an initial solution of a problem;
While (one step forward for a given total target)
{
a solution element of the feasible solution is obtained by using the feasible decision;
}
a feasible solution to the problem of the combination of all solution elements;

Five, the choice of greedy strategy
because the greedy algorithm can only solve the global optimal solution by solving the local optimal solution, we must pay attention to whether the problem is suitable for the greedy algorithm and whether the solution found is the best solution.
if the greedy algorithm can be used, it must choose the appropriate greedy strategy;

six . Some examples of greedy algorithms

1. Change of Notes

Suppose 1 yuan, 2 yuan, 5 yuan, 10 yuan, 20 yuan, 50 yuan, 100 yuan banknotes, the number is unlimited, now to pay K yuan, at least how many banknotes?

Obviously, we can easily think of using greedy algorithms to solve, and we are based on the greedy strategy is, every step as far as possible with a large amount of paper money. Of course this is correct, the code is as follows:

/*** Coin Change question * *@param Money The money*/     Public Static voidGreedygivemoney (intMoney ) {System.out.println ("Need change:" +Money ); int[] Moneylevel = {1, 5, 10, 20, 50, 100};  for(inti = moneylevel.length-1; I >= 0; i--) {            intnum = money/Moneylevel[i]; intMoD = money%Moneylevel[i]; Money=MoD; if(num > 0) {System.out.println ("Requires" + num + "Zhang" + moneylevel[i] + "block"); }        }    }

(1) If you do not limit the amount of paper money, then this situation is also suitable for greedy algorithm. For example, 1 yuan, 2 yuan, 3 yuan, 4 yuan, 8 yuan, 15 yuan of banknotes, to pay K yuan, at least how many banknotes?

By our analysis, this situation is not suitable for greedy algorithm, because the greedy strategy we provide above is not the optimal solution. For example, banknotes 1 yuan, 5 yuan, 6 yuan, to pay 10 yuan, according to the above algorithm, at least 1 Zhang 6 yuan, 4 1 yuan, and actually the best should be 2 5 yuan.

(2) If you limit the number of banknotes, then this situation is also suitable for greedy algorithm. such as 1 Yuan 10 Zhang, 2 Yuan 20 Zhang, 5 Yuan 1 Zhang, used to pay K yuan, at least how many banknotes?

Also, think about it and know that this is not a suitable greedy algorithm. For example, 1 Yuan 10 Zhang, 20 Yuan 5 Zhang, 50 Yuan 1 Zhang, that is used to pay 60 yuan, according to the above algorithm, at least 1 Zhang 50 yuan, 10 Zhang 1 yuan, and actually use 3 Zhang 20 yuan can be;

(3) So greedy algorithm is a kind of local optimal algorithm in a certain range.

java-Greedy algorithm

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.