Data structure and algorithm (implemented by golang) (8.1) basic knowledge Preface

Time:2020-6-27

Basic knowledge

Learn data structures and algorithms. We need to know some basic knowledge.

1、 What is algorithm

Algorithm (English)algorithm)This word is broad and profound in Chinese, indicating the method of calculating accounts, or the strategy of strategizing. In computer technology, what does it mean?

A computer, as its name implies, is a machine for calculation. In computer science, the algorithm can be described as: the computer receives an input instruction, then processes it, and finally outputs the calculation result.

This kind of input process processing output is easy to understand with human behavior mode. For example, Xiaoming was asked by his mother to play soy sauce, and the order to play soy sauce was input. Xiaoming found that there were five stores around the community selling soy sauce. JUANJUAN supermarket was the nearest to home, while Zilong grocery store was the farthest away, but soy sauce was very cheap. In order to save money, Xiao Ming went to the farthest Zilong grocery store to buy soy sauce, and then smoothly returned home to his mother. The process of buying soy sauce is processing, and the soy sauce for mom is output.

Why didn’t Xiao Ming go to the nearest JUANJUAN supermarket and go to the farthest Zilong grocery store? This is the best plan that Xiao Ming had after thinking in his head. Of course, now you can buy soy sauce through takeout software, Xiaoming can open meituan takeout software, search for keywords: soy sauce, and then click filter, the nearest and cheapest from home, and then choose the cheapest soy sauce to place an order.

The process of buying soy sauce = the process of ordering meituan take out software.

In the evolution of thousands of years, human beings have been able to carry out numerical calculation, balance interests, build machines, give their own behavior patterns to machines, and liberate themselves. If we really understand the information transmission process of human brain neurons, we may even create a self-conscious machine, but this kind of scene can only be seen in science fiction movies.

So, this logical process, or behavior pattern, maps algorithms in the computer.

In a more accurate description: the algorithm is aLimited, definite, effectiveA method of solving a problem that is suitable to be implemented by a computer program. First of all, there is a problem, and then there is a method to solve it. This method is called algorithm.

The algorithm is limited, that is, the steps of the algorithm are limited, the execution time is limited, and the results can be obtained in a limited time. The algorithm is deterministic, that is, no matter how many times it is executed, the calculated results are the same. The algorithm is effective, that is, the calculated results are helpful to solve the problem.

However, the definition of the algorithm has been refreshed all the time. Because of the emergence of machine learning, the steps of machine learning algorithm are infinite based on massive and large-scale data, which can be calculated all the time, and the results of each calculation are not the same. However, if the steps are limited artificially and the training threshold is increased, the parameters obtained during training will stop immediately after exceeding the set threshold, which is also in line with the requirements The above definition.

The algorithm to be completed in a limited time is a burden to human beings, because the machines made by human beings are not strong enough. Why? Because even if the steps of the algorithm are limited, the execution time may be particularly long.

Just like the three gems needles under the temple of Benares, the Hindu holy land in the book from one to infinity, the God of Hinduism burnt the sky and said that who can move 64 gold pieces of the first gems needle to the third through the second gems needle, the burning tower, the temple, and the Brahman will turn into ashes, which is the famous hannota algorithm.

The Hanoi Tower problem can be described as:

There are three rods (NoA、B、C), inAThe rods are placed in order from bottom to top and from large to small64Gold plate (as shown below). The goal of the game: toAAll the gold plates on the pole are moved toCOn the rod, and still keep the original order of stacking.

Data structure and algorithm (implemented by golang) (8.1) basic knowledge Preface

Operation rules: only one plate can be moved at a time, and the big plate is always kept at the bottom and the small plate is kept at the top during the moving process. During the operation, the plate can be placed at the bottomA、B、COn any rod.

We naturally think of an algorithm:

  1. We have to useCRod, willAFront of poleN-1Plates, move toBAfter the rod, theAThe remaining plate of the pole, move directly toCRod, this timeAIt’s empty.
  2. And then with the help ofARod, willBRodlikeN-1Plates, move toCThe task is finished.

