Greedy of common algorithms

Source: Internet
Author: User

I. Background

In the preparation of the soft test, met the algorithm, listen to the experience, algorithm research good is very simple, the study is not very difficult, so think about the algorithm to do a summary, because the algorithm is not only in the big problem occupies 15 points, but also in the choice of problems will appear, especially the test complexity and the application of various algorithms,

Greed (short-sighted): Like looking for a boyfriend and girlfriend, not the best, only suitable (feasible solution)

Second, how to know in a certain situation with greed is appropriate?

       the premise of greedy strategy is that local optimal strategy can lead to global optimal solution. In fact, greedy algorithms are rarely used. In general, whether a problem analysis is applicable to greedy algorithm, you can choose the problem of a few real data analysis, you can make judgments.         third, the greedy algorithm implementation of the basic steps:              1. Candidate set (C)

A candidate set C is used as a possible solution to the problem. (The final solution is taken from candidate set C)

    For example, in the problem of finding change, the currencies of various denominations form a candidate set.

2. Solution Set (S) 

Each time a greedy choice is made, one is liberated into s, and finally a complete solution s is obtained

    3. Solution function (solution)

Check whether the solution set S constitutes a complete solution to the problem.

    For example, in the problem of finding change, the solution function is that the monetary amount paid is exactly equal to the payables.

        4. Selection function (select)

Greedy strategy, which is the key to the greedy method, the choice of the most promising solution to the problem of the object. (This selection function is usually related to the target function)

For example, in the problem of finding change, the greedy strategy is to select the currency with the most face value in the candidate set.

5. Feasible function (feasible)

It is possible to check whether a candidate object is added to the solution set. (the constraint is not met when the next object is added)

    For example, in the problem of finding change, the feasible function is that each step of the selected currency and the sum of the paid money does not exceed the payables.

Four, greedy algorithm implementation FrameworkStarting 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;
The following are explained in C:              
<span style= "FONT-SIZE:14PX;" >greedy (C)  //c is the input set of the problem, which is the candidate set    {        s={};//The initial solution set is the empty set while        (not solution (S))  //collection S does not constitute a solution to the problem        {           x=select (C);    Greedy selection in candidate set C if           feasible (S, x)  //Determine if the solution after adding X in set S is feasible              s=s+{x};              C=C-{X};        }        return S;    }  </span>

Greedy of common algorithms

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.