01. Concept and algorithm basis of Data Structure, 01 Data Structure
Data Structure concepts and algorithm BasicsI. Data Structure Concept1. Data: A symbol used to describe objective transactions. It is an object that can be operated on by computers. It is a collection of symbols that can be recognized by computers and input to computers for processing. Data includes not only integer, real, and other numeric types, but also non-numeric types such as characters, sounds, images, and videos.2. Data ElementsIt is a basic unit that makes up data and has a certain significance. It is usually processed as a whole in a computer and also becomes a record. For example, in animals, cattle, horses, and sheep all belong to data elements.3. Data items: A data element can be composed of several data items, which are the smallest units that cannot be separated. Data elements such as people can have data items such as eyes, ears, and noses.4. Data Objects: A set of data elements with the same nature. It is a subset of data. The data element has the same quantity and type. For example, all people have the same data items such as name, birthday, and gender. In a sense, data objects are also called data (data objects are subsets of data ).5. Data Structure: Different data elements are not independent. Second, they have specific relationships, which can be called structures. The so-called data structure refers to a set of data elements with one or more specific relationships between each other-the data organization form.6. logical and physical structures(1) Logical Structure: it refers to the relationship between data elements in the data object. The logical structure is designed to solve a specific problem. Based on the understanding of the problem, a proper data structure is selected to represent the logical relationship between data elements. A. Set structure: Except for the same set, the data elements in the set structure have no other relationships. B. linear structure: the data elements in the linear structure have one-to-one relationships. C. Tree Structure: there is a one-to-many hierarchy between data elements in the tree structure. D. Graphic Structure: The data elements of the graphic structure are many-to-many relationships. Note: When we use a logical structure to represent data, we need to consider each data element as a node (represented by a circle ); the logical relationship between elements is expressed by the line between nodes. (2) Physical Structure: it refers to the storage form of the logic structure of exponential data in the computer, that is, how to store data elements in the memory of the computer, the data storage structure reflects the relationship between data elements. A. Ordered Storage Structure: stores data elements in address-contiguous storage units. The logical and physical relationships between the data are consistent. B. chained Storage Structure: stores data elements in any storage unit, which can be continuous or discontinuous. The storage relationship of data elements does not reflect the logical relationship. Therefore, you need to use a pointer to store the address of the data element. In this way, you can find the location of the associated data element through the address. Conclusion: The logical structure is problem-oriented. The physical structure is computer-oriented, and its basic goal is to store data and logical relationships in the computer's memory.7. Data Types and abstract data types(1) Data Type: Refers to a set of values of the same nature and a general term for some operations on this set.(2) abstract data type (ADT): Refers to a data model and a group of operations defined on the model. The meaning of "abstraction" lies in the mathematical abstraction of data types. An abstract data type is defined as a data object, the relationship between each data element in the data object, and the operations of data elements. In short, abstract data types reflect the problem decomposition, abstraction, and Information Hiding features in programming. ABSTRACT Data Types break down real-life problems into small and easy-to-handle problems, and then create a mathematical model that can be processed by a computer. The time details of each function module are used as an independent unit to hide the specific implementation process. Standard format for describing abstract data types:
ADT abstract Data type name definition of the logical relationship between Data elements Operation 1 Initial Condition Operation result description Operation 2... Operation n... endADT
Ii. algorithm Basics
1. What is an algorithm?An algorithm is a description of the steps for solving a specific problem. It represents a finite sequence of commands in a computer, and each instruction represents one or more operations. Specifically, commands need to be expressed as operation sequences to solve a certain type of problem. Operation sequences include a group of operations, and each operation completes specific functions, this is the algorithm.
2. What are the features of an algorithm?(1) input and output: the algorithm has zero or multiple inputs; at least one or more outputs. (2) poor: the algorithm automatically ends after a limited step, without infinite loops, and each step is completed within an acceptable period of time. (3) certainty: each step of an algorithm has a definite meaning and does not have ambiguity. (4) Feasibility: each step of the algorithm must be feasible.
3. What are the design requirements of algorithms?(1) Correctness (2) Readability (3) Robustness: when the input data is invalid, the algorithm can also perform relevant processing, rather than generating exceptional or inexplicable results (4) high time efficiency and low storage capacity
4. algorithm efficiency measurement method(1) post-event statistical method: This method is mainly to compare the running time of programs compiled by different algorithms by designing the test program data and using the computer timer. (2) pre-analysis estimation method: the time consumed by a program written in advanced programming to run on a computer depends on the policies and methods used by the algorithm (the Foundation of the algorithm ); the quality of the Code produced by compilation (determined by the software environment), the input size of the problem, and the speed of machine command execution (determined by hardware ). Note: The most reliable way to determine the running time is to calculate the number of executions of the basic operations consumed for the running time. The running time is proportional to the count. An algorithm, with n increasing, is better than another algorithm or worse than another algorithm.
Iii. algorithm time complexity
1. Definition: During algorithm analysis, the number of statement executions T (n) is a function about the problem scale n. In this way, T (n) changes with n and T (n) is determined). The time complexity of an algorithm, that is, the time measurement of an algorithm, is recorded as T (n) = O (f (n). It indicates that the problem increases with the increase of n, the growth rate of Algorithm Execution time is the same as that of f (n). It is called the progressive time complexity of an algorithm. F (n) is a function of problem scale n.
Among them, O () is used to express the algorithm's time complexity ). Generally, as n increases, T (n) is the slowest growing algorithm.
2. Method for pushing to large O-order: Analyze the time complexity of an algorithm. The principles are as follows:
Principle 1: replace all addition constants in the running time with constant 1 --------------- example: f (n) = 3, then according to the time complexity of the large order method is O (1) Principle 2: in the modified number of running functions, only the highest-order items are retained ------------ example: f (n) = 3n + 1 Principle 3: If the highest-order items exist and are not 1, then the constant multiplied by this is removed. ----- Example: --------- the time complexity of the Large O-order method is O (n) 0.
3. Common time complexity: The time complexity of the algorithm can be obtained based on the number of execution of Program Statements (1) Source Code of constant order: int sum = 0, n = 100; /* Run Once */sum = (1 + n) * n/2;/* Run Once */printf ("% d", sum ); /* execute once */The number of program executions is 3. According to principle 1, the time complexity is O (1 ). (2) linear source code: int I; for (I = 0; I <n; <I ++ ){.... /* sequence of program steps with a time complexity of O (1 */}
The number of program executions is (n + a), where a is a constant. According to Principle 2 and 3, the time complexity of the algorithm is O (n ). To determine the order of an algorithm, we often need to determine the number of times a specific statement or statement set runs. Therefore, the key to analyzing the complexity of algorithms is to analyze the running status of the loop structure. (3) source code of the logarithm level: int count = 1; while (count <n) {count = count * 2 ;.... /* program step sequence with a time complexity of O (1) */} since each count multiplied by 2, it is closer to n, that is to say, if the number of 2 is multiplied by greater than n, the loop will exit. From 2 ^ x <n x <log2 ^ n, the number of program executions is log2 ^ n = log (2*2*2*2*2 *... * n ). The time complexity of the algorithm based on principle 2 and 3 is O (logn ). (4) squared source code 1: int I, j; for (I = 0; I <m; I ++) {for (j = 0; j <n; j ++ );{.... /* program step sequence with a time complexity of O (1) */} program statement execution times: f (n) = m * n + 1 +, a is a constant. According to the large O-order method, the time complexity of the algorithm is O (m * n ). source code 2: int I, j; for (I = 0; I <n; I ++) {for (j = I; j <n; j ++) {/* program step sequence with time complexity of O (1) */} analysis: When I = 0, the internal cycle is executed n times; when I = 1, executes n-1 times ;.... when I = n-1, it is executed once. Therefore, the number of Program Statement executions is f (n) = n + (n-1) + (n-2) +... + 1 = n (n + 1)/2 = n ^ 2/2 + n/2 + a, where a is a constant. According to the large O-order method, the time complexity of the algorithm is O (n ^ 2 ).
4. Worst case and average case(1) Worst Case: The worst case is the guarantee that the running time will not be broken. This is the most important requirement in applications. Generally, unless otherwise specified, the running time we mentioned is the worst-case running time.
(2) average:The average running time is the most meaningful in all cases because it is the expected running time.
Iv. algorithm space complexity
The control complexity of an algorithm is implemented by the storage space required by the computing algorithm. The calculation formula of the control complexity is as follows: S (n) = O (f (n, n is the scale of the problem, and f (n) Is the function of the statement about the storage control occupied by n. "Time Complexity" refers to the running time requirement, and "space complexity" refers to the space requirement. In short, the advantages and disadvantages of algorithms directly determine the efficiency of program running.