Greedy and greedy common algorithms
I. background
In preparation for the soft test, I encountered an algorithm. After listening to it, it was very easy to study the algorithm. If the research was not good, I felt very difficult. So I thought about summing up the algorithm, because algorithms not only occupy 15 points in a major question, but also appear in multiple choice questions, especially the complexity and applicability of various algorithms,
Greedy (short-sighted): Just like looking for men and women,Not the best, just the right (feasible solution)
2. How can we know that it is appropriate to use greed in some situations?
The premise for greedy policies is that local optimal policies can
This results in a global optimal solution.In fact, greedy algorithms are rarely used. Generally, if a problem analysis is applicable to a greedy algorithm, you can select the actual data under the problem for analysis to make a judgment.
3. basic steps for implementing greedy algorithms:1. Candidate Set (C)
Use a candidate set C as the possible solution to the problem. (The final solution is taken from the candidate set C)
For example, when looking for change, currencies with different denominations constitute a candidate set.
2. solution set (S)
Every time a greedy choice is completed, one is liberated into S, and finally a complete solution is obtained.
3. solution)
Check whether the solution set S constitutes a complete solution to the problem.
For example, when looking for the change problem, the solution function is that the amount of money paid is exactly equal to the amount payable.
4. select Function)
That is, the greedy strategy, which is the key to the greedy method, selects the objects most likely to constitute a solution to the problem. (This selection function is usually related to the target function)
For example, when looking for change, the greedy strategy is to select the currency with the largest face value in the candidate set.
5. feasible Functions)
Check whether it is feasible to add a candidate object to the solution set. (Whether the constraint conditions are met after the next object is added)
For example, when looking for change, the feasible function is that the sum of the selected currency and the paid currency cannot exceed the payable.
IV. Implementation Framework of greedy AlgorithmsStarting from an initial solution of the problem; while (can take a step forward to the given overall goal) {use feasible decisions to find a solution element of the feasible solution ;}
Combine all solution elements into a feasible solution to the problem;
C is interpreted as follows:
<Span style = "font-size: 14px;"> Greedy (C) // C is the problematic input set, that is, the candidate set {S = {}; // The initial solution set is an empty set while (not solution (S) // The set S does not constitute a solution {x = select (C ); // make a greedy choice in the candidate set C if feasible (S, x) // determine whether the solution after x is added to the set S is feasible S = S + {x }; C = C-{x };}return S ;}</span>
What common algorithms are greedy algorithms?
Obviously, the KMP and FLOYD algorithms are not greedy algorithms. The FLOYD algorithms use the idea similar to dynamic planning, the KMP algorithm is to remove the string prefix to obtain all possible matching positions, thus reducing unnecessary displacement. There may be many greedy algorithms, but only these algorithms can be used in general. When determining whether a problem can be solved with greed, we should be able to prove the correctness of the greedy algorithm used here (see Introduction to algorithms)
One algorithm (Greedy Algorithm)
First, it doesn't matter where the content is dense and where it is not dense. This is a man-made distinction. You need to traverse all the grids first to determine, which is the slowest algorithm, the best route can be obtained after all traversal.
Since the greedy algorithm is used, we can assume that the board is infinite for the sake of convenience, and the goal of the algorithm is to determine whether to proceed to the right or to the next step. The idea is as follows:
Determine whether there are gold blocks in the right and bottom adjacent grids of the current grid, as shown in the following figure:
1) if one does not exist, go to the lattice with gold blocks.
2) If none or both of them exist, you need to determine the direction in which you can quickly pick up the next gold block. The method is as follows:
Let the robot take one step in two directions, and then judge the current grid as follows:
A) if one grid can continue to fetch gold blocks, and the other cannot, go to the grid in the previous step.
B) if there are still none or none, Repeat 2) until A) is found.
If the checker is N * N grids, the worst case of the greedy algorithm is to traverse the entire checker. For example, if the first checker has gold blocks, you need to traverse the entire Checker to determine the path. In the best case, you also need to traverse 4 * N grids.
The time complexity is calculated as O (nLogn)