Basic concepts and terminology ———— data structure and algorithm fundamentals

Source: Internet
Author: User
Tags abstract definition constant
data structure and algorithm Foundation 1.1 Object of data structure research

Steps for the computer to solve the problem:


1.3 Basic concepts and terminology

1. Data: A collection of symbols that can be entered into a computer and can be processed by the computer.

2. Data element: The basic unit of data, which is usually considered and processed as a whole in computer processing and program design.

3. Data item: the smallest unit of data that is indivisible. A data element can consist of several data items.

4. Data object: Is a collection of data elements that have the same characteristics and is a subset of the data.

5. Data structure: A collection of data elements that are one or more specific relationships that exist between each other. The relationship between elements is called a struct, and the form is defined as a two-tuple. That

Data_structure= (D,s)

Where: D is a finite set of data elements, and S is a finite set of relationships on D.


1. Logical Structure of data: refers to the logical relationship between data elements in a structure. There are four basic structures for data logic:

(1) Collection: There is no other relationship between the data elements in the structure, except for the same part of a collection.

(2) Linear structure: There is a one-to-one relationship between the data elements in the structure.

(3) tree structure: There is a one-to-many relationship between the data elements in the structure.

(4) Graph structure (mesh structure): There are many-to-many relationships between the data elements in the structure.

Diagram of the four basic structures: (their complexity is incremented sequentially)

Set structure: Linear structure:

Tree structure: Mesh structure:

The storage structure of the data (physical structure): is how the logical structure of the data is stored in the computer's memory. Also known as the physical structure.

(1) Sequential storage structure: the logical order between elements is represented by the relative position of data elements in memory.

(2) Chained storage structure: the use of pointers in nodes to represent the relationship between data elements. (It is usually implemented using pointers to programming languages)

Feature: Represents a logical relationship between data elements with a pointer indicating the storage address of the element.


1.4 Data types and abstract types 1.4.1 data Types

Data type: In a programming language, a variable has a type of data that defines the range of variables and the operations that can be done.


1.4.2 Abstract data Types

Abstract data Type,adt: Refers to a mathematical model and a set of operations defined on the model.

We do not need to understand its implementation methods and details, only concerned about its logical characteristics.

Features: Use and implementation of phase separation, implementation of encapsulation and concealment. When designing an abstract data type, separate the definition of the type from its implementation.


Three elements of an abstract data type: elements, relationships, operations. An abstract data type can be expressed as a ternary group:

(d,r,p)

Where: D is the data object, R is the relationship set on D, and P is the basic set of operations on D.


1.5 algorithm and algorithm analysis 1.5.1 Algorithm

Algorithm (algorithm): A description of the solution steps for a particular problem, which is a finite sequence of instructions, where each command represents one or more operations.

Key features of the algorithm:

1. Poor: An algorithm must always end after a poor step, and each step must be completed in a poor time.

2. Feasibility: The operations described in an algorithm must be accomplished through the finite execution of the basic operations already implemented.

3. Certainty: Each instruction in the algorithm must have no ambiguity and must have the same result for the same input.

4. An algorithm has 0 or more inputs: these inputs are taken from a collection of specific objects.

5. An algorithm has one or more outputs: they are the quantities that have certain relationships with the input, usually the result of processing the input.

The first three are the basic features of the algorithm.


A good algorithm should meet the following requirements:

1. Correctness: usually contains the following levels:

The ① program does not contain syntax errors.

The ② program can produce the required results for the given input data of the unit.

The ③ program for carefully selected, typical, demanding and difficult-to-

The input of the group can result in satisfying the requirements.

The ④ program can obtain the result of satisfying the requirements for all legal inputs.

2. Readability: The algorithm should be easy to understand, obscure difficult to debug.

3. Robustness: When the input data is illegal, you should return a value that represents the nature of the error or error.

4. High efficiency: The algorithm should have time execution time and the maximum storage space during algorithm execution, time complexity and space complexity are both not available. 1.5.2 Algorithm Analysis

