Summary of Algorithms

Source: Internet
Author: User

A long time ago, I summarized some basic algorithms. (I am not going ......)

Each algorithm has only a few words, but it is the core step and idea.

 

NO.1 Greedy Algorithm
1. When solving the problem, always make the best choice in the current view. That is to say, without considering it as a whole, all it does is make a local optimal solution in a certain sense (whether it is global optimal, it must be proved ).

2 basic steps:
1. Start with an initial solution to the problem.
2. A circular statement is used to obtain a partial decomposition to narrow down the scope or scale of the problem when a step can be taken toward the target.
3. Combine all parts to obtain the final solution to the problem.

Example 1: Activity arrangement problems
Hd 2073 less AC this summer, 2037

Ideas:
The shortest sequence must include the program with the oldest End Time, And then finds the oldest end time from that end time.

Example 2: interval coverage
I is used to represent the range of the coordinates on the X axis as [I-1, I] (length is 1), and given M (1 = <M = <200) different integers, m. Now, let you draw several line segments to cover the segments of the residence. The condition is that each line segment can be any longer, but the sum of the lines to be drawn must be the minimum, and the number of line segments cannot exceed N (1 = <N = <50 ).
Start from N = 1, and gradually increase N. consider where to disconnect (that is, consider intermittent line segments)

 

Example 3: Harman coding (minimum heap)
Hdu 1053

 

