Data structure and algorithm-2

Learning Video: Data Structure and Algorithm Foundation (Qingdao University-Wang Zhuo)

Definition of algorithm

Methods and steps to solve the problem (infiniteness, certainty, feasibility, input, output);
program = data structure + algorithm.
ps: Robustness means that the method of handling errors should not be to interrupt the execution of the program, but to output a value representing the error.

Algorithmic time measurement

Generally , the method of pre-analysis is adopted : the
running time of the algorithm = the time required for a simple operation * the number of simple operations; the
running time of the algorithm = the number of times each sentence is executed (also called the sentence frequency ) * the sentence points to the time required The time required for
each statement to be executed once is generally random, and has nothing to do with the algorithm, so we can assume that the time required for each statement is unit time , so that the running time of the algorithm depends only on the frequency of the statement .
Algorithm running time = sentence frequency * unit time;
ex: n*n matrix multiplication

for(i = 1; i <= n; i++{
    
    							//执行n+1次
	for(j = 1; j <= n; j++ ){
    
    						//执行n*(n+1)次
			c[i][j] = 0//执行n*n次
			for(k = 0; k < n; k++)					//执行n*n*(n+1)次
			c[i][j] = c[i][j] + a[i][k] * b[k][j]//执行n*n*n次
	}
}

The time consumption of the above algorithm T(n)= 2 * n³ + 3 * n² + 2n + 1;

ex: Comparison of algorithms
T1(n) = 10n T2(n) = 5n²
According to the asymptotic time complexity of the algorithm : if there is an auxiliary function f(n), so that when n approaches infinity, T(n)/f The limit value of (n) is a constant that is not equal to zero, then f(n) is said to be a function of the same order of T(n), denoted as T(n) = O(f(n)), O(f(n) ) Is the progressive time complexity of the algorithm;
so T1 is good;

ex: T(n)= 2 * n³ + 3 * n² + 2n + 1, find the time complexity? When
n approaches infinity, T(n)/n³ = 2;
T(n) = O(n³ );

Therefore, under normal circumstances, it is not necessary to calculate the number of executions of all operations, only the number of executions of the basic operations in the algorithm (usually the largest number, that is, the largest frequency) execution number n,

Sorting: n is the number of records;
matrix: n is the order of the matrix;
polynomial: n is the number of items;
set: n is the number of elements;
tree: n is the number of nodes;
graph: n is the number of fixed points or edges;

Time complexity analysis example

attention : the time complexity is determined by the frequency of the deepest nested sentence
first: find the most executed number;
second: find f(n);
third:: take its order of magnitude and use the symbol "O"

Sometimes, the number of repeated executions of the basic operations in the algorithm varies with the data set of the problem. For example, look for the element equal to e in a[i] and return its location. The best case is 1 time, and the worst case is n Second, the evaluation time complexity is O(n); (generally consider the worst)

Addition rules:
T(n) = T1(n) + T2(n) = O(f(n)) + O(g(n)) = O(Max(f(n),g(n)))
multiplication Rules:
T(n) = T1(n) * T2(n) = O(f(n)) * O(g(n)) = O(f(n) * g(n))

Insert picture description here

Algorithm time efficiency comparison
Insert picture description here
Insert picture description here

Insert picture description here

Algorithmic spatial measurement

Space complexity : A measure of the storage space required by the algorithm
S(n) = O(f(n)), where n is the size of the problem

Learning Video: Data Structure and Algorithm Foundation (Qingdao University-Wang Zhuo)

Guess you like

Origin blog.csdn.net/fly_ship/article/details/107730997