We use programming language to realize the simple idea:

package main

import "fmt"

var total = 0

//Hanoi Tower
//At first, there were n plates on pole a, and neither pole B nor pole C had plates.
func main() {
    N: = 4 // 64 plates
    A: = "a" // pole a
    B: = "B" // pole B
    C: = "C" // pole C
    tower(n, a, b, c)

    //When n = 1, the number of moves is 1
    //When n = 2, the number of moves is 3
    //When n = 3, the number of moves is 7
    //When n = 4, the number of moves is 15
    fmt.Println(total)
}

//It means to move n plates from the a-bar to the C-BAR with the help of the B-bar
func tower(n int, a, b, c string) {
    if n == 1 {
        total = total + 1
        fmt.Println(a, "->", c)
        return
    }

    tower(n-1, a, c, b)
    total = total + 1
    fmt.Println(a, "->", c)
    tower(n-1, b, a, c)
}

By induction, we can know the number of movesTotal(N)The relationship isTotal(N)=2*Total(N-1)+1For each additional plate, the number of moves will double and increase by one. We can know it through the related mathematical method of the number sequenceTotal(N)=2^N-1That is, the number of moves is an exponential equation:N power of 2The index is equal to the number of plates.

We calculated2^64-1=18446744073709551615You can know that a person moves every second day and night:18446744073709551615/3600/24/365/100000000=5849It will take 584.9 billion years to accomplish this, when the world may indeed have been destroyed.

In computer science, because all algorithms are human defined rules, rules are dead, so don’t worry about not learning. When you learn these algorithms, you will feel, wow, everything is so simple.

2、 What is data structure

Data structure, as the name implies, is the structure for storing data, and can also be considered as a container for storing data. For example, if you want to find the maximum of 1000 numbers, you should first record 1000 numbers on some cards, and then sort the cards.

Most algorithms need to organize data, so they produce data structures. Data structure in computer is mainly used to realize the basis of various algorithms, of course, data structure itself is also a part of the algorithm.

The basic data structures are: linked list, stack and queue, tree and graph.

Linked list is to link and associate data. One data node points to another data node, like an iron chain in nature. Most data structures are represented by several variants of linked list.

In each programming language, array is provided as basic data type, and array is a continuous memory storage space. Through subscript 0, 1, 2, you can quickly get the data at the specified position of array. The linked list can also be implemented by array, but generally, because the array is continuous, it is easy to cause redundancy and poor effect when adding and deleting nodes in the linked list. So the implementation of linked list in different programming languages is as follows:C、C++It’s done with pointers,JavaIt is implemented with classes, andGolangIt is implemented by using structure.

Stacks and queues are mainly used to store multiple data, but only one is first in first out and the other is first in first out. For example, when pushing down the stack, the data that enters the stack first can come out last. For the well-known queue, the people who queue first must get the service first.

Next is the tree and the graph. The tree has a tree root node, which stores data. There are many sub nodes below, which also stores data. It is similar to the tree in nature. The map can be compared with the map of nature. Multiple points point to multiple points, and there is one or more edges between points, and these points store data, while the edges can also store data, such as distance, etc.

Around these data structures, there are several extensions, plus some sorting and searching logic, which form a higher-level data structure.

Data structure is the auxiliary of algorithm implementation, which is to organize data structure more efficiently. Therefore, data structure and algorithm are closely related, so we can equate data structure with algorithm.

3、 What’s a good data structure and algorithm

The reason of learning algorithm is that a good algorithm can save resources, but it is difficult to choose a suitable algorithm. We need to do complex mathematical analysis to know what is good. In the computer, we call this kind of mathematical analysis algorithmic analysis.

What is a good data structure and algorithm?

  1. Computer resources are limitedSo the less computer resources are used, the better the data structure and algorithm.
  2. Human life is limitedYes, the waiting time is patient, so the faster the helper program completes the work, the better the data structure and algorithm.

So a theory is put forward: time and space algorithm complexity theory.

