Learning Data Structure-Chapter 1: Introduction

Chapter One Introduction

1. Basic concepts and terminology:

  • Data: the information carrier, is described objective things attribute number, and all the characters can be entered into a computer and a computer program to recognize and process the set of symbols
  • Data object: a collection of data elements with the same nature, which is a subset of data
  • Data element: the basic unit of data , usually considered and processed as a whole
  • Data items: an integral component of the data elements a minimum unit

Data contains data objects, and data elements constitute data objects.

The identity information of the owner can be used as a data object, the identity information of each individual can be used as a data element, and the name and number of the identity information can be used as a data item.

1.1 data

Data type (collection + operation)

  • Atomic type: value collection + operation (int, char, float, etc.)
  • Structure type: a collection of structures + operations (list, map, set, etc.)
  • Abstract data type ADT: Abstract data type Abstract data type (ADT) refers to one 数学模型and the one defined on the model 一组操作. The definition of an abstract data type only depends on its set of logical characteristics, and not how it is represented in the computer 实现无关. Usually use ( 数据对象、数据关系、基本操作集) such triples to represent abstract data types.

1.2 Structure

Insert picture description here
In any problem, data elements do not exist in isolation, but there is a certain relationship between them, this kind of data elements are structured with each other 关系称为结构. A data structure is a collection of data elements that have one or more specific relationships with each other. The data structure comprises three aspects: 逻辑结构、存储结构和数据的运算. The logical structure and storage structure of data are two inseparable aspects. An algorithm 设计depends on the selected logical structure, and the algorithm 实现depends on the storage structure used.

1.3 Data structure

The data structure is the presence of one or more particular between mutual relationship between the data elements of the set.

1.4 Three elements of data structure

  • Logical structure
  • Physical structure (storage structure)
  • Data calculation

1.5 Logical structure

逻辑结构Refers to the 逻辑关系logical relationship between data elements to describe the data. It 存储无关is independent of the computer in relation to the data .
The logical structure of the data is divided into 线性结构and非线性结构

Insert picture description here

  • 集合There is no other relationship between the data elements in the structure except the relationship of "belonging to the same set". Similar to mathematical sets
  • 线性结构The only 一对一relationship between the data elements in the structure . Such as queuing
  • 树形结构There is a one-to-many relationship between the data elements in the structure. Family tree
  • 图状结构或网状结构There is a many-to-many relationship between the data elements in the structure. Such as a map

1.6 Physical structure (storage structure)

The storage structure refers to the representation of the data structure in the computer (also known as the image), also known as the physical structure. It includes the representation of data elements and the representation of relationships. The storage structure of data is the realization of the logical structure in computer language, which depends on the computer language. Data storage structure are: 顺序存储、链式存储、索引存储和散列存储.
Insert picture description here

  • Sequential storage: The physical locations of storage are adjacent. (ps. The location is the location of the information in the computer.)
  • Linked storage: The physical locations of storage are 未必adjacent, and adjacent elements can be found by recording the physical locations of adjacent elements.
  • Index storage: similar to a directory, query through an index.
  • Hash storage: directly calculate the physical address of the element through the keyword (detailed later)

Sequential storage: The physical locations of storage are adjacent.
Bold style
Linked storage: the physical locations of storage are 未必adjacent
Insert picture description here

1.7 Calculation of data

Operations include operations defined and implemented , operations defined for the logical structure , implemented for the operation of the physical structure .

Example: Taking people as an example, suppose there is an operation to calculate the appearance of people.
Face value = the sum of the beauty of the facial features (the definition of the calculation is based on the logical structure)
reads the information of each person's facial features through the computer, and then adds the facial value. (The realization of the operation is based on the physical structure)

Insert picture description here

2. Algorithm & Algorithm Evaluation

2.1 Algorithm

Algorithm: A description of solving a specific problem. It is a finite sequence of instructions, each of which represents one or more operations.

2.2 Algorithm characteristics

  • Finiteness: An algorithm must 有穷步end after execution , and each step of the operation is completed in finite time.
  • Feasibility: An algorithm must be feasible, and the operations described in the algorithm can be achieved.
  • Determinism: Every instruction and every sentence in the algorithm must have a definite meaning, and the same input must get the same output.
  • Input: An algorithm must have zero or more inputs.
  • Output: An algorithm must have zero or more outputs.

Insert picture description here

2.2 Algorithm VS Program

Algorithm (instructor) : A method or process to solve a problem, considering how to convert input into output, a problem can have many algorithms.

Program (implementer) : A program is a specific realization of an algorithm in a certain design language.

Finite nature : the algorithm must be finite, the program can be infinite.

Correctness : The algorithm must be correct, the program can be wrong.

Description method : The algorithm can be described in pseudo code, programming language, etc. The program can only be written and run in programming language.

2.3 Measurement of algorithm efficiency

Insert picture description here
时间复杂度:

  • It is used to measure the speed of the algorithm execution time as the problem scale increases. ·
  • Time complexity is a function of problem size: denoted as T(n), time complexity mainly analyzes the order of T(n)
  • T(n)=O(f(n)), Big O notation, f(n) is the frequency of basic operations in the algorithm, generally we consider 最坏情况the time complexity.

计算方法: Take 最快the function item that the algorithm time increases ,把它的系数改为1

Basic operation frequency: the time complexity of the deepest loop execution.

Common time complexity
Insert picture description here

O(1)<O(log~2~^n^)<O(n)<O(nlog~2~^n^)<O(n^2^)<O(n^3^)<O(2^n^)<O(n!)<O(n^n^)

空间复杂度:

  • It is used to measure the speed of the increase in the space required by the algorithm as the problem scale increases:
  • Is a function of problem size: S(n)=0(g(n))

Insert picture description here

2.4 Time complexity

How to calculate the complexity

int sum=0;  //执行1次
for(inti=0;i<=n;i++){
    
      //int i=0执行一次,i<n执行n+1次,i++执行n+1次
    sum=sum+i;
}

Time analysis: The algorithm executed 3n+6 sentences.
Assuming that the execution time of each statement is the same, it is a constant t. Then the total time t=(3n+6)*t
As the problem size n increases, the growth rate of the total time is consistent with the growth rate of n, so the complexity is O(n).
Conclusion:

  • Complexity is about 增长率, so it can be straightforward 忽视常数项,系数化为1.
  • Generally, you can directly pay attention to 循环段基本操作语句(in the example sum=sum+i) 执行次数.

Time complexity calculation ( 单个循环体)
直接关注循环体的执行次数, set to k

Insert picture description here
Time calculation complexity ( 多个循环体)
two calculation rules: 乘法规则,加法规则.

Insert picture description here

2.5 Space complexity

The space complexity S(n) refers to 辅助空间the size used in the operation of the algorithm , denoted as: S(n)=(f(n))

  • Auxiliary space: In addition to storing algorithm 本身instructions, constants, variables and input data, it also needs to store storage units for data operations.
  • Algorithm 原地工作means that the auxiliary space required by the algorithm is constant, namely O(1).
  • Common is O(1), O(n) is more

Welcome to public concern number 理木客more exciting waiting for you to discover

Guess you like

Origin blog.csdn.net/qq_41941875/article/details/106126704