【Data Structure Learning Record 1】—— Introduction

Zero. Foreword

I opened another pit to record my own data structure learning. This article is based on the C language version of Yan Weimin's "Data Structure" published by Tsinghua University.

1. Basic concepts and terminology

数据: Data is a symbolic representation of objective things. In computer science, it refers to all symbols that can be input into a computer and processed by a computer program.

数据元素: It is the basic unit of data, which is usually considered and processed as a whole in a computer program. Sometimes, a data element can be composed of several 数据项, such as our common structure.

数据对象: It is a collection of data elements of the same nature, and a subset of data. For example, our common integer data objectN={-1,0,1,2···}

数据结构: There is no unanimously accepted definition of this concept. Therefore, in the "Data Structure" published by Yan Weimin of Tsinghua University, it is said that it 数据结构is a collection of data elements with one or more specific relationships between each other. In any problem, data elements cannot exist in isolation, and there is a certain relationship between them. The relationship between these elements is called 结构. Generally speaking, there are the following four common basic structures:

  1. Set: Except for the relationship of "belonging to the same set", the data elements in the structure have no other relationship.
  2. Linear structure: There is a one-to-one relationship between the elements in the data.
  3. Tree structure: There is a one-to-many relationship between data elements in the structure.
  4. Graphical or network structure: There is a multiple-to-many relationship.

It sounds similar to the basic correspondence of the database.

[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-mhqKaffU-1603164270609)(C:\Users\Kanna\AppData\Roaming\Typora\typora-user-images\ image-20201020102040691.png)]

The definition of data structure is a kind of data description of data objects, which can be understood as an abstract mathematical model used to describe its logical relationship, so this is also called data 逻辑结构.

The representation of a data structure in a computer (that is, where it is stored in your memory), also known as 物理结构OR 储存结构.

Common storage structures are 顺序储存结构and 链式储存结构. The physical location of the former is continuous, and the storage order of the latter is not necessarily continuous, and it needs to be stored in a special way.串联起来

数据类型: Data type is a concept closely related to data structure, generally used to describe the characteristics of program operation objects. Therefore, the data type is a set of values and is defined on the set of values a set of generic term operation . As the name implies, that is to say, the data type may include variables and functions that manipulate sets of these variables. This is very close to the concept.

抽象数据类型(abstract data type): Abbreviation ADT, is a mathematical model and a set of operations defined on the model.

2. How to represent abstract data types

Everything else is easy to say, just follow the style of C language. And ADT pseudo code, we can understand the format, put an example:

ADT 抽象数据类型名 {
    
    
    数据对象:<数据对象的定义>
    数据关系:<数据关系的定义>
    基本操作:<基本操作的定义>
}ADT 抽象数据的类型名

among them

基本操作名 (参数表)
    初始条件:<初始条件描述>
    操作结果:<操作结果描述>

E.g:

ADT List { Data object: D={a i | a i ∈ ElemSet, i = 1,2,3,4···,n, n≥0} Data relation: R1={<a i-1 ,a i > |a i-1 , a i-1 ∈ ElemSet, i = 2,3,4···,n} Basic operation: ListEmpty(L) Initial condition: The linear list L already exists. Operation result: If L is an empty list, return True, otherwise return False. ··· }







3. Algorithm and algorithm analysis

1. Algorithm

The algorithm has five important characteristics:

  1. Finiteness: An algorithm always ends after finite steps, and each step can be completed in finite time. (Of course this finite means that time can be accepted by you, not the mathematical definition that can be large enough but not infinite).
  2. Certainty: Every instruction has a clear meaning, and readers should not be ambiguous. And there is only one execution path under any condition, that is, the same input and the same output.
  3. Feasibility: An algorithm is feasible, that is, the operations described in the algorithm can be implemented by performing a limited number of basic operations that have been implemented.
  4. Input: An algorithm must have zero or more inputs.
  5. Output: An algorithm must have one or more outputs.

2. Algorithm design requirements

A good algorithm generally needs to consider achieving the following goals:

  1. Correctness: Correctness generally includes the following aspects:

    ①. The program has no grammatical errors

    ②. The result that the program can meet the specification requirements for several sets of input data.

    ③. The program should be able to obtain results that meet the specification requirements for carefully selected typical, harsh and difficult inputs.

    ④. For all legal inputs, the result that meets the specification requirements can be obtained.

  2. Readability: The algorithm is mainly used for people to see, so it must be understood by others.

  3. Robustness: In short, if the input is illegal, it can make appropriate operations to deal with it, instead of outputting inexplicable results or making the program look weird.

  4. Efficiency and low storage requirements: In short, the less time it takes, the better, and the less memory it takes up, the better. In other words, the 时间复杂度sum 空间复杂度is smaller

3. Measurement of algorithm efficiency

Generally speaking, when we write an algorithm, we usually have a 事前估算sum 事后统计.

In general, the number of repeated executions of the basic operation in the algorithm is a certain function f(n) of the problem size n, and the time metric of the algorithm is denoted as
T (n) = O (f (n)) T(n) = O( f(n))T(n)=O ( f ( n ) )
It means that as the problem size n increases, the growth rate of the algorithm execution time is the same as the growth rate of f(n), which is called the algorithm渐近时间复杂度, abbreviated时间复杂度

Three examples:

  1.  {
          
          ++x; s = 0;}
    
  2.  for (i = 1; i <= n; ++j) {
          
           ++s; }
    
  3.  for (j = 1; j <= n; ++j)
     {
          
          
         for (i = 1; i <= n; i++)
         {
          
          
             ++s;
         }
     }
    

For these three examples, their complexity is O(1), O(n), O(n 2 )

4. Space requirements of the algorithm

Similar to time complexity, it is used in this book 空间复杂度as a measure of the space required by the algorithm. Denoted as:
S (n) = O (f (m)) S(n)=O(f(m))S(n)=O ( f ( m ) )
can basically equal the size of the space occupied by the variable used.

As for how to calculate the time complexity and space complexity of an algorithm, very complicated calculations are required, which will be recorded in a section later.

Guess you like

Origin blog.csdn.net/u011017694/article/details/109177718