Algorithm refers to an accurate and complete description of the solution scheme, which is a series of clear instructions for solving problems. Algorithm represents a systematic descriptionProblem solving strategiesMechanism.In other words, the required output can be obtained within a limited time for a certain standard input.If an algorithm has defects or is not suitable for a problem, executing the algorithm will not solve the problem.Different algorithms may use different time, space or efficiency to complete the same task.The advantages and disadvantages of an algorithm can be usedSpatial complexityAndTime complexityTo measure.
The instructions in the algorithm describe a calculation. When it runs, it can start from an initial state and (possibly empty) initial input, and go through a series ofLimitedThe clearly defined state will eventually produceoutputandstop itOn aFinal state。The transition from one state to another is not necessarily certain.Randomization algorithmSome algorithms include some random inputs.
An algorithm should have the following five important characteristics:
Finiteness
(Finiteness)
The finiteness of the algorithm means that the algorithm must be able to terminate after executing a limited number of steps;
Accuracy
(Definiteness)
Each step of the algorithm must have an exact definition;
Entry
(Input)
An algorithm has 0 or more inputs to describe the initial situation of the operation object. The so-called 0 inputs means that the algorithm itself has determinedinitial condition ;
Output Item
(Output)
An algorithm has one or more outputs to reflect the inputData processingResults after.The algorithm without output is meaningless;
feasibility
(Effectiveness)
Any calculation step performed in the algorithm can be decomposed into basic executable operation steps, that is, each calculation step can be completed in a limited time (also known asEffectiveness)。
essential factor
Announce
edit
Idata objectOperations and operations: The basic operations that a computer can perform are described in the form of instructions.Onecomputer systemThe set of all instructions that can be executed is called theCommand system。One computerBasic operationThere are four types of and operations:[1]
2、 Algorithmiccontrol structure: The functional structure of an algorithm depends not only on the selected operation, but also on the execution order of each operation.[1]
evaluate
Announce
edit
The same problem can be solved by different algorithms, and the quality of an algorithm will affect the efficiency of the algorithm and even the program.algorithm analysisThe purpose of the algorithm is to select the appropriate algorithm and improve the algorithm.The evaluation of an algorithm is mainly based onTime complexityandSpatial complexityTo consider.
Time complexity
AlgorithmicTime complexityIt refers to the calculation workload required to execute the algorithm.In general,Computer algorithmIt's the scale of the problemFunction ofThe time complexity of the algorithm is therefore recorded as:
The spatial complexity of the algorithm isFingering algorithmConsumableMemory space。Its calculation and expression method is similar to the time complexity, and generally usesComplexityThe asymptotic behavior of the.Compared with time complexity, the analysis of space complexity is much simpler.
Correctness
The correctness of an algorithm is the most important criterion for evaluating an algorithm.
Readability
The readability of an algorithm refers to the ease with which an algorithm can be read by people.[1]
RecursiveIt is a common algorithm in sequence computers.It calculates each item in the sequence according to certain rules, and usually obtains the value of the specified item in the sequence through some items in front of the computer.Its idea is to transform a complex and huge computing process into multiple repetitions of a simple process. The algorithm takes advantage of the fast and tireless machine characteristics of the computer.
Recursive method
The programming skill of program calling itself is calledrecursion(recursion)。A procedure or function has a method of calling itself directly or indirectly in its definition or description, which usually converts a large and complex problem into aOriginal questionSimilar small scale problems can be solved. The recursive strategy can describe the multiple repeated calculations required in the problem solving process with only a few programs, which greatly reduces the code amount of the program.The ability of recursion is to define the object'sInfinite set。In general, recursion requiresboundary condition , recursive forward segment and recursive return segment.When the boundary conditions are not satisfied, recursion advances;When the boundary conditions are met, it returns recursively.
be careful:
(1) Recursion is to call itself in a procedure or function;
(2) When using the recursion strategy, there must be a clear recursion end condition, called recursion exit.
Exhaustion method
Exhaustion method, or called violence cracking method, its basic idea is: for the problem to be solved, list all the possible situations, judge one by one which meet the requirements of the problem, and then get the solution of the problem.It is also often used to decipher the password, that is, the password is calculated one by one until the real password is found.For example, a password that is known to be four digits and consists of all numbers may have 10000 combinations, so the correct password can be found after 10000 attempts at most.Theoretically, this method can be used to crack any kind of password, but the problem is how to shorten the trial and error time.Therefore, some people use computers to increase efficiency, while others use dictionaries to narrow the range of password combinations.
useGreedy Design algorithmIt is characterized by step by step, often based on the current situation to make the optimal selection according to a certain optimization measure, without considering various possible overall situations, which eliminates the need to findOptimal solutionTo spend as much time as possible, it uses a top-down iterative method to make successive greedy choices. Each greedy choice simplifies the problem to a smaller sub problem. Through each greedy choice, an optimal solution to the problem can be obtained,However, the resulting global solution is not always optimal, soGreedy methodDon't backtrack.
greedy algorithm Is an improved classificationprocessing methodIts core is to select a measurement standard according to the meaning of the question, and then arrange these multiple inputs into the order required by this measurement standard. Input one quantity at a time in this order. If this input and some of the best solutions currently constituted in this measurement sense are added together, they cannot produce onefeasible solution, this input is not added to this part of the solution.This hierarchical processing method, which can obtain the optimal solution in a certain metric sense, is called greedy algorithm.
For a given problem, there may be several measurement standards.At first glance, these metrics seem to be desirable, but in fact, the optimal solution in the sense of this metric obtained by greedy processing with most of them is not the optimal solution of the problem, but the suboptimal solution.Therefore, the core of greedy algorithm is to select the optimal metric that can produce the optimal solution of the problem.
Generally, it is not easy to select the optimal measurement standard, but it is particularly effective to solve a problem with greedy algorithm after the optimal measurement standard can be selected.[2]
Divide and conquer
Divide and conquerIt is to divide a complex problem into two or more identical or similar subproblems, and then divide the subproblem into smaller subproblems... until the final subproblem can be simply solved directly, the solution of the original problemNamely sonThe combination of the solutions of the problem.[2]
The problems that divide and conquer can solve generally have the following characteristics:
(1) If the scale of the problem is reduced to a certain extent, it can be easily solved;
(2) The problem can be decomposed into several smaller identical problems, that is, the problem has the optimal substructure property;
(3) Use thisProblem decompositionThe solution of the sub problem can be combined into the solution of the problem;
(4) The sub problems decomposed from this problem areMutual independenceThat is, there are no common sub problems between sub problems.
Dynamic programming
dynamic programmingIt is a kind ofcomputer scienceUsed in to solve problems with overlapping subproblemsoptimization problem Method.Its basic idea is to decompose the original problem into similar subproblems, and to find the solution of the original problem through the solution of the subproblems in the process of solving.The idea of dynamic programming is the basis of many algorithms and is widely used in computer science and engineering.
dynamic programmingProgramming is a way to solve optimization problems, not a special algorithm.Unlike the search ornumerical calculationSo, there is a standard mathematicsexpressionAnd clear solutions.Dynamic programming is often designed for aoptimization problem Because the nature of various problems is different, the conditions for determining the optimal solution are also different from each other, so the design method of dynamic programming has its own unique solution method for different problems, but there is no universal dynamic programming algorithm that can solve all kinds of optimization problems.Therefore, in addition to correctly understanding the basic concepts and methods, readers mustSpecific analysis of specific problemsProcessing, building models with rich imagination, and solving problems with creative skills.[2]
Iterative method
Iterative methodAlso known as rolling method, it is a method of continuously using the old value of variablesRecursiveThe process of new value corresponds to the iterative methoddirect method(or called one-time solution), that is, solve the problem at one time.Iterative method can be divided into exact iteration and approximate iteration.“dichotomy”And“Newton iterative method”It belongs to approximate iteration method.iterative algorithm It is a basic method of solving problems with computers.It uses computers with fast computing speed and is suitable forRepeatabilityThe operation feature allows the computer to repeatedly execute a group of instructions (or certain steps). Each time the group of instructions (or these steps) is executed, a new value of the variable is derived from its original value.[2]
Branch and bound method
Branch and bound methodIt is a widely used algorithm, which is highly skilled, and different types of problem solutions are also different.
Branch and bound methodThe basic idea ofconstraint conditionOfoptimization problem All offeasible solution(Limited number) space to search.When the algorithm is implemented, all feasibleSolution spaceConstantly split into smaller and smaller subsets (called branches), and calculate aLower boundOr upper bound (calledDelimitation)。After each branch, no further branches will be made for those subsets whose boundaries exceed the known feasible solution values, so that many subsets of the solution (that is, many nodes on the search tree) can be ignored, thus reducing theSearch scope。This process continues until we find outfeasible solutionSo far, the value of the feasible solution is not greater than the limit of any subset.Therefore, this algorithm can generally obtainOptimal solution。
AndGreedy Algorithm Similarly, this method is also used to design algorithms for combinatorial optimization problems. The difference is that it searches the whole possible solution space of the problemTime complexitythangreedy algorithm High, but its advantages areExhaustion methodSimilarly, it can ensure that the best solution to the problem can be found, and this method is not blindExhaustive search, but in the process of searching, you can stop in the middle of the process for some impossible to get the optimal solutionSubspaceFurther search (similar to theprune), so it is more efficient than the exhaustion method.[2]
Backtracking method
Backtracking method(Exploration and backtracking method) is a kind of optimizationSearch method, search forward according to the optimization conditions to reach the goal.However, when a certain step is explored and it is found that the original selection is not optimal or the goal cannot be reached, it is necessary to return to one step for re selection. This technology of returning to the next step is called backtracking method, and the point in a certain state that meets the backtracking conditions is called "backtracking point".
The basic idea is that in the solution space tree containing all solutions of the problemDepth first searchPolicy for, fromRoot nodeStart to explore the solution space tree in depth.When exploring a node, it is necessary to first determine whether the node contains the solution of the problem. If it does, it is necessary to continue exploring from the node. If the node does not contain the solution of the problem, it is necessary to backtrack to its ancestor node layer by layer.(In fact, the backtracking method is rightImplicit graphDepth first search algorithm).If we use the backtracking method to find all the solutions of the problem, we need to backtrack to the root, and all the feasiblesubtreeIt doesn't end until it has been searched all over.However, if the backtracking method is used to find any solution, the search can end as long as one solution of the problem is found.[2]
Description
Announce
edit
There are many methods to describe algorithms, includingnatural languageStructuredflow chart、PseudocodeandPAD DiagramAmong them, the most common is the flow chart and the split thinking method[2]
Algorithms can be broadly divided intothree classes of initials:
1、 Limited,Deterministic algorithmThis kind of algorithm terminates in a limited period of time.They may take a long time to perform the assigned task, but they will still terminate within a certain time.The results of such algorithms often depend on the input values.
2、 Finite, uncertain algorithms such algorithms terminate in a finite time.However, for a given value (or some values), the results of the algorithm are not unique or certain.
3、 Infinite algorithms are those algorithms that do not stop running because there is no defined termination definition condition, or the defined condition cannot be satisfied by the input data.In general, the infinite algorithm results from the undefined defined termination condition.[2]
history
Announce
edit
"Algorithm" is the Chinese name of the algorithm《Zhou Bi Suanjing》;The English name Algorithm comes from the 9th centuryPersiaMathematician al Khwarizmi proposed the concept of algorithm mathematically."Algorithm" was originally "algorism", meaning Arabic numeralAlgorithm, evolved into“algorithm"。Euclid algorithmIt is considered to be the first algorithm in history.The first program was written by Ada Byron in 1842 forBabbageAnalyzerWrite solutionBernoulli equationAda Byron is considered by most people to be the first in the worldprogrammer。becauseCharles Babbage (Charles Babbage) failed to complete his Babbage analyzer, and this algorithm could not be executed on the Babbage analyzer.Because "well defined procedure" lacks precise mathematical definition, mathematicians and logicians in the 19th and early 20th centuries had difficulties in defining algorithms.Turing, a British mathematician in the 20th century, put forward the famous Turing thesis and proposed a hypothetical computerAbstract model, this model is calledTuring machine。The emergence of Turing machine solved the problem of algorithm definition, and Turing thought played an important role in the development of algorithm.[2]