In the process of program execution, either space for time, or time for space. Space can be considered as a kind of computer resources such as memory usage, and time is the fourth dimension of human perception, which is slow or fast. Generally, they can not have both. If it is found that they have both, it is to invent a better algorithm.

In the forty or fifty years of the development of computer science, this kind of invention that saves both resources and time is still relatively few. For example, data compression algorithm, because of the invention of super efficient lossless data compression algorithm, when we watch video on the Internet, it is neither distorted, nor stuck, fast and good, which is called good algorithm.

At present, there is a new computing method under study, called quantum computing. It can calculate a huge amount of data in a very small space, using very few resources, and in a short time. Let’s hope that we can succeed in mass production. At that time, human productivity will be greatly liberated.

4、 Summary

In general, many people think that = data structure + algorithm.

We study data structure and algorithm to write faster and better code with higher efficiency.

Because we have learned, we don’t need to design from scratch, and the work efficiency is improved.

Because we know the complexity of each data structure and algorithm and the applicable scenarios, we can choose the combination freely, and the code we write will be faster and occupy less resources.

So we should study and understand the common data structure and algorithm.

Welcome to the rest of the chapter.

Series entry

I’m Chen Xingxing. Welcome to read what I wroteData structure and algorithm (implemented by golang), the article starts with a more friendly gitbook.

  • Data structure and algorithm (implemented by golang) (1) introduction to golang
  • Data structure and algorithm (implemented by golang) (2) simple introduction to golang package, variable and function
  • Data structure and algorithm (implemented by golang) (3) simple introduction to golang process control statement
  • Data structure and algorithm (gorang Implementation) (4) simple introduction to gorang structure and method
  • Data structure and algorithm (implemented by golang) (5) simple introduction to golang interface
  • Data structure and algorithm (implemented by golang) (6) simple introduction to golang – concurrency, cooperation and channel
  • Data structure and algorithm (golang Implementation) (7) simple introduction to golang standard library
  • Data structure and algorithm (implemented by golang) (8.1) basic knowledge Preface
  • Data structure and algorithm (implemented by golang) (8.2) basic knowledge – divide and conquer and recursion
  • Data structure and algorithm (implemented by golang) (9) basic knowledge algorithm complexity and progressive symbols
  • Data structure and algorithm (implemented by golang) (10) basic knowledge algorithm complexity master method
  • Data structures and algorithms (implemented in golang) (11) common data structures – Foreword
  • Data structure and algorithm (implemented by golang) (12) common data structure linked list
  • Data structure and algorithm (implemented by golang) (13) common data structure variable length array
  • Data structures and algorithms (implemented in golang) (14) common data structures – stacks and queues
  • Data structure and algorithm (implemented by golang) (15) common data structure list
  • Data structure and algorithm (implemented by golang) (16) common data structure dictionary
  • Data structure and algorithm (implemented by golang) (17) common data structure tree
  • Data structure and algorithm (implemented by golang) (18) sorting algorithm foreword
  • Data structure and algorithm (implemented by golang) (19) sorting algorithm bubble sorting
  • Data structure and algorithm (implemented by golang) (20) sorting algorithm – selective sorting
  • Data structure and algorithm (implemented by golang) (21) sorting algorithm insert sorting
  • Data structure and algorithm (implemented by golang) (22) sorting algorithm Hill sorting
  • Data structure and algorithm (implemented by golang) (23) sorting algorithm merging sorting
  • Data structure and algorithm (implemented by golang) (24) sorting algorithm priority queue and heap sorting
  • Data structure and algorithm (implemented by golang) (25) sorting algorithm – fast sorting
  • Data structure and algorithm (implemented by golang) (26) lookup algorithm hash table
  • Data structure and algorithm (implemented by golang) (27) search algorithm binary search tree
  • Data structure and algorithm (implemented by golang) (28) search algorithm AVL tree
  • Data structure and algorithm (implemented by golang) (29) search algorithm-2-3 tree and left leaning red black tree
  • Data structure and algorithm (implemented by golang) (30) search algorithm-2-3-4 tree and common red black tree