Data structure, data structure and Algorithm
Program = Algorithm + Data Structure
Professor Niklaus Wirth proposed:
Program = Algorithm + Data Structure
The above formula illustrates the following two problems:
(1) algorithms determine how to construct and organize data (algorithms → data structures ).
(2) algorithm selection depends on the basic data structure (data structure → algorithm ).
Software = program + documentation (software engineering point of view)
Solving Non-numerical computing problems
The main consideration is to design a suitable data structure and corresponding algorithms.
That is: first, we need to consider how to express, organize, and store related information?
Therefore, we can think that the data structure is a discipline that studies the computer operation objects and their relationships and operations in non-numerical computing programming problems.
Basic concepts of Data Structure
Concepts:
Data ):It is a symbolic representation of an objective object. In computer science, it refers to all the symbols that can be input into a computer and processed by a computer program.
Data Element ):It is the basic unit of data. It is usually considered and processed as a whole in computer programs.
Data items are the smallest units that are inseparable from each other. A data element can be composed of several data items.
Data Object ):Is a set of data elements of the same nature. Is a subset of data.
What is a data structure?
Definition 1 --
Is a set of data elements that have one or more specific relationships with each other.
Definition 2 --
A batch of data (or a set of structured data elements) organized by a logical relationship is applied to computer languages and stored in computer memory in a certain storage representation, it defines a set of operations.
Logical Structure
Abstract relationships between data elements (Logical Structure for short ).
It is independent of data storage. It is a mathematical model abstracted from a specific problem.
Storage Structure (physical structure )--
Data elements and their relationships (the logical structure of data) are stored in computer memory.
Is the implementation of the Logical Structure in computer language, it depends on computer language.
Operation (algorithm)
Logical Structure-division method 1
(1) linear structure --
There is only one Start Node and one terminal node, and all nodes have at most one direct forward trend and one successor.
For example: Linear table, stack, queue, string
(2) nonlinear structure --
One node may have multiple direct frontend and direct successors.
For example, tree and graph.
Logical Structure-division method 2
1. The data elements in the collection structure have no other relationships except the same type.
2. There is a one-to-one relationship between data elements in a linear structure.
3. There is a one-to-many relationship between data elements in the tree structure.
4. There is a many-to-many relationship between data elements in a graph or mesh structure.
Storage Structure
Storage Structure:
(1) representation of the data element's own value (data field)
(2) representation of the relationship between the node and other nodes (Chain Domain)
There are two basic storage methods:
(1) sequential storage method (sequential storage structure)
(2) link storage method (chained storage structure)
The same logical structure can adopt different storage methods (one or both of the above). This mainly takes into account the convenience of computing and the time-space requirements of algorithms.
Data structure calculation (operations on data)
(1) Create a data structure;
(2) Destroy a data structure;
(3) Delete A data element from a data structure;
(4) Insert a data element into a data structure;
Access a data structure );
Modify a data structure (Data Element in)
Sort sorts a data structure (Sort );
⑻ Searches for a data structure ).
Data Types and abstract data types
Data Type: the data type of a variable in a programming language.
Example 1: In FORTRAN, the Data Types of variables include integer, real, and plural
Example 2: In C
Data Type: basic type and construction type
Basic types: integer, floating point, and complex
Construction type: array, structure, union, pointer, enumeration type, custom
Note: The data structure is different from the data type and also from the data object. It not only describes the data type of the data object, but also describes the relationship between each element of the data object.
Abstract Data Type (ADT): Refers to a mathematical model and a group of operations defined on the model.
The definition of ADT is only a set of logical feature descriptions, and has nothing to do with the representation and implementation in the computer. Therefore, no matter how the internal structure of ADT changes, as long as its mathematical characteristics remain unchanged, it will not affect its external use.
The formal definition of ADT is triple: ADT = (D, S, P)
Where: D is the data object, S is the relational set on D, and P is the basic operation set on D.
The general definition form of ADT is:
ADT <abstract data type Name> {
Data Object: <data object definition>
Data Relationship: <Data Relationship definition>
Basic operations: <Definition of basic operations>
} ADT <abstract data type Name>
The definition of the Data Object and data relationship is described by the pseudo code.
The basic operation is defined as follows:
<Basic operation Name> (<parameter table>)
Initial Condition: <initial condition description>
Operation Result: <operation result description>
Initial Condition: describes the conditions that should be met by the data structure and parameters before the operation is executed. If not, the operation fails and the corresponding error information is returned.
Operation Result: describes the changes in the data structure and returned results after the operation is completed.
Algorithm
Concepts and descriptions of algorithms:
What is an algorithm?
The so-called Algorithm (Algorithm) is a description of the specific problem solving method (STEP.
A finite sequence composed of several commands to solve a specific problem.
A method suitable for solving problems implemented by computer programs
Concepts and descriptions of algorithms:
An algorithm must meet the following five criteria:
(1) Poor-it must be terminated after a limited number of commands are executed.
(2) certainty (no ambiguity )-
Each step of an algorithm must be defined with no ambiguity.
(3) Availability-
Each step of the algorithm must be feasible, that is, each step can be completed within a limited time.
(4) input data-
An algorithm has n (n> = 0) Initial data input.
(5) output data-
An algorithm outputs one or more valid information that has a certain relationship with the input.
The time complexity and growth rate of common functions increase progressively by quantity.
Constant order O (1)
Logarithm level O (log2n)
Linear order O (n)
Linear logarithm order O (nlog2n)
Level 2 O (n2)
Cubic order O (n3)
......
K power level O (nk)
Exponential order O (2n)
Space complexity)
: A measure of the size of the storage space required to run an algorithm in a computer after it is compiled into a program. Note: S (n) = O (f (n ))
Where: n is the scale (or size) of the problem)
The Bucket generally includes three aspects:
Storage space occupied by the directive constant variable;
The storage space occupied by the input data;
Auxiliary (storage) space.
Generally, the space complexity of an algorithm refers to the auxiliary space.
One-dimensional array a [n]: space complexity O (n)
Two-dimensional array a [n] [m]: space complexity O (n * m)