[Python data structure and algorithm] (1) Introduction: basic concepts, algorithm analysis, performance of Python structure

1 Basic concepts

A data structure
is a collection of data elements that have one or more specific relationships with each other. There are generally four basic types of structures: collections, linear structures, tree structures, and graph or network structures.

Algorithm
A description of the steps to solve a property problem, as a finite sequence of instructions. An algorithm has the following five important properties:
Finiteness : the number of execution steps is finite, and the execution time is finite
Certainty : there is only one execution path, and the same output can only be obtained for the same input
Feasibility : the operation described in the algorithm Inputs can be realized by performing a finite number of basic operations that have been implemented : an algorithm
has zero or more inputs
output : an algorithm has one or more outputs

The difference between a program and an algorithm is that the program [implements] the algorithm, and the algorithm does not depend on the operating environment, programming language, etc., and the program compiled in C language is definitely different from Python.

Data type
A data type is a general term for a set of values ​​and a set of operations defined on this set of values. For example, for an integer variable in C language, its value set is an integer in a certain interval, and the operations defined on it are operations such as addition, subtraction, multiplication, and division.

The abstract data type
abstract data type, referred to as ADT, is a mathematical model and a set of operations defined on the model. Abstract data types and data types are actually a concept, but abstract data types have a wider category, such as stacks and queues that will be introduced later, all belong to abstract data types.

2 Algorithm analysis

2.1 Requirements for Algorithm Design

  • Correctness: Contains no grammatical errors and produces satisfactory results
  • Readability: easy to understand, debug and modify
  • Robustness: Illegal input can also be handled appropriately without producing inexplicable output results
  • Efficiency and low storage requirements

2.2 Measurement of Algorithm Efficiency - Big O Notation

Efficiency generally refers to the running time of an algorithm, but the same algorithm may take different time to write in different languages ​​on different machines, so it is necessary to find a way to measure the efficiency of the algorithm regardless of other factors. The usual method is: select an original operation from the algorithm that is a basic operation for the problem under study, and take the number of repetitions of the basic operation as the time measure of the algorithm.

It can be considered that the workload of an algorithm depends only on the size of the problem nnn , the number of repetitions of basic operations in general algorithms is the problem sizenSome function f of n ( n ) f(n)f ( n ) . The time measure of the algorithm is written as: T ( n ) = O ( f ( n ) ) T(n)=O(f(n))T(n)=O ( f ( n ) ) it means that as the problem sizenThe increase of n , the growth rate of the algorithm time andf ( n ) f(n)The growth rate of f ( n ) is the same, which is called the time complexity .

Tidy it up: nnn is the problem size,f ( n ) f(n)f ( n ) is the number of repetitions of the basic operation, which isnnfunction of n , O ( f ( n ) ) O(f(n))O ( f ( n ) ) versusf ( n ) f(n)f ( n ) is processed, generally "look at the highest order, discard the constant", the two have the same growth rate, and finallyO ( f ( n ) ) O(f(n))O ( f ( n ) ) is written asT ( n ) T(n)T(n)

Let's give an example to see how to calculate the time complexity:

a = 5
b = 6
c = 10
for i in range(n):
	for j in range(n):
		x = i * j
		y = j * j
		z = i * j
for k in range(n):
	w = a * k + 45
	x = b * b
d = 33

The assignment statement can be thought of as a basic operation here:

  • First assign a value to abc, and finally assign a value to d. The assignment statement is executed 4 times in total.
  • In the nested loop of i and j, the 3 assignment statements loop n 2 n^2n2 times, so3 n 2 3n^23 n2
  • In the loop of k, 2 assignment statements loop nnn times,2 n 2n2 n
    f ( n ) = 3 n 2 + 2 n + 4 f(n)=3n^2+2n+4f(n)=3 n2+2 n+4 T ( n ) = O ( f ( n ) ) = O ( n 2 ) T(n)=O(f(n))=O(n^2) T(n)=O(f(n))=O ( n2)

What determines the complexity of the algorithm is the statement with the most execution times, which is generally the statement in the innermost loop.

3 Performance of Python structures

3.1 list

operate performance
index, index assignment O(1)
add append O(1)
pop() O(1)
pop(i) O(n)
insert(i,item) O(n)
delete O(n)
traverse O(n)
Include O(n)
slice O(k)
to sort O(nlogn)

3.2 Dictionaries

operate performance
copy O(n)
value, assignment O(1)
delete O(1)
Include O(1)
traverse O(n)

Guess you like

Origin blog.csdn.net/codelady_g/article/details/123373963