Algorithm Introduction and algorithm analysis
Introduction to Algorithms
Definition of the algorithm:
The algorithm is a description of the solution steps of a particular problem and is a finite sequence of instructions. (so as long as the above conditions are met, even a very simple loop is an algorithm)
algorithm has 5 Characteristics of:
Input
Output
have poor sex
Certainty
Feasibility
What is a good algorithm:
Correctness
Robustness
Simplicity of
Abstract grading
High efficiency of
Algorithm Analysis:
Efficiency is an important criterion for evaluating whether an algorithm is a good algorithm, so how can we determine whether the algorithm is efficient? Some people say, the algorithm in the program language implementation, and then enter a number of test data to detect the actual speed of operation (time frequency) and the space cost is good Bai! This method of post-mortem statistics can not be accurately detected, it involves the computer hardware and software and other environmental factors. Therefore, the method of pre-analysis and estimation, progressive complexity analysis (progressive time complexity and progressive space complexity) is generally used.
?
Complexity of Time
When the problem is sufficiently large, the execution times of the basic statements in the algorithm are in the progressive order, denoted by the large O notation.
Complexity of space
Refers to the number of secondary spaces that are required during the execution of the algorithm.
?
Algorithm analysis and calculation method:
In general, we pay more attention to the time performance of the algorithm, so we explain the computational method of time complexity.
Common algorithm time complexity is from small to large in turn: 0 (1) < 0 (log2n) < 0 (n) < 0 (nlog2n) < 0 (n2) < 0 (n3) < ... < 0 (2n) < 0 (n!) /p>
First, find out that the most executed statement in the statement as the basic statement, calculate the number of executions of the basic statement, take its order of magnitude into the large O.
To understand this approach, you need to understand a definition and a theorem.
Defined:
If there are two positive constants C and N0, for any n>=n0, there is t (n) <=c * f (n), which is called T (N) =o (f (n)).
(This definition shows that the function t (n) and F (n) have the same growth trend, and that the growth of T (N) is at most convergent to the growth of function f (n). )
Theorem:
If a (n) =amnm + am-1nm-1 + am-2nm-2 + am-3nm-3 + am-4nm-4 + ... + a1n1 + a0, then a (n) =o (nm).
?
Examples
(Reproduced from http://blog.csdn.net/booirror/article/details/7707551/):
" Span style= "font-family:arial" >1 "If the execution time of the algorithm does not follow the problem size n o (1)
x=91; y=100;
while (y>0) if (x>100) {x=x-10;y--;} else x + +;
Answer: T (N) =o (1) ,
This app looks a little scary, in a total loop run 1100 times, but we see N No ?
No, No. The operation of this program is independent of N,
Even if it circulates for another 10,000 years, we don't care about him, just a function of constant order.
?
" 2 "When there are several loop statements, the time complexity of the algorithm is the frequency of the most internal statements in the Loop statement with the highest number of nested layers f (N) decision-making.
? x=1;?
for (i=1;i<=n;i++)?
? ? ? ? for (j=1;j<=i;j++)
?????? for (k=1;k<=j;k++)
?? ? ? ? ? ? ? x + +;
The most frequent statement in this program segment is (5), while the number of executions in the inner loop is not directly related to the problem size n , but is associated with the variable value of the outer loop. The number of outermost loops is directly related to n , so the number of executions ofstatements (5) can be performed from the inner Loop to the outer layer :?? The time complexity of the program segment is T (n) =o (n3/6+ Low-time items ) =o (n3)
?
" 3 "The time complexity of the algorithm depends not only on the scale of the problem, but also on the initial state of the input instance.
in the value A[0..n-1] To find the given value in K the algorithm is roughly as follows: ???
i=n-1;??????
while (i>=0&& (a[i]!=k))?? ? ??
? ? ? i--;????
return i;????????
the statements in this algorithm(3)the frequency of the problem not only with the sizeNalso associated with the input instance .Athe values of each element andKthe value of the: ① IfAdoes not match theKan equal element, the statement(3)the frequencyf (n) =n;② IfAthe last element equalsK,the statement(3)the frequencyf (N)is constant0.
Algorithm Introduction and algorithm analysis