Example 4: single-source shortest path (Dijkstra algorithm) 2066
The basic idea is to set the vertex set S and constantly make greedy choices to expand the set. A vertex belongs to the set S and is known only when the shortest path length from the source to the vertex is known.
For (I = 1; I <= n-1; I ++)
{
Id = choose (); // locate a vertex from the s set. The path sent from this vertex is the shortest path adjacent to the s set.
Used [id] = true; // include it in the s set
For (j = 1; j <= n; j ++)
If (dis [id] + mat [id] [j] <dis [j] & (! Used [j]) // updates the distance between the source point and each point.
{
Dis [j] = dis [id] + mat [id] [j]; // mat [] [] is the adjacent matrix, dis [I] is the distance from the specified point to the I point

}
}

 

Example 5: Minimum Spanning Tree 1863 1233
(1) Prim algorithm
(2) Kruscal algorithm (and query set)

 

Appendix: Comparison between greedy algorithms and dynamic planning.
Both greedy and dynamic planning conditions have the optimal sub-structure, but they are still different:
For example, a backpack contains n items and a backpack. The Weight of item I is Wi, its value is Vi, and the size of the backpack is C. How should we select the items that are loaded into the backpack to maximize the total value of the items in the backpack?
(1) You cannot attach item I to a backpack multiple times or just part of item I.
(2) You cannot attach item I to a backpack multiple times. You can only attach part of item I.

Answer: (2) greedy algorithms can be used, but (1) dynamic planning is required.

(2) Calculate the value Vi/Wi of each item unit weight, and then add the item according to greedy selection policy.

(1) The greedy choice cannot obtain the optimal solution because in this case, it cannot guarantee that the backpack can be fully filled. Some idle space reduces the value of each kilogram of space. In fact, when considering the 0-1 backpack problem, we should compare the Final Solution caused by selecting this item and not selecting this item, and then make the best choice. As a result, we can export many subproblems that overlap each other. This is another important feature that can be solved using a dynamic planning algorithm.

 

 

Method for Finding the power B of
Function (a, B)
Double function (a, B)
{
Int ret = 1;
While (B> 0)
{
If (B % 2 = 1) // The basic idea is to propose 2 from B to first perform the power of a. The power of model 2 and the power of model 2 plus 1 is added to the power of.
Ret * =;
B/= 2;
A * =;
}
Return ret;
}
Generally, the results are too large to express, and the question is usually the result % a certain number of m values. Make minor changes to the above program
Function (a, B)
Int function (a, B, m)
{
Int ret = 1;
While (B> 0)
{
If (B % 2 = 1)
Ret = ret * a % m;
B/= 2;
A = a * a % m;
}
Return ret;
}

 


#3 constructor and reload operator for writing struct in c/c ++

Struct AA
{
Public:
Int;
Int B;
Private:
Int;
Int B;
Protected:
Int GetA () const;
Void SetA ();
Public:
Int GetB () const;

AA & operator = (const AA & );
Public:
AA ();
AA (const AA & );
};

It can be seen that there is no difference between struct and class. The only difference is that, if public and private are not written, struct is a public member by default, and class is private by default.

 

No. 4 KMP algorithm failure function f () code (two strings a and B Return the subscript of B for the first time in)

Fail [h] is a string [h] flag.
Void f ()
{
Fail [0] =-1;
For (I = 1; I <l; I ++)
{
J = fail [I-1];
While (p [j + 1]! = P [I] & j> = 0)
{
J = fail [j];
}
If (p [j + 1] = p [I])
Fail [I] = j + 1;
Else
Fail [I] =-1;
}

}

No. 5 Dynamic Planning

1. 0-1 backpack Problems

Make m (I, j) the greatest value when there are I, I + 1, I + 2... n items to be placed, and the backpack space is j.

So: If j-wi> = 0, m (I, j) = max (m (I + 1, j), m (I + 1, j-wi) + vi );

If j-wi <0, m (I, j) = m (I + 1, j );

2. Questions about the oldest Sequence
Similar to the 01 backpack, p [I] [j] records the maximum number of subsequences In the first j of string a and string B, if a [I] = B [j], p [I] [j] = p [I-1] [J-1];
If not, p [I] [j] = max {p [I] [J-1], p [I-1] [j]}

 

3. The largest number of strings and the largest matrix.
Number string: B [I] is the largest and ending with a [I], B [I] = max {a [I], B [I-1] + a [I]}
(B [I] records are not required.) sum = B [I], I increments, and sum is constantly updated. The maximum sum is used as the answer.
Matrix: The key lies in the selection of child matrices. For the child matrices that have been set for a column, you can use the method used to calculate the maximum number of strings to find the rows to make and the maximum, then, select the smallest possible column combinations.

 

 

No. 6 getting started with Game Problems

Any game problem (ICG) can be converted to a directed graph problem. Each node in the figure represents a situation where each node has a SG function value. Generally, the SG function value of the last node (when the game ends) is introduced by the question, the function values of the remaining node x are sg (x) = mex {sg (y1), sg (y2), sg (y3 )......} Where y1, y2, y3 ...... For all successors of x, mex {A} indicates taking the smallest positive integer that is not included in set.
For example, a NIM game can be viewed as having n pieces on a directed graph. All the pawns will win at the final point. Algorithm: It can be converted into n images, each of which is a piece.
A sg function is the exclusive or of all the sg function values on the graph. (^ @ ^)

No. 7 recursive Problems
1, 2 ,......, N of n
Each element is arranged in a column. If the sequence number of each element is different from its number, it is called n.
An error line for different elements. The total number of error columns for n different elements is f (n ).
F (n) = n! [1-1/1! + 1/2! -1/3! + ...... + (-1) ^ n * 1/n!]

This formula is introduced using a recursive method below:

An error line for n different elements can be completed in the following two steps:

Step 1: "error" element 1 (place element 1 in one of the positions from 2nd to n) with n-1
Method.

Step 2: "error" the remaining n-1 elements are arranged in the following order. Depending on the result of step 1, if 1
Element number falls in the k position. In the second step, the k element is first "incorrectly arranged ".
Different sorting methods of Number elements will lead to two different situations: (1) k elements ranked 1st
N-2 elements left in the "error" column on the same position set as their number set, with f (n-2)
(2) The k element does not have 1st positions. In this case, the 1st positions can be regarded as the k
(Including the k element), the "error" of n-1 elements is formed, with f (n-1)
Method. According to the addition principle, f (n-2) + f (n-1) methods are used to complete the second step.

According to the multiplication principle, the number of wrong sorting of n different elements

F (n) = (n-1) [f (n-2) + f (n-1)] (n> 2 ).

 

 

 

 

 

 

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.