[Post] What is an algorithm?

Source: Internet
Author: User

Http://www.ibeifeng.com/read.php? Tid = 3671 & u= 14387

What is an algorithm?

1. What is an algorithm?
Algorithms are a series of clear instructions for solving problems, that is, can
Enough for a certain standard input, get the required output within a limited period of time. Algorithms often contain repeated steps and comparison or logical judgment. If an algorithm has a defect or is not suitable for a problem, execute this calculation.
Method will not solve this problem. Different algorithms may use different time, space, or efficiency to accomplish the same task. The advantages and disadvantages of an algorithm can be measured by space complexity and time complexity.
Algorithm time
Inter-complexity refers to the time resources consumed by algorithms. In general, computer algorithms are the function f (n) of the problem scale N, the growth rate of Algorithm Execution time and F (N)
Is called Asymptotic Time
Complexity ). Time complexity is expressed by "O (order of magnitude)", which is called "order ". Common time complexity:
O (1) constant order; O (log2n) Logarithm order; O (n) linear order; O (n2) Square order.
The space complexity of an algorithm refers to the space resources that the algorithm consumes. The computation and Representation Methods are similar to the time complexity. They are generally expressed by the approximation of the complexity. Compared with time complexity, the analysis of space complexity is much simpler.

Ii. Algorithm Design Method
1. Progressive Method
Delivery
The push method is a method that uses the recursive relationship of the question itself to solve the question. If n = 1, the solution is known or can be easily obtained. Can be constructed using the Progressive Method
The problem of the algorithm has an important recursive nature, that is, when the problem scale is the solution of the I-1, from the recursive nature of the problem, can be obtained from the scale of 1, 2 ,..., A series of solutions of I-1, the construction of the problem scale for I
. In this way, the program can start from I = 0 or I = 1, repeatedly, from known to the I-1 scale of the solution, through recursion, to obtain the scale of the I solution, until the solution with a scale of N is obtained.
[Problem] factorial calculation
Problem description: write a program to calculate and output the factorial K of K for the given n (n ≦ 100! (K = 1, 2 ,..., N.
Because the required integer may be much larger than the digits of a general integer, the program uses a one-dimensional array to store long integers. Each element of a long integer array stores only one digit of a long integer. Store M-bit integer N in array:
N = A [m] × 10m-1 + a [M-1] × 10m-2 +... + A [2] × 101 + A [1] × 100
Use a [0] to store the M digits of the long integer N, that is, a [0] = m. As agreed above, each element of the array stores the factorial K of K! The second and third elements of the array from the low to the high ....... Example: 5! = 120, the storage format in the array is:
3 0 2 1 ......
The first element 3 indicates that the long integer is a three-digit number, followed by 0, 2, and 1 from the low position to the high position, and expressed as an integer 120.
Calculate the factorial K! Can use the obtained factorial (k-1 )! After the continuous accumulation of K-1 obtained. For example, known 4! = 24, calculate 5 !, You can add four times to the original 24 and then get 120. For details, see the following procedure.
# Include <stdio. h>
# Include <malloc. h>
......
2. Recursion
Recursion is a powerful tool for designing and describing algorithms. Because it is often used in the description of complex algorithms, we will discuss it before introducing other algorithm design methods.
Yes
Recursive Algorithms generally have the following features: To solve the n-scale problem, we try to break it down into smaller-scale problems, then, we can easily construct the solutions for big problems from the solutions of these small problems, and these scales
Small Problems can also be resolved into smaller problems using the same decomposition and synthesis method, and the solutions to large-scale problems are created from the deconstruct of these smaller problems. In particular, when the scale N is 1, it can be directly solved.

[Problem] compile the nth function fib (n) for calculating the Fibonacci series ).
Fibonacci series: 0, 1, 1, 2, 3 ,......, That is:
FIB (0) = 0;
FIB (1) = 1;
FIB (n) = fib (n-1) + fib (n-2) (when n> 1 ).
Recursive functions:
Int fib (int n)
{If (n = 0) return 0;
If (n = 1) return 1;
If (n> 1) return fib (n-1) + fib (n-2 );
}
Delivery
The execution process of the regression algorithm is divided into two stages: recurrence and regression. In the recurrence stage, the solution of more complex problems (scale N) is pushed to the solution of problems that are simpler than the original problem (scale less than N. For example, in the above example
To solve fib (N), push it to solve fib (n-1) and FIB (n-2 ). For calculation of Fib (N), FIB (n-1) and FIB (n-2) must be calculated first
Calculation of Fib (n-1) and FIB (n-2), must first calculate fib (n-3) and FIB (n-4 ). And so on until the calculation of Fib (1) and FIB (0) can be obtained immediately.
To result 1 and 0. In the recurrence stage, termination of recursion is required. For example, in function fib, when n is 1 and 0.
In the regression phase, after obtaining the solution of the simplest case, return the result step by step and obtain the solution of a slightly complex problem, for example, after obtaining fib (1) and FIB (0, the result of Fib (2) is returned ,......, After obtaining the results of Fib (n-1) and FIB (n-2), the results of Fib (n) are returned.
When writing recursive functions, note that the knowledge of local variables and parameters in the functions is limited to the current call layer. When recursion enters the simple problem layer, parameters and local variables at the original level are hidden. In a series of "simple question" layers, they have their own parameters and local variables.
By
Recursive Algorithms cause a series of function calls and may have a series of repeated computations. The execution efficiency of recursive algorithms is relatively low. When a recursive algorithm can be easily converted into a recursive algorithm
Write a program. For example, for the above example, the function fib (n) of the nth item of the Fibonacci series should adopt a recursive algorithm, that is, the next item is calculated from the first two items of the Fibonacci series, until the requirements are calculated
.
[Problem] combination Problem
Problem description: Find the natural numbers 1, 2 ,...... And N. For example, all the combinations of N = 5 and r = 3 are: (1) 5, 4, 3 (2) 5, 4, 2 (3) 5, 4, 1
(4) 5, 3, 2 (5) 5, 3, 1 (6) 5, 2, 1
(7) 4, 3, 2 (8) 4, 3, 1 (9) 4, 2, 1
(10) 3, 2, 1
Minute
The 10 combinations listed in the analysis can be used to consider the algorithm of the combination function. Set the function to void comb (int m, int
K) to find the natural numbers 1, 2 ,...... And any combination of K numbers in M. When the first number of a combination is selected, the following number is a combination of the number of K-1 from the remaining number of m-1. This will calculate m
The combination problem of K number in the number is transformed into the problem of the combination problem of the number of m-1 and the number of K-1. Set function to introduce work array [
] Stores the numbers of the obtained combinations. The Convention function puts the first number of the K-digit combination in a [K]. After a combination is obtained, [
. The first number can be m m-1 ,...... , K, the function puts the first number of the combination into the array, there are two possible options, because the remaining elements of the combination have not been removed, continue to pass
Return to confirm; or because all elements of the combination have been determined, output the combination. For details, see the function comb in the following program.
[Program]
# Include <stdio. h>
# Define maxn100
Int A [maxn];
Void comb (int m, int K)
{Int I, J;
For (I = m; I> = K; I --)
{A [k] = I;
If (k> 1)
Comb (I-1, k-1 );
Else
{For (j = A [0]; j> 0; j --)
Printf ("% 4D", a [J]);
Printf ("/N ");
}
}
}

Void main ()
{A [0] = 3;
Comb (5, 3 );
}
3. Backtracking
Back
This method is also called the test method. It first gives up the limit on the size of the problem temporarily, and enumerates and tests the problem in a certain order. Select the next
Explain; if the current explain solution does not meet the problem scale requirements and meets all other requirements, continue to expand the current explain scale and continue testing. If the current failover solution meets the requirements
When all requirements are met, this solution is a solution to the problem. In the Backtracking Method, the process of dropping the current half solution is called backtracking. Expanding the scale of the current solution to continue testing is called forward
Test.

[Problem] combination Problem
Problem description: Find the natural number 1, 2 ,..., N contains all the combinations of r numbers.
Use the Backtracking Method to locate the problem and save the found combination in ascending order of a [0], a [1],…, In a [r-1], the elements of the combination meet the following properties:
(1) A [I + 1]> A. the last digit is greater than the previous digit;
(2) A-I <= N-R + 1.
The process of searching for solutions can be described as follows:
First
First, discard the condition that the number of combinations is R. The candidate combination starts with only one number 1. Because the solution satisfies all the conditions except the problem scale, expands its scale and satisfies the above conditions (1), candidate combinations
Change to 1, 2. Continue this process and obtain the candidate combinations 1, 2, and 3. This solution satisfies all the conditions, including the problem scale, and thus is a solution. On the basis of this solution, select the next partial solution, because
3 In A [2] is adjusted to 4, and 5 in the future all the requirements of the problem are met. solution 1, 2, 4, 1, 2, 5 is obtained. Since 5 cannot be adjusted any more, we need to go back from a [2] to a [1],
At this time, a [1] = 2, can be adjusted to 3, and test forward, get solution 1, 3, 4. Repeat the previous test and Backward Tracing until a [0] is used for backtracking, which indicates that all solutions to the problem have been found.
Write the program as follows:
[Program]
# Define maxn100
Int A [maxn];
Void comb (int m, int R)
{Int I, J;
I = 0;
A = 1;
Do {
If (a-I <= m-R + 1
{If (I = R-1)
{For (j = 0; j <r; j ++)
Printf ("% 4D", a [J]);
Printf ("/N ");
}
A ++;
Continue;
}
Else
{If (I = 0)
Return;
A [-- I] ++;
}
} While (1)
}

Main ()
{Comb (5, 3 );
}

4. Greedy method
The greedy method is a method that does not pursue the optimal solution and only wants to obtain a satisfactory solution. The greedy method can quickly get a satisfactory solution, because it saves a lot of time to find the optimal solution to exhaust all possibilities. The greedy method is usually based on the current situation for optimal selection, regardless of the overall situation of various possibilities, so the greedy method should not backtrack.
Example
For example, when looking for money during regular shopping, in order to minimize the number of coins to be recovered, we do not consider all kinds of publishing schemes for finding the change, but start from the currency with the largest face value, consider each currency in a descending order, and use the largest nominal value first
Currency, when the amount of the currency is less than the large nominal value, consider the next currency with a smaller nominal value. This is the use of the greedy method. This method is always optimal here because the Bank issues a coin category and the coin nominal value
Clever arrangement. For example, only coins with a nominal value of 1, 5, and 11 are expected to be recovered. According to the greedy algorithm, we should find one coin with 11 nominal values and four coins with one nominal value,
5 coins are retrieved. However, the optimal solution should be 3 Coins with 5 nominal values.
[Problem] Packing Problem
Problem description: The packing problem is described as follows: 0, 1 ,... ,
N-1-1 items, volume: v0, V1 ,... Vn-1. Pack the N items into several boxes with a capacity of v. It is agreed that the volume of these N items shall not exceed V, that is, for 0 ≤ I <n,
There are 0 <VI ≤ v. The number of boxes required for different packing schemes may be different. The packing problem requires that the number of boxes containing the N items be less.
If n items are divided into N or
If the number is smaller than all the subsets of N items, the optimal solution can be found. However, the total number of all possible partitions is too large. For an appropriately large N, it is unacceptable to find out all possible classifications. For this reason, the packing problem is
Use a very simple approximation algorithm, that is, the greedy method. This algorithm puts items in the first box that can be placed in sequence. Although this algorithm cannot find the optimal solution, it can still find a very good solution. Without losing its universality
The volume of N items is sorted in ascending order, that is, v0 ≥ V1 ≥... ≥ Vn-1. If the preceding requirements are not met, you only need to first sort the N items by their volume from large to small, and then sort
Rename an item. The packing algorithm is described as follows:
{Input box volume;
Number of input items N;
Input the volume of each item in the order of size from large to small;
The pre-used box chain is empty;
The pre-configured box counter box_count is 0;
For (I = 0; I <n; I ++)
{Search for the box J that can be placed in item I in sequence from the first box in use;
If (no more items can be placed in used boxes I)
{Use another box and put item I into it;
Box_count ++;
}
Else
Put item I into the box J;
}
}
Upper
The algorithm can calculate the number of boxes box_count and find the items in each box. The following example shows that the algorithm may not be able to find the optimal solution. There are 6 items with their sizes
Unit: 60, 45, 35, 20, 20, and 20. The volume of the box is 100 units. Based on the above algorithm, three boxes are required. The items in each box are: the first box is used
1, 3; 2, 4, and 5; 3, 6. The optimal solution is two boxes with items 1, 4, 5, 2, 3, and 6 respectively.
If the items in each box are represented by a linked list, the first node pointer of the linked list is stored in a structure, and the remaining space is recorded in the structure and the first pointer of the linked list of the items in the box. In addition, the information of all boxes is also a linked list. The following is a program written based on the above algorithms.
}

5. Divide and conquer Law
Ren
The computing time required for a problem that can be solved by a computer is related to its scale N. The smaller the problem, the easier it is to solve it directly, and the less computing time it takes to solve the problem. For example, for sorting n elements
When n = 1, no computation is required; when n = 2, the order can be sorted as long as a comparison; when n = 3, only three comparisons can be performed ,.... When N is large, the problem is not easy to handle. To directly
Solving a large-scale problem is sometimes quite difficult.
The principle of divide and conquer law is to divide a big problem that is hard to solve directly into the same problems of small scale, so that they can be cracked and managed separately.
For example
The original problem can be divided into k sub-problems (1 <k ≤ n), and all these sub-problems can be solved. You can use the solutions of these sub-problems to find the solutions of the original problem, this method is feasible. Produced by the Division and Control Law
Sub-problems are often small models of the original problem, which provides convenience for the use of recursive technology. In this case, the sub-problem type can be consistent with the original problem type through repeated application of sub-governance means, but its scale is constantly reduced, the most
In the end, it is easy to directly solve the subproblem. This naturally leads to the generation of recursive processes. Grouping and Recursion are like twins. They are often applied to algorithm design at the same time, and many efficient algorithms are generated.
The problems solved by the Division and control law generally have the following characteristics:
(1) The problem can be easily solved by narrowing down to a certain extent;
(2) The problem can be divided into several small-scale identical problems, that is, the problem has the optimal substructure;
(3) The solutions of subproblems decomposed by the problem can be merged into the solutions of the problem;
(4) Each subproblem identified by the problem is independent of each other, that is, the subproblem does not include a public subproblem.
Upper
The first feature is that the vast majority of problems can be met, because the computing complexity of the problem generally increases with the increase of the scale of the problem. The second feature is the premise of applying the divide and conquer method, it is also possible for most problems
Satisfied, this feature reflects the application of recursive thinking. The third feature is the key. Whether the divide and conquer method can be used depends on whether the problem has the third feature. If the first and second features are met, not Third
The greedy method or dynamic programming method can be considered. Article 4 features involve the efficiency of the Division and control law. If the sub-problems are not independent, the division and Control Law should do a lot of unnecessary work to solve public sub-problems repeatedly,
At this time, although the divide and conquer method is available, it is better to use dynamic programming method.
There are three steps in recursion of each layer:
(1) decomposition: the original problem is divided into several subproblems, which are small in size and independent from each other and form the same as the original problem;
(2) solution: If the subproblem is small and easy to solve, the subproblem will be solved directly; otherwise, the subproblem will be solved recursively;
(3) Merge: Merge the solutions of each subproblem into the solutions of the original problem.
6. Dynamic Programming
Complex problems often occur. Instead of simply breaking down them into several subproblems, they may break down a series of subproblems. Simply resolve a large problem into a sub-problem, and combine the sub-problem solution to export the solution of the big problem. the time consumed for solving the problem increases in a power series according to the scale of the problem.
To reduce the time required to repeatedly find the same subproblem, an array is introduced, no matter whether they are useful for the final solution or not, to resolve all subproblems in the array, this is the basic method used by dynamic programming. The following describes how to use the dynamic planning method with examples.
[Problem] calculates the longest common character subsequence of a two-character sequence.
Question
Description: A subsequence of a character sequence is a sequence of characters formed after several characters (either one or not) are removed randomly (not necessarily consecutively) from a given character sequence. Makes a given Character Sequence
X = "x0, X1 ,..., Xm-1 ", sequence y =" y0, Y1 ,..., Yk-1 is a subsequence of X, there is a strictly incrementing subscript sequence of x <I0, i1 ,..., Ik-
1>, so that all J = ,..., K-1 with Xij = YJ. For example, x = "abcbdab" and Y = "bcdb" are subsequences of X.
Consider how to break down the longest common subsequence into sub-problems, set a = "A0, A1 ,..., Am-1 ", B =" B0, B1 ,..., Bm-1 ", and z =" z0, Z1 ,..., Zk-1 "is their longest common subsequence. It is not hard to prove that it has the following features:
(1) If am-1 = bn-1, then zk-1 = Am-1 = bn-1, and "z0, Z1 ,..., Zk-2 "is" A0, A1 ,..., Am-2 "and" B0, B1 ,..., A Longest Common subsequence of bn-2;
(2) If am-1! = Bn-1, if zk-1! = Am-1, containing "z0, Z1 ,..., Zk-1 "is" A0, A1 ,..., Am-2 "and" B0, B1 ,..., A Longest Common subsequence of bn-1;
(3) If am-1! = Bn-1, if zk-1! = Bn-1, contains "z0, Z1 ,..., Zk-1 "is" A0, A1 ,..., Am-1 "and" B0, B1 ,..., A Longest Common subsequence of bn-2.
This
Sample, in the search for a and B Public sub-sequence, if there is am-1 = bn-1, then further solve a sub-problem, find "A0, A1 ,..., Am-2 "and" B0, B1 ,..., A bm-2
Longest public subsequence; If am-1! = Bn-1, it is to solve two sub-problems, find out "A0, A1 ,..., Am-2 "and" B0, B1 ,..., A Longest Common subsequence of bn-1
And find "A0, A1 ,..., Am-1 "and" B0, B1 ,..., The longest common subsequence of bn-2, and the elders of the two are used as the longest common subsequence of A and B.
The Code is as follows:
# Include <stdio. h>
# Include <string. h>
# Define n 100
Char A [n], B [N], STR [N];

Int lcs_len (char * a, char * B, int C [] [N])
{Int M = strlen (A), n = strlen (B), I, J;
For (I = 0; I <= m; I ++) C [0] = 0;
For (I = 0; I <= N; I ++) C [0] = 0;
For (I = 1; I <= m; I ++)
For (j = 1; j <= m; j ++)
If (A [I-1] = B [J-1])
C [J] = C [I-1] [J-1] + 1;
Else if (C [I-1] [J]> = C [J-1])
C [J] = C [I-1] [J];
Else
C [J] = C [J-1];
Return C [m] [N];
}

Char * buile_lcs (char s [], char * a, char * B)
{Int K, I = strlen (A), j = strlen (B );
K = lcs_len (A, B, C );
S [k] = '';
While (k> 0)
If (C [J] = C [I-1] [J]) I --;
Else if (C [J] = C [J-1]) j --;
Else {s [-- K] = A [I-1];
I --; j --;
}
Return S;
}

Void main ()
{Printf ("enter two string (<% d )! /N ", N );
Scanf ("% S % s", a, B );
Printf ("LCS = % s/n", build_lcs (STR, a, B ));
}
7. Iterative Method
Iteration Method is a common algorithm design method used to obtain the approximate root of equations or equations. Set the equation to f (x) = 0, use a mathematical method to export the equivalent form X = g (x), and then follow these steps:
(1) Select the approximate root of an equation and assign it to variable x0;
(2) Save the value of x0 to the variable X1, calculate g (X1), and save the result to the variable x0;
(3) when the absolute value of the difference between x0 and X1 is smaller than the specified precision requirement, repeat the calculation in step (2.
If the equation has roots and the approximate root sequence obtained by the above method converges, The x0 obtained by the above method is considered as the root of the equation. The above algorithm is represented in the form of a C program:
The procedure is as follows:
[Algorithm] iterative method is used to obtain the root of the equations.
{For (I = 0; I <n; I ++)
X = initial approximate root;
Do {
For (I = 0; I <n; I ++)
Y = X;
For (I = 0; I <n; I ++)
X = GI (X );
For (delta = 0.0, I = 0; I <n; I ++)
If (FABS (Y-x)> delta) Delta = FABS (Y-x);} while (delta> epsilon );
For (I = 0; I <n; I ++)
Printf ("the approximate root of variable X [% d] is % F", I, X );
Printf ("/N ");
} Pay attention to the following two possible situations when using iterative methods to root out:
(1) If the equation has no solution, the approximate root sequence obtained by the algorithm will not converge, and the iteration process will become an endless loop. Therefore, before using the iterative algorithm, we should first check whether the equation has a solution, limit the number of iterations in the program;
(2) Although the equation has solutions, improper selection of iteration formulas or unreasonable selection of the initial approximate root of iteration may also lead to iteration failure.
8. exhaustive search
The exhaustive search method is used to enumerate and test the number of partial solutions that may be the solutions one by one in a certain order, and identify the compliant partial solutions from the crowd as the solution to the problem.
[Problem] The six variables A, B, C, D, E, and F are arranged in a triangle. These six variables are integers on [1, 6], respectively, and are not the same. Evaluate all solutions that make the sum of the variables on the three sides of a triangle equal. Is a solution.
Cheng
Introduce variables A, B, C, D, E, and F in sequence, and let them take integers ranging from 1 to 6, test whether the sum of the variables on the three sides of the triangle arranged by them is
Equal. If they are equal, they are arranged to meet the requirements and are output. After all these variables are combined, the program can obtain all possible solutions. The procedure is as follows:
Programs compiled by the exhaustive method are usually unable to adapt to changes. If the problem is changed to that where nine variables are arranged in a triangle and each side has four variables, the program's cyclic weight will change accordingly.

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.