Algorithm analysis mainly refers to the efficiency of the analysis algorithm.


1. Time complexity of the algorithm

(1) Post-mortem method: mainly in the key parts of the algorithm to insert the timer, when the algorithm executes when the timer is opened, the termination of the shutdown, to obtain the algorithm execution time difference, so as to measure the efficiency of the algorithm. However, it has a large defect.

(2) Pre-analysis and estimation method:

The size of the ① algorithm

The scale of the ② problem

③ programming language

④ Compiled code Quality

⑤ speed of machine execution instructions

In order to avoid the influence of the hardware and software environment factors, assuming that the running time of an algorithm is only the function of the problem scale, then the strategy adopted by the algorithm will directly determine the efficiency of the algorithm in the case of the problem scale. All the methods of solving time complexity are only related to the scale of the problem.

An algorithm consists of the control structure and the original operation.

Remember that the number of times the original operation was repeatedly executed is a function of the relationship between the problem size n: F (n).

When discussing the running time of a program, we focus on the growth rate of time. As the size of the problem increases, its growth rate is similar to that of F (n), so they are consistent in order of magnitude. Therefore, when the problem scale n tends to ∞, the order of magnitude of F (N) becomes the asymptotic time complexity of the algorithm, which is referred to as the time complexity, which is recorded as T (N).

T (n) =o (f (n))

It indicates that the growth rate of the algorithm execution time and the growth rate of f (n) are the same as the problem size n increases.

Computational method of time complexity:

Assuming that the execution time of each statement is per unit time, the time complexity of the algorithm is the sum of the execution frequency of all the operator statements in the algorithm.


Example 1-5 assignment statement

Temp=1;
I=j;
J=temp;

Time complexity t (n) =o (1)

The algorithm execution time is a constant that is independent of the problem size n, the time complexity of the algorithm is the constant order, namely: T (N) =o (1).


example 1-6 simple loop

y=0;
for (k=1;k<=n;k++)  		//Nesting layer 0
    x + +;
for (i=1;i<=n;i++) 		//Nesting layer 1 for
    (j=1;j<=n;j++)
        y++;    		Based on the frequency of this statement, nested

Time complexity t (n) =o (n2), when there are several loop statements, the time complexity of the algorithm is determined by the frequency of the innermost statement in the Loop statement with the highest number of nested layers.


example 1-7 Selecting a branch structure

if (x>n)
x + +;
else for
   (j=1;j<=n<j++)
       x + +;

Time complexity t (n) =o (n)

If you choose a branching structure, the time complexity is determined by the longest-consuming portion of each branch.


example 1-8 finds an element with a value of K in array A[n], returns its position I (0<=i<n) if found, otherwise returns-1.

i=n-1;
while ((i>=0) && (a[i]!=k))
   i--;
return i;

The problem is not only related to the size n, but also to the position of the element with the value K in array a. So divide the situation: best case (found in the beginning) time complexity is T (n) =o (1); The worst case scenario (the last time the target element was found) is the time complexity of T (n) =o (n). (Increase in complexity in turn)


2. Complexity of Space

Definition: The storage space consumed by the algorithm, which is also a function of the problem size, is recorded as S (n) =o (f (n)). Progressive spatial complexity is also the spatial complexity degree.

The amount of storage space that an algorithm occupies on a computer depends on three points:

(1) Storage space occupied by the storage algorithm itself

(2) Storage space occupied by the input/output data of the algorithm

(3) The storage space that the algorithm temporarily occupies during operation


Summary

Figure out the basic terms of the data structure, identify the variables in the two-tuple, distinguish the logical structure of the data and the concept of the storage structure, clarify the abstract data type (ADT) and the meaning of variables in the ternary group, clarify the definition of the algorithm, characteristics and design requirements of the algorithm, and focus on the algorithm's time complexity analysis method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.