【Data structure】Basic concepts of data structure and algorithm

Table of contents

1. The basic concept of data structure

Two, the three elements of the data structure

 3. Data types and abstract data types

Fourth, the basic concept of the algorithm

 5. Measurement of Algorithm Efficiency


Learning content: 1. Use program code to informatize real-world problems

                   2. Use computers to efficiently process this information to create value

Overview:

1. The basic concept of data structure

1. Data: Data is the carrier of information , a collection of numbers, characters and all symbols that can be input into a computer and recognized and processed by . Data is the raw material that computer programs process.

2. Processing data

Early computers - only used to deal with purely numerical problems

Modern computers - often deal with non-numerical problems

Non-numerical questions:

        (1) Specific information about each individual

        (2) Relationships between individuals (linear relationships, network-like relationships)

Represent individual information - data elements

 A data element is the fundamental unit of data that is usually considered and processed as a whole.
A data element can be composed of several data items , and a data item is the smallest indivisible unit that constitutes a data element.

Example:

If a data item is composed of multiple more subdivided attributes, the data item is called a composite item.

 Data object: A collection of data elements with the same nature, which is a subset of data.

 Describe the relationship between individuals - data structure

Data structure: A collection of data elements that have one or more specific relationships with each other

        The data structure course focuses on the relationship between data elements and the operations on these data elements, rather than the specific content of data items.

Two, the three elements of the data structure

1. Logical structure

Collection structure:

 Linear structure:

Tree structure:

 Figure/network structure:

 2. Operation of data——Aiming at a certain logical structure, combined with actual needs, define basic operations

Basic operations on linear structures:

① Find the i-th data element

②Insert a new data element at the i-th position

③Delete the data element at the i-th position....

3. Physical structure/storage structure-represents the logical relationship of data elements

Sequential storage:

        Store logically adjacent elements in physically adjacent storage units, and the relationship between elements is reflected by the adjacency of the storage units.

Chain storage:

        Logically adjacent elements may not be adjacent in physical position , and the logical relationship between elements is represented by pointers indicating the storage addresses of the elements.

 Index storage:

        While storing element information, an additional index table is also established . Each item in the index table is called an index item , and the general form of an index item is ( keyword, address )

 Hash storage (the hash table will be discussed in detail in Chapter 6):

Calculate the storage address of the element directly         according to the keyword of the element , also known as hash (Hash) storage

 Sequential and non-sequential storage:

1. If sequential storage is used, each data element must be physically continuous ;

      If non-sequential storage is used, each data element can be physically discrete .
2. The storage structure of data will affect the convenience of storage space allocation
3. The storage structure of data will affect the speed of data calculation

The definition of operation is the function of pointing out the operation for the logical structure; the realization
of the operation is the specific operation steps of pointing out the operation for the storage structure.

 3. Data types and abstract data types

A data type is a general term for a collection of values ​​and a set of operations defined on this collection .

1) Atom type. A data type whose value cannot be subdivided.
2) Structure type. A data type whose value can be subdivided into components (components).

Abstract Data Type (Abstract Data Type, ADT) is an abstract data organization and its related operations

 Knowledge review and key points:

Fourth, the basic concept of the algorithm

 1. Algorithm is a description of the steps to solve a specific problem . It is a finite sequence of instructions, each of which represents one or more operations.

 Example 1:

 Example 2:

 2. Five characteristics of the algorithm:

Infinity . An algorithm must always end after executing a finite number of steps, each of which can be completed in finite time.

Note: The algorithm must be finite (use finite steps to solve a specific problem), while the program (eg: WeChat) can be infinite .

Certainty . Each instruction in the algorithm must have an exact meaning,

Only the same output can be obtained for the same input (result uniqueness) .

 feasibility . The operations described in the algorithm can be realized by performing the basic operations that have been realized for a limited number of times .

 input . An algorithm has or more inputs , which are taken from a particular set of objects.

 output . An algorithm has one or more outputs , which are quantities that have some specific relationship to the inputs.

3. The characteristics of a "good" algorithm -- the goal to pursue as much as possible when designing an algorithm

1) Correctness. The algorithm should be able to correctly solve the solution problem.
2) Readability. Algorithms should be well readable to help people understand them.
3) robustness. When illegal data is input , the algorithm can react or process it appropriately without producing inexplicable output results.

4) High efficiency (less time spent, low time complexity ) and low storage requirements (no memory consumption, low space complexity )

 Knowledge review and key points:

 5. Measurement of Algorithm Efficiency

 Evaluate algorithm time overhead method:

(1) Problems in post-event statistics:

It is related to the performance of the machine , such as: supercomputer vs single chip microcomputer

It is related to the programming language , the higher the language, the lower the execution efficiency

It is related to the quality of the machine instructions generated by the compiler.
Some algorithms cannot be counted afterwards , such as: missile control algorithm

 Optimization ideas: exclude external factors and prior estimates that have nothing to do with the algorithm itself.

(2) Algorithm time complexity

        Pre-estimate the relationship between the time cost T(n) of the algorithm and the problem size n (T means "time")

Order of magnitude: 0(1)< O(log,n)<o(n)< O(nlog,n)<0(n²)< O( )<0(2)<O(n!)<O(n n^{3}" )---- often refers to the order of the power

for example:

 The code executed sequentially will only affect the constant item, which can be ignored.
Just pick a basic operation in the loop and analyze the relationship between its execution times and n

Therefore, the time complexity is: T(n) = 3n+3= O(n)

 If there are multiple levels of nested loops , just focus on the deepest loop looping a few times

So the time complexity is T(n)= O(n)+ O( n^{2}) = O( n^{2})

Calculate the time complexity T(n) of the above algorithm:

Let the statement frequency of the deepest loop (the total number of loops) be x, then

It can be seen from the loop condition that at the end of the loop, it just satisfies 2^{x}>n

x= \log _{2}n +1
T(n) = o(x)= O(\log _{2}n)

Best case: element n is in the first position - best time complexity T(n)=O(1)
worst case : element n is in the last position - worst time complexity T(n)=O (n)

Number of cycles x= (1+2+3+...+n)*(1/n)= (n(1+n))/2*(1/n)= (1+n)/2≈n →T(n)=O(x)=O(n)
Average case : Assume that the probability of element n in any position is the same as 1/n—the average time complexity T(n)=O(n)

Knowledge review and important test points:

 Space complexity:

        Relationship between space overhead (memory overhead) and problem size n

No matter how the scale of the problem changes, the memory space required for the algorithm to run is a fixed constant, and the algorithm space complexity is s(n) = O(1) 

Note: S stands for "Space"

Assuming an int variable occupies 4B...
then the required memory space = 4+4n +4 = 4n +8→S(n) = O(n)

 Memory overhead caused by function recursive calls:

Knowledge review and important test points: 

Guess you like

Origin blog.csdn.net/m0_75045191/article/details/131441159